• 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 /
This question was closed Feb 14, 2021 at 06:30 AM by kaleidosgu for the following reason:

I have solved this question

avatar image
0
Question by kaleidosgu · Feb 14, 2021 at 01:56 AM · inspector

Is there a way to change a enum type in inspector

I want to change a enum to another enum in inspector.


This is my code. But I don't want to code one by one when I need add new enum type in my project.


For example: If I want to change tiletype to MainEnumTyp.MainEnum_3. Then I hanve to add it to the code.


Is there a good way add new type without one by one?

 public override void OnInspectorGUI()
 {
     TestEnum myScript = (TestEnum)target;
     myScript.tileType = (MainEnumTyp)EditorGUILayout.EnumPopup("MainType", myScript.tileType);
     if ( myScript.tileType == MainEnumTyp.MainEnum_1)
     {
         myScript.subTypeValue = (int)(SubEnumTyp01)EditorGUILayout.EnumPopup("SubType", (SubEnumTyp01)subTypeValueInt.intValue);
     }
     else if (myScript.tileType == MainEnumTyp.MainEnum_2)
     {
         myScript.subTypeValue = (int)(SubEnumTyp02)EditorGUILayout.EnumPopup("SubType", (SubEnumTyp02)subTypeValueInt.intValue);
     }
     serializedObject.ApplyModifiedProperties();
 }

alt text

alt text




I changed the api EnumPopup instead of IntPopup
I have to add code

 _buildEnum<SubEnumTyp02>(ref m_dic, MainEnumTyp.MainEnum_2);


But it just a little

here is my code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 using System;
 using System.Reflection;
 
 [CustomEditor(typeof(TestEnum))]
 [CanEditMultipleObjects]
 public class TestEnumEditor : Editor
 {
     SerializedProperty subTypeValueInt;
     private Dictionary<MainEnumTyp, Dictionary<int,string>> m_dic;
     private string[] m_arraySubEnumName;
     private int[] m_arraySubEnumValue;
     void OnEnable()
     {
         m_dic = new Dictionary<MainEnumTyp, Dictionary<int, string>>();
 
         _buildEnum<SubEnumTyp01>(ref m_dic, MainEnumTyp.MainEnum_1);
         _buildEnum<SubEnumTyp02>(ref m_dic, MainEnumTyp.MainEnum_2);
 
 
         subTypeValueInt = serializedObject.FindProperty("subTypeValue");
     }
     private void _SetSubTypInfo(MainEnumTyp _mainTyp, int nSubTyp)
     {
         if(m_dic.ContainsKey(_mainTyp) == true)
         {
             Dictionary<int, string> _dicSub = m_dic[_mainTyp];
             m_arraySubEnumName = new string[_dicSub.Count];
             m_arraySubEnumValue = new int[_dicSub.Count];
 
             foreach (KeyValuePair<int, string> entry in _dicSub)
             {
                 m_arraySubEnumName[entry.Key] = entry.Value;
                 m_arraySubEnumValue[entry.Key] = entry.Key;
             }
         }
     }
     public override void OnInspectorGUI()
     {
         TestEnum myScript = (TestEnum)target;
         myScript.tileType = (MainEnumTyp)EditorGUILayout.EnumPopup("MainType", myScript.tileType);
 
         _SetSubTypInfo(myScript.tileType, myScript.subTypeValue);
 
         myScript.subTypeValue = EditorGUILayout.IntPopup("Resize Scale: ", myScript.subTypeValue, m_arraySubEnumName, m_arraySubEnumValue);
 
         serializedObject.ApplyModifiedProperties();
     }
 
     private void _buildEnum<T>(ref Dictionary<MainEnumTyp, Dictionary<int, string>> _refDic, MainEnumTyp _mainTyp)
     {
         Dictionary<int, string> _DicEnum = new Dictionary<int, string>();
         string str = "";
         foreach (T suit in (T[])Enum.GetValues(typeof(T)))
         {
             str = Enum.GetName(typeof(T), suit);
             int nValue = Convert.ToInt32(suit);
             _DicEnum.Add(nValue,str);
         }
         _refDic.Add(_mainTyp, _DicEnum);
     }
 }






01.png (7.7 kB)
02.png (6.9 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

  • Sort: 

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

115 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

Related Questions

Block Placement Help 2 Answers

Assigning C# scripts to a JS variable through the inspector? 0 Answers

Rigidbody mass not updating in inspector after calling SetDensity 2 Answers

Enum become integer in the inspector 1 Answer

Call default inspector of non-inherited class 1 Answer


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