Unity---Inspector面板自定義


一. 參數自定義

一個含有成員的類Player

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

    public int id;

    public string playerName;
    public string backStory;
    public float health;
    public float damage;

    public float weaponDamage1, weaponDamage2;

    public string shoeName;
    public int shoeSize;
    public string shoeType;

    void Start()
    {
        health = 50;
    }
}

寫完之后,inspector面板上是這樣的:

然后,寫一個編輯擴展腳本(寫出該腳本即可,不需要做任何操作):

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

//CustomEditor(typeof()) 用於關聯你要自定義的腳本
[CustomEditor(typeof(Player))]
//必須要讓該類繼承自Editor,且不需要導入UnityEditor程序集
public class PlayerInspector : Editor
{

    Player player;
    bool showWeapons;

    void OnEnable()
    {
        //獲取當前編輯自定義Inspector的對象
        player = (Player)target;
    }

    //執行這一個函數來一個自定義檢視面板
    public override void OnInspectorGUI()
    {
        //設置整個界面是以垂直方向來布局
        EditorGUILayout.BeginVertical();

        //空兩行
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        //繪制palyer的基本信息
        EditorGUILayout.LabelField("Base Info");
        player.id = EditorGUILayout.IntField("Player ID", player.id);
        player.playerName = EditorGUILayout.TextField("PlayerName", player.playerName);

        //空三行
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        //繪制Player的背景故事
        EditorGUILayout.LabelField("Back Story");
        player.backStory = EditorGUILayout.TextArea(player.backStory, GUILayout.MinHeight(100));

        //空三行
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        //使用滑塊繪制 Player 生命值
        player.health = EditorGUILayout.Slider("Health", player.health, 0, 100);

        //根據生命值設置生命條的背景顏色
        if (player.health < 20)
        {
            GUI.color = Color.red;
        }
        else if (player.health > 80)
        {
            GUI.color = Color.green;
        }
        else
        {
            GUI.color = Color.gray;
        }

        //指定生命值的寬高
        Rect progressRect = GUILayoutUtility.GetRect(50, 50);

        //繪制生命條
        EditorGUI.ProgressBar(progressRect, player.health / 100.0f, "Health");

        //用此處理,以防上面的顏色變化會影響到下面的顏色變化
        GUI.color = Color.white;

        //空三行
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        //使用滑塊繪制傷害值
        player.damage = EditorGUILayout.Slider("Damage", player.damage, 0, 20);

        //根據傷害值的大小設置顯示的類型和提示語
        if (player.damage < 10)
        {
            EditorGUILayout.HelpBox("傷害太低了吧!!", MessageType.Error);
        }
        else if (player.damage > 15)
        {
            EditorGUILayout.HelpBox("傷害有點高啊!!", MessageType.Warning);
        }
        else
        {
            EditorGUILayout.HelpBox("傷害適中!!", MessageType.Info);
        }

        //空三行
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        //設置內容折疊
        showWeapons = EditorGUILayout.Foldout(showWeapons, "Weapons");
        if (showWeapons)
        {
            player.weaponDamage1 = EditorGUILayout.FloatField("Weapon 1 Damage", player.weaponDamage1);
            player.weaponDamage2 = EditorGUILayout.FloatField("Weapon 2 Damage", player.weaponDamage2);
        }

        //空三行
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        //繪制鞋子信息
        EditorGUILayout.LabelField("Shoe");
        //以水平方向繪制
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Name", GUILayout.MaxWidth(50));
        player.shoeName = EditorGUILayout.TextField(player.shoeName);
        EditorGUILayout.LabelField("Size", GUILayout.MaxWidth(50));
        player.shoeSize = EditorGUILayout.IntField(player.shoeSize);
        EditorGUILayout.LabelField("Type", GUILayout.MaxWidth(50));
        player.shoeType = EditorGUILayout.TextField(player.shoeType);
        EditorGUILayout.EndHorizontal(); 
        EditorGUILayout.EndVertical();
    }

}

寫完之后inspector面板上是這樣的

 

通過自定義Inspector視圖可以實現很多方便的功能。例如將腳本組件的static變量或者私有變量顯示出來,或者實現多參數的聯動變化,即修改其中的某個參數,其余參數會隨之調整。

內容轉載自https://www.jianshu.com/p/2f686da4905c

二. 編輯時運行

代碼:

[ExecuteInEditMode]
public class Test : MonoBehaviour
{

    // Update is called once per frame
    void Update()
    {
        transform.LookAt(Vector3.zero);
    }
}

ExecuteInEditMode表示程序的Update在編輯狀態下運行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM