[Solved] Set material texture in inspector by script

I have a “Tile Prefab” prefab with a quad mesh component and a script called “Tile” and a public enum value TileType, which I defined in another script. When the game runs the instances of Tile Prefab use the selected enum value to set - among other things - the texture of the quad’s material.

This is all working fine, but I’m wondering if there’s a way to write a script such that when I’m looking at the scene in the editor it will show the tiles with the texture indicated by the enum instead of being untextured. I suppose it would work even just to set the texture value on the instace whenever the enum value is set.

I poked around the API docs, but I don’t think I know what kind of terms to search for.

Any advice?


Update:

Okay, I fond a few things but I’m still stuck on one detail. Here’s my editor script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Tile))]
public class TileEditor : Editor {

    SerializedProperty val;

    private void OnEnable()
    {
        val = serializedObject.FindProperty("type");
    }

    public override void OnInspectorGUI()
    {
        Tile tile = (Tile)target;
        int startIndex = (int)tile.type;

        serializedObject.Update();
        EditorGUILayout.PropertyField(val);
        serializedObject.ApplyModifiedProperties();

        int index = (int)tile.type;
        if(startIndex != index)
        {
            //~~! need to be able to read from database statically
            //TileData data = tile.db.tiles[index];
            //tile.rend.material.mainTexture = data.texture;
            //serializedObject.ApplyModifiedProperties();
        }


    }

}

I think this is more or less on the right track, but the problem I’m running into is that the Textures aren’t loaded (?) in the editor. Right now the way I have the tile type database set up, I just enter the name/description/texture in the editor in a public List on the TileDB script instance. That’s all runtime stuff, but I need to be able to pick the texture to assign to the tile in the editor. I can convert the TileDB to hold a public readonly array of TileData, but I need to know how to populate the Texture members for each entry.

Any advice is appreciated.

So after giving it a little time I ended up with a different approach that works seamlessly. Rather than keying from a set of enumerated values I just key from the actual tile database names. That is, instead of picking the type index from an enum dropdown, I just list the actual names of the database entries, and I got rid of the enum altogether.

Here’s the code in case anyone else ever wants to do something similar:

//TileEditor.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Tile))]
public class TileEditor : Editor {
    SerializedObject sObject;
    SerializedProperty sProperty;
    TileDB db;

    public void OnEnable() {
        sObject = new SerializedObject(target);
        sProperty = sObject.FindProperty("type");
        db = FindObjectOfType<TileDB>();
    }

    public override void OnInspectorGUI() {
        sObject.Update();
    
        int val = sProperty.intValue;
        val = EditorGUILayout.Popup("Type", val, db.TypeNames());
        sProperty.intValue = val;

        if (sObject.ApplyModifiedProperties()) {
            var tile = (Tile)target;
            var renderer = tile.transform.Find("Floor").GetComponent<MeshRenderer>();
            renderer.sharedMaterial = db.types[sProperty.intValue].floorMaterial;
        }
    }

}
//TileDB.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TileDB : MonoBehaviour {
    [System.Serializable]
    public struct Data {
        public string Name;
        public Material floorMaterial;
        public Material sideMaterial;
    }

    public List<Data> types;

    public string[] TypeNames() {
        List<string> names = new List<string>();
        foreach (var type in types) { names.Add(type.Name); }
        return names.ToArray();
    }
}

3226010--247468--80cfa43b8d.png 3226010--247469--240fd85e8b.png