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 ShivekGG · Nov 13, 2016 at 10:45 AM · editorinspectoroopdesign-patternsenumerate

Custom enumeration class accessible through inspector?

Hi! I am working on my custom enumeration class, I think it looks beautiful and most of all in my opinion it's a good OOP solution. Any ideas how could I make its values (sword, exampleValue, troll) public CustomEnumeration dropDownList; accessible through inspector?

 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 
 //Example
 public class CustomEnumeration
 {
     //Attribute
     public readonly Vector3 Position;
     public readonly string Word;
 
     //Easy foreach imitation
     protected static readonly List<CustomEnumeration> _enumeration = new List<CustomEnumeration>();
     public static ReadOnlyCollection<CustomEnumeration> Enumeration
     {
         get
         {
             return _enumeration.AsReadOnly();
         }
     }
 
     //"Enums"
     public static readonly CustomEnumeration sword = new CustomEnumeration(new Vector3(0,0,0), "Sw0rd");
     public static readonly CustomEnumeration exampleValue = new CustomEnumeration(new Vector3(1, 2, 3), "example");
     public static readonly CustomEnumeration troll = new CustomEnumeration(new Vector3(3, 2, 1), "troll");
 
     //PROTECTED constructor - only here (in this script) we can create enum values
     protected CustomEnumeration(Vector3 position, string word)
     {
         this.Position = position;
         this.Word = word;
         _enumeration.Add(this);
     }
 
     public void DoSth()
     {
         //Instead of switch statements we can expand our enumeration class by creating methods
         //So OOP
         //Wow
         //Such CleanCode
         //Doge is dead meme like Harambe
     }
 }
Comment
Add comment · Show 1
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
avatar image mwnDK1402 · Nov 13, 2016 at 06:04 PM 0
Share

I'd like you to be more specific in your question, but I'll try to help you anyway.

I'm not sure what you mean by "accessible" since the values you are talking about are static and read-only, but you could write a PropertyDrawer for your class: https://docs.unity3d.com/$$anonymous$$anual/editor-PropertyDrawers.html

With a PropertyDrawer, you could show the values of the CustomEnumerator when it is shown in the inspector of another script.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LucianoMacaDonati · Nov 13, 2016 at 07:17 PM

Hello ShivekGG,

I believe you want to use something like SerializeField.

Example:

 public enum MyWeapons
 {
     Knives, Rope, Dagger, Chains, Rocks, Laser_beams, Acid, Body_bag
 }
 
 [SerializeField]
 public MyWeapons weaponEnum;


If you're making your own custom classes you might want to take a look at this too.

Comment
Add comment · Show 1 · Share
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
avatar image LucianoMacaDonati · Nov 13, 2016 at 07:20 PM 0
Share

I took a second look at your code and you will want to add [System.Serializable] at the top of your class. Look at the last link I showed you.

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

92 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

Related Questions

How can I detect when the name of a gameobject has been changed in the editor and do something when that happens? 2 Answers

How to create a drop down menu in editor inspector 0 Answers

Expose protected variable in inspector 1 Answer

,Unity Custom Inspector CreateInspectorGUI redraw on change 1 Answer

The variables modified in the inspector are not kept on build 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