Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Selecting a custom class type from inspector

Discussion in 'Scripting' started by jlanisdev, Jul 2, 2017.

  1. jlanisdev

    jlanisdev

    Joined:
    Jan 18, 2016
    Posts:
    76
    Let's say I have some classes defined like this:
    Code (CSharp):
    1. public class ClassA: BaseClass { ... }
    2. public class ClassB: BaseClass { ... }
    3. public class ClassC: BaseClass { ... }
    Now I want to be able to select one of these classes from a drop down menu in the inspector, and have it stored as either a string (i.e, "ClassA"), or as System.Type. Is that possible? I know I can simply use serializable strings and convert them, but I was hoping to find a less error-prone approach.

    Edit:
    Looks like I found what I wanted here: https://bitbucket.org/rotorz/classtypereference-for-unity/src
     
    Last edited: Jul 2, 2017
  2. The_Dogg

    The_Dogg

    Joined:
    Apr 6, 2017
    Posts:
    4
    here is solution
    maybe it help you
     
  3. BrightRocket987

    BrightRocket987

    Joined:
    Jul 26, 2021
    Posts:
    7
    anyone still needing this you can create an enum for the dropdown, and serialize the enum or make it public:

    Code (CSharp):
    1. public class dropdownexample: MonoBehaviour
    2. {
    3. //serialize dropdown
    4.     public ActivationType activationType;
    5.  
    6.     private void Start()
    7.     {
    8.         if (activationType == ActivationType.Start)
    9.         {
    10.             ActivateFunction();
    11.         }
    12.     }
    13.  
    14.     private void Awake()
    15.     {
    16.         if (activationType == ActivationType.Awake)
    17.         {
    18.             ActivateFunction();
    19.         }
    20.     }
    21.  
    22.     private void Update()
    23.     {
    24.         if (activationType == ActivationType.Update)
    25.         {
    26.             ActivateFunction();
    27.         }
    28.     }
    29.  
    30.     // Other methods and functionality
    31.  
    32.     private void ActivateFunction()
    33.     {
    34.         Debug.Log("Function activated!");
    35.     }
    36.  
    37. //dropdown emun
    38.     public enum ActivationType
    39.     {
    40.         None,
    41.         Start,
    42.         Awake,
    43.         Update
    44.     }
    45. }
    46.  
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,869
    Glad that works for you. Please don't raise ancient threads like this. Nobody was continuing to ask.
     
    Kurt-Dekker likes this.