Skip to content

Commit ec1e796

Browse files
Stephen HodgsonStephen Hodgson
authored andcommitted
Changed sharedMaterial reference to just material reference to prevent changing all materials. Also explicitly destroy material reference when we destroy the game object.
1 parent dc583a0 commit ec1e796

File tree

7 files changed

+39
-7
lines changed

7 files changed

+39
-7
lines changed

Assets/HoloToolkit/Input/Tests/Scripts/FocusedObjectColorChanger.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class FocusedObjectColorChanger : MonoBehaviour, IFocusable
1717

1818
private void Awake()
1919
{
20-
cachedMaterial = GetComponent<Renderer>().sharedMaterial;
20+
cachedMaterial = GetComponent<Renderer>().material;
2121
originalColor = cachedMaterial.GetColor("_Color");
2222
}
2323

@@ -30,5 +30,10 @@ public void OnFocusExit()
3030
{
3131
cachedMaterial.SetColor("_Color", originalColor);
3232
}
33+
34+
private void OnDestroy()
35+
{
36+
DestroyImmediate(cachedMaterial);
37+
}
3338
}
3439
}

Assets/HoloToolkit/Input/Tests/Scripts/SelectedObjectMessageReceiver.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class SelectedObjectMessageReceiver : MonoBehaviour
1717

1818
private void Awake()
1919
{
20-
cachedMaterial = GetComponent<Renderer>().sharedMaterial;
20+
cachedMaterial = GetComponent<Renderer>().material;
2121
originalColor = cachedMaterial.GetColor("_Color");
2222
}
2323

@@ -30,5 +30,10 @@ public void OnClearSelection()
3030
{
3131
cachedMaterial.SetColor("_Color", originalColor);
3232
}
33+
34+
private void OnDestroy()
35+
{
36+
DestroyImmediate(cachedMaterial);
37+
}
3338
}
3439
}

Assets/HoloToolkit/Input/Tests/Scripts/SphereGlobalKeywords.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private void Awake()
1717
cachedChildMaterials = new Material[childRenderers.Length];
1818
for (int i = 0; i < childRenderers.Length; i++)
1919
{
20-
cachedChildMaterials[i] = childRenderers[i].sharedMaterial;
20+
cachedChildMaterials[i] = childRenderers[i].material;
2121
}
2222
}
2323
}
@@ -34,5 +34,13 @@ public void OnSpeechKeywordRecognized(SpeechKeywordRecognizedEventData eventData
3434
break;
3535
}
3636
}
37+
38+
private void OnDestroy()
39+
{
40+
for (int i = 0; i < cachedChildMaterials.Length; i++)
41+
{
42+
DestroyImmediate(cachedChildMaterials[i]);
43+
}
44+
}
3745
}
3846
}

Assets/HoloToolkit/Input/Tests/Scripts/SphereKeywords.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class SphereKeywords : MonoBehaviour, ISpeechHandler
1212

1313
private void Awake()
1414
{
15-
cachedMaterial = GetComponent<Renderer>().sharedMaterial;
15+
cachedMaterial = GetComponent<Renderer>().material;
1616
}
1717

1818
public void ChangeColor(string color)
@@ -35,5 +35,10 @@ public void OnSpeechKeywordRecognized(SpeechKeywordRecognizedEventData eventData
3535
{
3636
ChangeColor(eventData.RecognizedText);
3737
}
38+
39+
private void OnDestroy()
40+
{
41+
DestroyImmediate(cachedMaterial);
42+
}
3843
}
3944
}

Assets/HoloToolkit/Input/Tests/Scripts/TestButton.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected virtual void OnEnable()
105105
// Set the initial alpha
106106
if (ToolTipRenderer != null)
107107
{
108-
cachedToolTipMaterial = ToolTipRenderer.sharedMaterial;
108+
cachedToolTipMaterial = ToolTipRenderer.material;
109109

110110
Color tipColor = cachedToolTipMaterial.GetColor("_Color");
111111
tipColor.a = 0.0f;
@@ -231,5 +231,10 @@ public void OnFocusExit()
231231

232232
UpdateVisuals();
233233
}
234+
235+
private void OnDestroy()
236+
{
237+
DestroyImmediate(cachedToolTipMaterial);
238+
}
234239
}
235240
}

Assets/HoloToolkit/Sharing/Scripts/Utilities/Utils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static void SetMaterialRecursiveForName(Transform t, Material mat, string
126126
{
127127
if (t.gameObject && t.gameObject.GetComponent<Renderer>() && t.gameObject.name == nameToTest)
128128
{
129-
t.gameObject.GetComponent<Renderer>().sharedMaterial = mat;
129+
t.gameObject.GetComponent<Renderer>().material = mat;
130130
}
131131

132132
for (int ii = 0; ii < t.childCount; ++ii)

Assets/HoloToolkit/Utilities/Scripts/DirectionIndicator.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public class DirectionIndicator : MonoBehaviour
3434
// Cache the MeshRenderer for the on-cursor indicator since it will be enabled and disabled frequently.
3535
private Renderer directionIndicatorRenderer;
3636

37+
// Cache the Material to prevent material leak.
38+
private Material indicatorMaterial;
39+
3740
// Check if the cursor direction indicator is visible.
3841
private bool isDirectionIndicatorVisible;
3942

@@ -60,6 +63,7 @@ public void Awake()
6063

6164
public void OnDestroy()
6265
{
66+
DestroyImmediate(indicatorMaterial);
6367
Destroy(DirectionIndicatorObject);
6468
}
6569

@@ -90,7 +94,7 @@ private GameObject InstantiateDirectionIndicator(GameObject directionIndicator)
9094
Destroy(rigidBody);
9195
}
9296

93-
Material indicatorMaterial = directionIndicatorRenderer.sharedMaterial;
97+
indicatorMaterial = directionIndicatorRenderer.material;
9498
indicatorMaterial.color = DirectionIndicatorColor;
9599
indicatorMaterial.SetColor("_TintColor", DirectionIndicatorColor);
96100

0 commit comments

Comments
 (0)