Skip to content

Commit 353b5a1

Browse files
committed
Addressing Scrolling Object Collection related issues
1 parent d26e5fc commit 353b5a1

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

Assets/MRTK/Core/Utilities/Rendering/MaterialInstance.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void ReleaseMaterial(Object owner, bool autoDestroy = true)
8686

8787
if (autoDestroy && materialOwners.Count == 0)
8888
{
89-
DestorySafe(this);
89+
DestroySafe(this);
9090

9191
// OnDestroy not called on inactive objects
9292
if (!gameObject.activeInHierarchy)
@@ -295,7 +295,7 @@ private static void DestroyMaterials(Material[] materials)
295295
{
296296
for (var i = 0; i < materials.Length; ++i)
297297
{
298-
DestorySafe(materials[i]);
298+
DestroySafe(materials[i]);
299299
}
300300
}
301301
}
@@ -321,7 +321,7 @@ private static bool HasValidMaterial(Material[] materials)
321321
return false;
322322
}
323323

324-
private static void DestorySafe(UnityEngine.Object toDestroy)
324+
private static void DestroySafe(Object toDestroy)
325325
{
326326
if (toDestroy != null)
327327
{

Assets/MRTK/Core/Utilities/StandardShader/ClippingPrimitive.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void RemoveRenderer(Renderer _renderer)
160160
}
161161
}
162162

163-
private void RemoveRenderer(int index)
163+
private void RemoveRenderer(int index, bool autoDestroyMaterial = true)
164164
{
165165
Renderer _renderer = renderers[index];
166166

@@ -181,21 +181,21 @@ private void RemoveRenderer(int index)
181181
var materialInstance = _renderer.GetComponent<MaterialInstance>();
182182
if (materialInstance != null)
183183
{
184-
materialInstance.ReleaseMaterial(this);
184+
materialInstance.ReleaseMaterial(this, autoDestroyMaterial);
185185
}
186186
}
187187
}
188188

189189
/// <summary>
190190
/// Removes all renderers in the list of objects this clipping primitive clips.
191191
/// </summary>
192-
public void ClearRenderers()
192+
public void ClearRenderers(bool autoDestroyMaterial = true)
193193
{
194194
if (renderers != null)
195195
{
196196
while (renderers.Count != 0)
197197
{
198-
RemoveRenderer(renderers.Count - 1);
198+
RemoveRenderer(renderers.Count - 1, autoDestroyMaterial);
199199
}
200200
}
201201
}
@@ -314,9 +314,12 @@ protected virtual void UpdateRenderers()
314314
for (int i = renderers.Count - 1; i >= 0; --i)
315315
{
316316
var _renderer = renderers[i];
317-
if (Application.isPlaying && _renderer == null)
317+
if (_renderer == null)
318318
{
319-
RemoveRenderer(i);
319+
if (Application.isPlaying)
320+
{
321+
RemoveRenderer(i);
322+
}
320323
continue;
321324
}
322325

Assets/MRTK/SDK/Features/UX/Scripts/ScrollingObjectCollection/ScrollingObjectCollection.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,22 @@ private void OnDisable()
11831183
CoreServices.InputSystem?.UnregisterHandler<IMixedRealityTouchHandler>(this);
11841184
CoreServices.InputSystem?.UnregisterHandler<IMixedRealityPointerHandler>(this);
11851185

1186+
// Currently in editor duplicating prefab GameObject containing both TMP and non-TMP children inside the Scrolling Object Collection container causes material life cycle management issues
1187+
// https://github.com/microsoft/MixedRealityToolkit-Unity/issues/9481
1188+
// Thus we do not automatically destroy material controlled by Material Instance if the OnDisable comes from pasting in editor
1189+
#if UNITY_EDITOR
1190+
if (!Application.isPlaying)
1191+
{
1192+
bool? isCalledFromPastingGameObject = new System.Diagnostics.StackFrame(1)?.GetMethod()?.Name?.Contains("Paste");
1193+
RestoreContentVisibility(!isCalledFromPastingGameObject.GetValueOrDefault());
1194+
}
1195+
else
1196+
{
1197+
RestoreContentVisibility();
1198+
}
1199+
#else
11861200
RestoreContentVisibility();
1201+
#endif
11871202

11881203
if (useOnPreRender && cameraMethods != null)
11891204
{
@@ -1643,9 +1658,9 @@ private void RemoveRenderersFromClippingObject(List<Renderer> renderers)
16431658
/// <summary>
16441659
/// Removes all renderers currently being clipped by the clipping box
16451660
/// </summary>
1646-
private void ClearClippingBox()
1661+
private void ClearClippingBox(bool autoDestroyMaterial = true)
16471662
{
1648-
ClipBox.ClearRenderers();
1663+
ClipBox.ClearRenderers(autoDestroyMaterial);
16491664
}
16501665

16511666
/// <summary>
@@ -1897,9 +1912,9 @@ private void ResetScrollOffset()
18971912
/// <summary>
18981913
/// All inactive content objects and colliders are reactivated and renderers are unclipped.
18991914
/// </summary>
1900-
private void RestoreContentVisibility()
1915+
private void RestoreContentVisibility(bool autoDestroyMaterial = true)
19011916
{
1902-
ClearClippingBox();
1917+
ClearClippingBox(autoDestroyMaterial);
19031918
ManageVisibility(true);
19041919
}
19051920

0 commit comments

Comments
 (0)