Display Dialog | Popup Message with Options | C# | Unity Game Engine


PlayDisplayDialog.cs
using UnityEditor;

public class PlayDisplayDialog
{
    [MenuItem("Custom Menu/Play")]
    public static void Play()
    {
        bool playOutput = EditorUtility.DisplayDialog("Play", "Are you sure you want to enter play mode?", "Yes", "No");
        if (playOutput)
            EditorApplication.EnterPlaymode();
        else
            EditorUtility.DisplayDialog("Canceled", "You canceled the play mode.", "OK");
    }
}

Create Your Own Editor Window | Editor Scripting | C# | Unity Game Engine


TestWindow.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class TestWindow : EditorWindow
{
    string myString = "Hi";
    float myFloat = 0f;
    bool myBool = false;
    bool isGroupEnabled = false;

    [MenuItem("Custom Window/Test Window")]
    public static void OpenWindow()
    {
        EditorWindow.GetWindow(typeof(TestWindow));
    }

    void OnGUI()
    {
        GUILayout.Label("Header", EditorStyles.boldLabel);
        myString = EditorGUILayout.TextField("Text", myString);

        isGroupEnabled = EditorGUILayout.BeginToggleGroup("Toggle Settings", isGroupEnabled);
        myFloat = EditorGUILayout.Slider("Slider", myFloat, -10, 10);
        myBool = EditorGUILayout.Toggle("Toggle", myBool);
        EditorGUILayout.EndToggleGroup();

        if(GUILayout.Button("Reset"))
        {
            myString = "Hi";
            myFloat = 0f;
            myBool = false;
            isGroupEnabled = false;
        }
    }
}

Sorting an Array using "Insertion Sort" | C# | Unity Game Engine


Solution.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Solution : MonoBehaviour
{
    public int[] numbers = { 78, 55, 45, 98, 13 };

    void Start()
    {
        InsertionSort(numbers);
        Debug.Log("Sorted numbers : " + string.Join(',', numbers));
    }

    public void InsertionSort(int[] nums)
    {
        for (int i = 1; i < nums.Length; i++)
        {
            int temp = nums[i];
            int j;
            for (j = i - 1; j >= 0 && nums[j] > temp; j--)
            {
                nums[j + 1] = nums[j];
            }
            nums[j + 1] = temp;
        }
    }
}

Menu Items - Component Context | Editor Scripting | C# | Unity Game Engine


MenuItems_ComponentContext_Editor.cs
using UnityEngine;
using UnityEditor;

public class MenuItems_ComponentContext_Editor
{
    [MenuItem("CONTEXT/Camera/Set Orthographic")]
    public static void SetOrthographic()
    {
        Camera cam = Selection.activeGameObject.GetComponent<Camera>();
        cam.orthographic = true;
    }

    [MenuItem("CONTEXT/Camera/Set Perspective")]
    public static void SetPerspective(MenuCommand cmd)
    {
        Camera cam = cmd.context as Camera;
        cam.orthographic = false;
    }
}

Sorting an Array using "Bubble Sort" | C# | Unity Game Engine


Solution.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Solution : MonoBehaviour
{
    public int[] numbers = { 78, 55, 45, 98, 13 };

    void Start()
    {
        BubbleSort(numbers);
        Debug.Log("Sorted numbers : " + string.Join(',', numbers));
    }

    public void BubbleSort(int[] nums)
    {
        for (int i = nums.Length - 2; i >= 0; i--)
        {
            for (int j = 0; j <= i; j++)
            {
                if (nums[j] > nums[j + 1])
                {
                    int temp = nums[j + 1];
                    nums[j + 1] = nums[j];
                    nums[j] = temp;
                }
            }
        }
    }
}

Menu Items - Conditional | Editor Scripting | C# | Unity Game Engine


MenuItems_Conditional_Editor.cs
using UnityEngine;
using UnityEditor;
public class MenuItems_Conditional_Editor
{
    [MenuItem("Conditional Menu/Process Material")]
    public static void DoSomethingWithMaterial()
    { }

    //Use the same name in MenuItem and pass 'true' to the second argument to mark the method as Validator.
    //The return type of method is bool and bool value depends on the type of selected object.
    //The menu item will be enabled when the selected object is a material otherwise disabled.
    [MenuItem("Conditional Menu/Process Material", true)]
    public static bool ProcessMaterialValidation()
    {
        return Selection.activeObject.GetType() == typeof(Material);
    }
}