📜  unity editor dropdown - C# (1)

📅  最后修改于: 2023-12-03 15:35:29.462000             🧑  作者: Mango

Unity Editor Dropdown - C#

Unity Editor Dropdown is a user interface widget that allows the user to select one option from a list of available options. This widget is commonly used in Unity Editor to create custom editor windows, where the user can choose from a list of options to adjust different settings.

How to Create a Dropdown in Unity Editor - C#

Creating a dropdown in Unity Editor using C# is a simple process. Here is an example code snippet that demonstrates how to create a dropdown in Unity Editor using C#:

using UnityEngine;
using UnityEditor;

public class MyWindow : EditorWindow
{
    string[] options = new string[] { "Option 1", "Option 2", "Option 3" };
    int selectedIndex = 0;

    [MenuItem("Window/My Window")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow(typeof(MyWindow));
    }

    void OnGUI()
    {
        selectedIndex = EditorGUILayout.Popup("Select an option:", selectedIndex, options);

        switch (selectedIndex)
        {
            case 0:
                Debug.Log("Option 1 was selected.");
                break;
            case 1:
                Debug.Log("Option 2 was selected.");
                break;
            case 2:
                Debug.Log("Option 3 was selected.");
                break;
        }
    }
}

In this example, we create an Editor Window called "My Window" that contains a dropdown with three options. The options array contains the list of available options, and the selectedIndex variable is used to keep track of which option was selected.

The EditorGUILayout.Popup method is called to create the dropdown in the window, and it takes three parameters: the label for the dropdown, the current selectedIndex, and the array of available options.

Finally, we use a switch statement to perform different actions depending on which option was selected.

Conclusion

Unity Editor Dropdown is a powerful tool that allows programmers to create custom editor windows with ease. Using the example code snippet provided, you can create your own dropdowns in Unity Editor using C# and customize them to fit your needs.