hi,
I have a “sprite” prefab with a simple double triangle (its not the unity thing, its a 3dsmax fbx).
i make levels where a “sprite” is often used only one time (background things).
I want to place it in the scene and change the material (texture offset from one atlas).
i find boring to duplicate the material in the asset manager, apply it to the new “sprite” every time.
i made a gui thing that do :
if(GUILayout.Button("Duplicate Mat")) {
Selection.activeGameObject.renderer.material =
new Material(Selection.activeGameObject.renderer.material);
}
So yes, it make a new material “materialXX (instance)” in the scene but not in the asset manager.
so unity cry saying there is a leaked material, but is it a big problem?
one solution i thought is to make a script for the sprite with a material variable that it is applied at the start of the game. but i can’t see it in the scene view so it is difficult to make levels.
Another one is a gui script that duplicate material in the asset manager, rename it, and assign it to the selection. duplicate and rename was ok but i dont know why, script dont want to assign it to the selection (script is below). it seemed an easy and fun solution.
do you have a better solution?
newmatname = EditorGUILayout.TextField(newmatname);
if(GUILayout.Button("Duplicate Material")) {
//Take the path, add the new name, add .mat
newpath=AssetDatabase.GetAssetPath(Selection.activeGameObject.renderer.sharedMaterial).Substring(0,AssetDatabase.GetAssetPath(Selection.activeGameObject.renderer.sharedMaterial).LastIndexOf("/")) +"/"+newmatname+".mat";
// Duplicate with the new name
AssetDatabase.CopyAsset(AssetDatabase.GetAssetPath(Selection.activeGameObject.renderer.sharedMaterial), newpath);
//Try to assign the new material asset to my fuck**g Selection but dont work. asset path was good though. but material assign give a null material
Selection.activeGameObject.renderer.sharedMaterial=AssetDatabase.LoadAssetAtPath(newpath, typeof(Material));
//other try :
//mat = EditorGUIUtility.Load(newpath, (typeof(Material))) as Material;
}