- Home /
How could I collect all gameobject Materials?
I have a base cube prefab with six materials - one per side, representing a virtual dice-like object that can change it's texture per side. Originally, I was going to use a Materials[] array to collect the materials, but realized that might not be the best way. If it is, I can easily revert to that, but I was also thinking about adding a specific string/int keys beyond 0,1,2,3,4,5. A dictionary feels like it may work, but I can't easily drag and drop materials into the Editor to assign them with that.
Answer by supercouge · Jul 15, 2013 at 09:08 AM
I don't think there is a problem with a Material[] array to collect the materials (even if, regarding your problem, a Texture[] array may be a better idea).
Just like you mentioned, Unity doesn't allow to access a Dictionnary through script. You could do your own EditorScript, but it may be a little overkill. Anyway, if you want to use a dictionnary, you can do something like this:
public Material[] mats = new Material[6];
protected Dictionary<string, Material> dMats = new Dictionary<string, Material>();
// Use this for initialization
void Start () {
if(mats.Length != 6) Debug.LogError("should have 6 materials");
for(int i = 0; i < mats.Length; i++) {
if(mats[i] == null) Debug.LogError("should not be null");
dMats.Add ("face_"+i.ToString(), mats[i]);
}
}