Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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?


alt text

alt text


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
 }


step1.png (6.1 kB)
step2.png (16.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

168 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges