Unity small utility six-String dropdown in Unity editor (String Dropdown)

Unity small utility six-String dropdown in Unity editor (String Dropdown)

In Unity, the enum type is converted to a drop-down box for selection by default. But in normal use, sometimes it is still very inconvenient, I want a string as a choice?

Look for the official method of Unity , if you don't find it, you can customize it yourself.

Step 1: We need to define our own StringInList property. Then add it to the Inspector for editing. The file name is: StringInListDrawer.cs The code is as follows:

using System;

using UnityEngine;

#if UNITY_EDITOR

using UnityEditor;

#endif



public class StringInList : PropertyAttribute

{

    public delegate string[] GetStringList();



    public StringInList(params string[] list)

    {

        List = list;

    }



    public StringInList(Type type, string methodName)

    {

        var method = type.GetMethod(methodName);

        if (method != null)

        {

            List = method.Invoke(null, null) as string[];

        }

        else

        {

            Debug.LogError("NO SUCH METHOD " + methodName + " FOR " + type);

        }

    }



    public string[] List

    {

        get;

        private set;

    }

}



#if UNITY_EDITOR

[CustomPropertyDrawer(typeof(StringInList))]

public class StringInListDrawer : PropertyDrawer

{

    // Draw the property inside the given rect

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)

    {

        var stringInList = attribute as StringInList;

        var list = stringInList.List;

        if (property.propertyType == SerializedPropertyType.String)

        {

            int index = Mathf.Max(0, Array.IndexOf(list, property.stringValue));

            index = EditorGUI.Popup(position, property.displayName, index, list);



            property.stringValue = list[index];

        }

        else if (property.propertyType == SerializedPropertyType.Integer)

        {

            property.intValue = EditorGUI.Popup(position, property.displayName, property.intValue, list);

        }

        else

        {

            base.OnGUI(position, property, label);

        }

    }

}

#endif

Step 2: We need to support custom types and initialize all the values ​​of the select box. This file, you can customize your own methods. I give an example, the file name is: PropertyDrawersHelper.cs. code show as below: 

using System.Collections.Generic;

using UnityEditor;

using UnityEngine;



public static class PropertyDrawersHelper

{

#if UNITY_EDITOR



    public static string[] AllSceneNames()

    {

        var temp = new List<string>();

        foreach (UnityEditor.EditorBuildSettingsScene S in UnityEditor.EditorBuildSettings.scenes)

        {

            if (S.enabled)

            {

                string name = S.path.Substring(S.path.LastIndexOf('/') + 1);

                name = name.Substring(0, name.Length - 6);

                temp.Add(name);

            }

        }

        return temp.ToArray();

    }



#endif

}

Finally, start to demonstrate this function. For example, the code is as follows:

public class MyBehavior : MonoBehaviour

{

    // This will store the string value

    [StringInList("Cat", "Dog")] public string Animal;

    // This will store the index of the array value

    [StringInList("John", "Jack", "Jim")] public int PersonID;



    // Showing a list of loaded scenes

    [StringInList(typeof(PropertyDrawersHelper), "AllSceneNames")] public string SceneName;

}

The result is as follows:

 

This satisfies my needs very well. The string drop-down box can meet my requirements better than using enum directly.

Guess you like

Origin blog.csdn.net/grace_yi/article/details/117705663