3
votes

I have an object which has for example 5 meshes where I would like to change the color in runtime. But every mesh has different colors to change. For example mesh 1 can be changed to blue, red and orange and mesh 2 can be changed to orange, yellow,green,purple and white.

I would like to have a c# script where I have an array with all 5 meshes. And for each array I would like to have another array appear in the inspector, where I can put in the different colors. This is no problem if the mesh count would always be 5. But I want it to be flexible. For example, another object only has 3 meshes than I only need 3 color arrays instead of 5.

I have never worked with Editor Scripts, but maybe there is a easy workaround for that?

I am working with Unity 5.3

Thank you.

1
What object type is that "Mesh" you speak of? Do you mean GameObjects which are the children of another GameObject or are you talking about UnityEngine.Mesh objects? Can you give an example object hierarchy? As I understand, you basically want a script that has a UnityEngine.Color[] field which has the length of the number of "meshes", then color each mesh according to that color (via a Shader? material.color?) - Maximilian Gerhardt
I mean, I will have a gameobject with several child objects. And I would like to put some of those child objects into an array, than for each element in that array I will get a new Array with colors. And those colors are coloring that mesh / object trough material.color. So yes, exactly like you said I guess. - Jenny

1 Answers

0
votes
public class Test: MonoBehaviour
{
    public Models [] models;
    private int index = 0;
    [SerializeField] private MeshFilter meshFilter = null;
    [SerializeField] private Renderer rend = null;
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            if(++index == models.Length)index = 0;
            meshFilter.mesh = models[index].mesh;
            int currIndex = models[index].currentIndex;
            rend.material = models[index].materials[currIndex];
        }
        if(Input.GetKeyDown(KeyCode.A))
        {
            if(++models[index].currentIndex == models[index].materials.Length)models[index].currentIndex = 0;
            int currIndex = models[index].currentIndex;
            rend.material = models[index].materials[currIndex];
        }
    }   
}
[System.Serializable]
public class Models{
    public Mesh mesh;
    public int currentIndex;
    public Material[]materials;
}

This will show the Models array in your inspector. You can give a size for it, then each element opens up a mesh slot and an array of materials. Give it a size and you can start dragging materials.

I would suggest to add 2 items in the array, then add two meshes (Unity has some default ones like sphere and cube). Then create some basic materials of colors and add them to the materials arrays of each object.

Then you can run and press Space to change mesh and A to change color. You just need to use your own after that.