I have a lot of duplicated objects, and I need in editor, not in the game, remove specific (18) material slot. Remove not material in slot 18, but remove slot 18. Manually, by hands, it can be done using this button:
All objects that I need to remove slot from has the next mutuals:
- Contain "_lod" in the name;
- Have the same mesh, with the same GUID
I did not find how to get GUID of the given object, so the search, probably need to go by name.
I have never scripted Unity, I even did not write in C# (although I write in C++).
What I done so far: I created an empty game object and assign cs script below to it as a component. DestroyImmediate(myMaterials[18])
, works almost that I need, but it removes not the slot, but the material on it (and automatically creates default material). I need to remove exactly the slot.
LookAtPoint.cs
:
using UnityEngine.SceneManagement;
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEditor;
[ExecuteInEditMode]
public class LookAtPoint : MonoBehaviour
{
void Iter(GameObject gameObject) // Itterate children
{
for (int j = 0; j < gameObject.transform.childCount; j++)
{
GameObject a = (GameObject)gameObject.transform.GetChild(j).gameObject;
MeshFilter viewedModelFilter = (MeshFilter)a.GetComponent("MeshFilter");
if(viewedModelFilter && viewedModelFilter.mesh && a.name.IndexOf("_lod") != -1) // check if object has mesh and it's name contains "_lod"
{
Material[] myMaterials = a.GetComponent<Renderer>().materials;
if(myMaterials[18].name.IndexOf("Подоконник")!=-1) // not neccesary check if 18s slot has "Подоконник" name (actually now it is already "Default-Material")
{
DestroyImmediate(myMaterials[18]);
}
}
Iter(a);
}
}
void Start()
{
List<GameObject> rootObjects = new List<GameObject>();
Scene scene = SceneManager.GetActiveScene();
scene.GetRootGameObjects( rootObjects );
for (int i = 0; i < rootObjects.Count; ++i)
{
GameObject gameObject = rootObjects[ i ]; // get root objects
for (int j = 0; j < gameObject.transform.childCount; j++) // enumarate them
{
Iter(gameObject); // iteratevely process the children
}
}
}
}
Material[] myMaterials = object.GetComponent<Renderer>().materials;
, but I can't remove it.Destroy
does not compile,DestroyImmediate
removes material but not the slot. I need exactly remove the slot (object have few materials slots). Probably rewrite my question to specify this \$\endgroup\$