当前位置:网站首页>Unity3D - modification of the Inspector panel of the custom class

Unity3D - modification of the Inspector panel of the custom class

2022-08-11 08:13:00 Solia Katong

Unity3D——自定义类的InspectorModification of the panel

  1. 预期目标: for custom classesInspector面板进行修改,达到如下效果
    自定义类的Inspector面板
  2. 步骤
    (1)Create your own script(例如Help.cs),置于Assets / Scripts目录(非必须)下
    (2)Create a script corresponding to it(HelpInspector.cs), 置于 Assets / Editor 目录(没有可自行创建)下
    (3)在SudentInspector.cs中
    在这里插入图片描述
  • 引用UnityEditor命名空间
  • 添加特性[CutomEditor(typeof(Help))]
  • 继承Editor类
  • 重写OnInspectorGUI类
  1. 注意:这里重写OnInspectorGUI方法时,The compiler comes with calls in the parent classOnInspectorGUI,Here if you don't want toHelpUnwanted in the classpublic变量出现在Inspector面板上,就将base.OnInspectorGUI()注释掉

  2. 代码

// SudentInspector.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEditor;

[CustomEditor(typeof(Help))]
public class HelpInspector : Editor
{
    
    public override void OnInspectorGUI()
    {
    
        Help myHelp = (Help)target;        // target就是Help Inspector的实例

        GUILayout.BeginVertical();
        GUILayout.Label("Help Here");
        GUILayout.Label("Version: 1.0.0");        
        GUILayout.EndVertical();

        GUILayout.BeginHorizontal();
        GUILayout.Label("Test Input", GUILayout.Width(60));
        myHelp.helpId = EditorGUILayout.IntField( myHelp.helpId,GUILayout.Width(150));
        GUILayout.EndHorizontal();

        GUILayout.BeginVertical();
        myHelp.helpName = EditorGUILayout.TextField(myHelp.helpName, GUILayout.Width(150));
        GUILayout.EndVertical();
    }
}





// Help.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Help : MonoBehaviour
{
    
    public bool helpHere;
    public int helpId;
    public string helpName;
}

功能
在这里插入图片描述

  1. 注意
  • 这里的target就是指Help创建出来的Help实例(面板),如果想要控制Help中的变量,No more new instances can be created,Because we are going to control the instance that has been created on the panel,也就是target
原网站

版权声明
本文为[Solia Katong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110803406921.html