Skip to main content
added 1930 characters in body; edited title
Source Link

Create Remove material slot via Unity Editor script to do same action for multiple objectsScript

I have a lot of duplicated objects, and I need in editor, not in the game, apply some changes to the objectsremove specific (remove specific material18) material slot. Of course I can do this manuallyRemove not material in slot 18, but there are a lot of themremove slot 18. Manually, by hands, it can be done using this button:

In caseenter image description here

All objects that I need to remove slot from has the next mutuals:

  1. Contain "_lod" in the name;
  2. Have the same mesh, with the same GUID

I did not find how to get GUID of Javascriptthe given object, you can just open consoleso the search, and get all stuff Youprobably need to go by methods like getElementsByClassName. It would be perfect if some similar functionality exist (at least as addons) in Unityname.

I do not need some UI for using script currentlyhave never scripted Unity, and I also doeven did not need to run it on Unity launch.write in C# (although I want to call it every time by pressing some keywrite in editorC++).

So, seems thatWhat I have to enumarate alldone so far: I created an empty game objects, get their meshes, check GUID,object and if it equalsassign cs script below to it as a component. DestroyImmediate(myMaterials[18]), works almost that I need, then enumarete object's materials listbut it removes not the slot, find by GUIDbut the material on it (and automatically creates default material). I need, and call some remove method to itremove exactly the slot.

Point me where I should look, and should learn to achive that.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 
            }
        }
    }
}

Create Unity Editor script to do same action for multiple objects

I have a lot of duplicated objects, and I need in editor, not in the game, apply some changes to the objects (remove specific material). Of course I can do this manually, but there are a lot of them.

In case of Javascript, you can just open console, and get all stuff You need by methods like getElementsByClassName. It would be perfect if some similar functionality exist (at least as addons) in Unity.

I do not need some UI for using script currently, and I also do not need to run it on Unity launch. I want to call it every time by pressing some key in editor.

So, seems that I have to enumarate all game objects, get their meshes, check GUID, and if it equals to that I need, then enumarete object's materials list, find by GUID material I need, and call some remove method to it.

Point me where I should look, and should learn to achive that.

Remove material slot via Unity Script

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:

enter image description here

All objects that I need to remove slot from has the next mutuals:

  1. Contain "_lod" in the name;
  2. 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 
            }
        }
    }
}
Source Link

Create Unity Editor script to do same action for multiple objects

I have a lot of duplicated objects, and I need in editor, not in the game, apply some changes to the objects (remove specific material). Of course I can do this manually, but there are a lot of them.

In case of Javascript, you can just open console, and get all stuff You need by methods like getElementsByClassName. It would be perfect if some similar functionality exist (at least as addons) in Unity.

I do not need some UI for using script currently, and I also do not need to run it on Unity launch. I want to call it every time by pressing some key in editor.

So, seems that I have to enumarate all game objects, get their meshes, check GUID, and if it equals to that I need, then enumarete object's materials list, find by GUID material I need, and call some remove method to it.

Point me where I should look, and should learn to achive that.