Showing posts with label Editor-Scripting. Show all posts
Showing posts with label Editor-Scripting. Show all posts

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;
        }
    }
}

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;
    }
}

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);
    }
}

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


MenuItems_Priority_Editor.cs
using UnityEngine;
using UnityEditor;

public class MenuItems_Priority_Editor
{
    [MenuItem("Priority Menu/Option 1", priority = 1)]
    public static void Option1Action()
    { }

    [MenuItem("Priority Menu/Option 2", priority = 101)]
    public static void Option2Action()
    { }

    [MenuItem("Priority Menu/Option 3", priority = 51)]
    public static void Option3Action()
    { }

    [MenuItem("Priority Menu/Option 4", priority = 2)]
    public static void Option4Action()
    { }
}

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


MenuItems_Hotkeys_Editor.cs
using UnityEngine;
using UnityEditor;

public class MenuItems_Hotkeys_Editor
{
    //Hotkey Shift-Ctrl-Alt A
    [MenuItem("Hotkeys Menu/Create Cube #%&a")]
    public static void CreateCube()
    {
        GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    }

    //Hotkey A
    [MenuItem("Hotkeys Menu/Option 2 _a")]
    public static void Option2Action()
    { }

    //Hotkey Left
    [MenuItem("Hotkeys Menu/Option 3 _Left")]
    public static void Option3Action()
    { }

    //Hotkey F1
    [MenuItem("Hotkeys Menu/Option 4 _F1")]
    public static void Option4Action()
    { }

    //Hotkey Home
    [MenuItem("Hotkeys Menu/Option 5 _Home")]
    public static void Option5Action()
    { }
}

Creating Menu Items | Editor Scripting | C# | Unity Game Engine


MenuItems_Editor.cs
using UnityEngine;
using UnityEditor;

public class MenuItems_Editor
{
    [MenuItem("Control Menu/Play")]
    public static void Play()
    {
        EditorApplication.EnterPlaymode();
    }

    [MenuItem("Control Menu/Stop")]
    public static void Stop()
    {
        EditorApplication.ExitPlaymode();
    }

    [MenuItem("Assets/Custom/Clear PlayerPrefs")]
    public static void DeleteAllPlayerPrefs()
    {
        PlayerPrefs.DeleteAll();
        EditorUtility.DisplayDialog("Cleared", "PlayerPrefs Cleared", "OK");
    }

    [MenuItem("Assets/Create/Cube at Center")]
    public static void CreateCenterCube()
    {
        GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.transform.position = Vector3.zero;
    }
}

Adding Button in Custom Inspector | Editor Scripting | C# | Unity Game Engine


PlayerScript.cs
using UnityEngine;

public class PlayerScript : MonoBehaviour
{
    public int health = 100;
    public int maxHealth = 100;
}

PlayerScriptEditor.cs
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(PlayerScript))]
public class PlayerScriptEditor : Editor
{
    public override void OnInspectorGUI()
    {
        PlayerScript playerScript = (PlayerScript)target;
        playerScript.health = EditorGUILayout.IntField("Health", playerScript.health);
        playerScript.maxHealth = EditorGUILayout.IntField("Max-Health", playerScript.maxHealth);

        if(GUILayout.Button("Reset Values"))
        {
            playerScript.health = 100;
            playerScript.maxHealth = 100;
        }
    }
}

Use of "HelpBox" in Custom Inspector | Editor Scripting | C# | Unity Game Engine


PlayerScript.cs
using UnityEngine;

public class PlayerScript : MonoBehaviour
{
    public int health = 100;
    public int maxHealth = 100;
}

PlayerScriptEditor.cs
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(PlayerScript))]
public class PlayerScriptEditor : Editor
{
    public override void OnInspectorGUI()
    {
        PlayerScript playerScript = (PlayerScript)target;
        playerScript.health = EditorGUILayout.IntField("Health", playerScript.health);
        playerScript.maxHealth = EditorGUILayout.IntField("Max-Health", playerScript.maxHealth);

        if (playerScript.maxHealth > 0)
        {
            if(playerScript.maxHealth < playerScript.health)
                EditorGUILayout.HelpBox("Max Health is smaller than Health", MessageType.Warning);
        }
        else
        {
            EditorGUILayout.HelpBox("Max Health should be greater than 0", MessageType.Error);
        }
    }
}

Use of "DrawDefaultInspector" method in Custom Inspector | Editor Scripting | C# | Unity Game Engine


PlayerScript.cs
using UnityEngine;

public class PlayerScript : MonoBehaviour
{
    public int health = 100;
    public int maxHealth = 100;
}

PlayerScriptEditor.cs
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(PlayerScript))]
public class PlayerScriptEditor : Editor
{
    public override void OnInspectorGUI()
    {
        PlayerScript playerScript = (PlayerScript)target;
        DrawDefaultInspector();

        if (playerScript.maxHealth > 0)
            EditorGUILayout.LabelField("Health %", playerScript.health * 100 / playerScript.maxHealth + "%");
    }
}

Building a Custom Inspector | Editor Scripting | C# | Unity Game Engine


PlayerScript.cs
using UnityEngine;

public class PlayerScript : MonoBehaviour
{
    public int health = 100;
    public int maxHealth = 100;
}

PlayerScriptEditor.cs
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(PlayerScript))]
public class PlayerScriptEditor : Editor
{
    public override void OnInspectorGUI()
    {
        PlayerScript playerScript = (PlayerScript)target;
        playerScript.health = EditorGUILayout.IntField("Health", playerScript.health);
        playerScript.maxHealth = EditorGUILayout.IntField("Max-Health", playerScript.maxHealth);

        EditorGUILayout.Space();
        EditorGUILayout.BeginHorizontal("box");
        if (playerScript.maxHealth > 0)
        {
            EditorGUILayout.LabelField("Health %", playerScript.health * 100 / playerScript.maxHealth + "%");
        }
        EditorGUILayout.EndHorizontal();
    }
}