Unity Editor knowledge point sorting (create custom Inspector 1)

Create the Editor folder in Unity. The custom viewer scripts must be placed in the Editor folder, and then create the Scripts folder. Generally, Mono scripts will be placed in this folder.
Create a Level script in the Scripts folder, declare a public variable totalTime
in the Editor folder, and create a Level Inspector script

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

namespace RunAndJump
{
    
    
    [CustomEditor(typeof(Level))]
    public class LevelInspector : Editor
    {
    
    
        private Level _myTarget;

        //在我们对象被选中的时候调用的,常常用于一些初始化的代码
        private void OnEnable()
        {
    
    
            Debug.Log("OnEnable");
            //为哪个对象写查看器 target就是哪个对象
            //target的类型 是Object,需要转换类型
            _myTarget = (Level)target;
        }

        //在对象超出我们操作范围内调用的
        //在对象被销毁的时候调用的
        //可以用来做一些代码退出前的清理工作(数组清零,变量重置)
        private void OnDisable()
        {
    
    
            Debug.Log("OnDisable");
        }

        //在对象被销毁的时候调用的
        private void OnDestroy()
        {
    
    
            Debug.Log("OnDestroy");
        }

        //绘制查看器GUI的回调函数
        //必须是public,为了使自定义的查看起生效 必须重载
        public override void OnInspectorGUI()
        {
    
    
            //查看器输出文本
            EditorGUILayout.LabelField("这个inspector的GUI被修改了");
            EditorGUILayout.LabelField("当前关卡时间:"+_myTarget.TotalTime);
        }
    }
}

As you can see, the viewer of the object that mounts the level script becomes as follows
insert image description here

Guess you like

Origin blog.csdn.net/qq_43388137/article/details/122219702