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.