Hi, I have leaked all materials in their gameobjects because I called renderer.material instead renderer.sharedMaterial in a editor script…
I tried some scripting to fix it with no luck…
Anyone can help to remove instances and set the real material? Any ideas?
Cheers
What kinds of problems is this causing in your project?
Basically I made a editor script that sets renderer.material on all gameObjects. this causes the problem. Now, I use renderer.sharedMaterial instead, but my materials are still leaked…
Anyone can help me doing a “Unleak mats” script??
Ok, the below script basically find the real material by name and sets it on renderer.sharedMaterial, BUT NOT WORKS:-|
I really don’t know why…
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
public class UnleakMaterials : MonoBehaviour
{
[MenuItem("Scripts/Unleak mats")]
static void mats()
{
Undo.RegisterSceneUndo("Unleak mats");
GameObject[] gobjects = (GameObject[])Resources.FindObjectsOfTypeAll(typeof(GameObject));
foreach (GameObject g in gobjects)
{
if (g.renderer)
{
Debug.Log(g.renderer.sharedMaterial.name.Substring(0, g.renderer.sharedMaterial.name.Length - 11));
foreach (Material mat in Resources.FindObjectsOfTypeAll(typeof(Material)))
{
if (g.renderer.sharedMaterial.name.Substring(0, g.renderer.sharedMaterial.name.Length - 11) == mat.name)
{
g.renderer.sharedMaterial = mat;
Debug.Log("Mat Unleaked! (" + mat.name + ")");
}
}
}
}
}
}
It takes a while, but looks like the last if statament is never executed… nothing changes…
Anyone know where is my error??