Question by
pcapon_work · Aug 17, 2018 at 04:22 PM ·
inspectoreditor-scriptingtexture2ddropdownpopup
How to add Texture2D to Popup/Dropdown
I'm working on a Custom Inspector that allows the Designer to select a Color to apply to an Enemy Sprite from a Popup/Dropdown. I can't figure out how to display Texture2D's in the Popup/Dropdown. Text is no problem. Is there a way to do this?
Code:
[CustomEditor(typeof(EnemySpawnController))]
public class EnemySpawnController_CustomInspector : Editor
{
#region FIELDS
private const int TEXTURE_WIDTH = 32;
private const int TEXTURE_HEIGHT = 32;
private bool m_drawDefaultInspector = false;
private EnemySpawnController m_controller = null;
private Color m_selectedColor;
private static List<Color> m_idColors = null;
private static Dictionary<Color, Texture2D> m_colorSwatches = null;
#endregion
#region PROPERTIES
private EnemySpawnController Controller
{
get
{
if (m_controller == null)
{
m_controller = target as EnemySpawnController;
}
return m_controller;
}
}
private static List<Color> Colors
{
get
{
if (m_idColors == null)
{
m_idColors = EnemySpawnController.ColorTable.Keys.ToList();
}
return m_idColors;
}
}
public static Dictionary<Color, Texture2D> ColorSwatches
{
get
{
if (m_colorSwatches == null)
{
m_colorSwatches = new Dictionary<Color, Texture2D>();
foreach (Color color in Colors)
{
Texture2D texture = new Texture2D(TEXTURE_WIDTH, TEXTURE_HEIGHT);
for (int x = 0; x < texture.width; x++)
{
for (int y = 0; y < texture.height; y++)
{
texture.SetPixel(x, y, color);
}
}
texture.Apply();
m_colorSwatches.Add(color, texture);
}
}
return m_colorSwatches;
}
}
#endregion
#region METHODS
public override void OnInspectorGUI()
{
EditorGUILayout.TextField("Enemy Info", EditorStyles.centeredGreyMiniLabel);
if (GUILayout.Button("Select Color"))
{
GenericMenu menu = new GenericMenu();
foreach (Color color in Colors)
{
GUIContent content = new GUIContent(color.ToString(), ColorSwatches[color]);
menu.AddItem(content, m_selectedColor == color, OnColorSelected, color);
}
menu.ShowAsContext();
}
m_drawDefaultInspector = EditorGUILayout.Toggle("Draw Default Inspector:", m_drawDefaultInspector);
if (m_drawDefaultInspector)
{
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
EditorGUILayout.Space();
EditorGUILayout.LabelField("Default Inspector", EditorStyles.centeredGreyMiniLabel);
DrawDefaultInspector();
}
}
private void OnColorSelected(object color)
{
Controller.SetColor((Color)color);
}
#endregion
}
Comment
Your answer
Follow this Question
Related Questions
Popup to choose which child class I want in Custom Editor Inspector 0 Answers
How to edit array/list property with custom PropertyDrawer? 3 Answers
Creating an Inspector view that only changes on scene hierarchy selection or asset selection 1 Answer
Access an Array of class inside another Array of class? (EditorGUILayout) 1 Answer
How to make a dropdown in Unity Editor with list from another Monobehaviour Script 0 Answers