Unity Editor Extensions (Script Tags)

1. Commonly used script tags: adjust the data editing of script components and expand some function menus on the editor

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

[RequireComponent(typeof(AudioSource))] //给类添加。在给物体添加该标签标记的脚本类后,如果物体没有typeof中的组件则会自动给物体添加
[AddComponentMenu("Test/ComponentTest")] //给类添加。会出现在Component菜单中,点击后可以为物体添加对应的脚本类
[HelpURL("https://www.baidu.com")]//给类添加。将标记的脚本组件的右上角的小问号链接到给定的URL(那个小问号本身是链接到官方脚本文档的,相当于这里的标签修改了它的链接地址)
[DisallowMultipleComponent]//给类添加。标记的脚本只能对同一个物体添加一个该标记脚本,而不能添加多个该脚本
[ExecuteInEditMode]//给类添加。使脚本在编辑模式下也能执行生命周期函数,如Start、Update等。但是使用起来效果并不好。
public class Test : MonoBehaviour
{
    private void Start()
    {
        Debug.Log("Start");
    }
    [HideInInspector]//隐藏公有变量在Inspector面板上显示
    public int num;
    [NonSerialized]//序列化属性,并可以隐藏公有属性
    public int Nonnum;
    [SerializeField]//使非公有属性序列化,并显示在面板上
    private string playerName;
    public Student stu;
    [Header("文章标题")]//给变量加个标题
    public string title;
    [Space(30)]//设置上边距
    public int edgeDistance;
    [Tooltip("鼠标悬停在属性名上")]//鼠标悬停在属性名上显示的信息
    public int hover;
    [Range(0, 10)]//限制int、float、double类型变量的大小范围
    public int hitDamage;
    [Multiline(3)]//给string类型变量设置行数
    public string text;
    [TextArea]//给string类型变量设置成文本区域的形式显示
    public string area;
    [ColorUsage(true)]//调用颜色复选框,其中的true表示显示透明度设置
    //注:如果要调节3D物体的透明度,需要将3D物体的材质的渲染模式改为Fade
    public Color color;
    [FormerlySerializedAs("obj")]//防止GameObject类型变量在拖拽赋值后,因改变了变量名而使引用丢失的情况,里面的参数是最初拖拽赋值时的变量名
    public GameObject obj;
    [MenuItem("菜单/菜单项 &%Q", priority =1)]//给unity菜单栏加功能菜单,该标签下可以加静态方法,表示功能按钮调用的方法,&%Q表示快捷键,%:Ctrl #:Shift &:Alt。存在多个菜单项时priority可以控制菜单项的显示优先级
    static void ShowMenuItem()
    {
        Debug.Log("MenuItem");
    }
    [MenuItem("CONTEXT/Test/buttonl")]//Test表示脚本名,button1表示功能按钮名。在对应的脚本组件右上角的三个小点或齿轮的那里,点击button1,就会调用下面的静态方法
    static void ShowInpectorInfo()
    {
        Debug.Log("InpectorInfo");
    }
    [ContextMenuItem("Test", "MyTest")]//给属性添加右键菜单功能按钮,Test表示右键菜单的功能按钮名,MyTest表示该功能对应得触发方法
    public string printMyTest;
    void MyTest()
    {
        Debug.Log("MyTest");
    }
}
[System.Serializable]//使序列化的类中的公有变量可以在Inspector上显示
public class Student
{
    public int id;
    public string name;
    private int age;
}

2.CreateAssetMenu label: In the Create menu of the Project, you can see that the label has a function

(1) Write a script that inherits ScriptableObject, and add the CreateAssetMenu tag to the script class

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

[CreateAssetMenu(fileName = "ScriptFile", menuName = "玩家/玩家信息", order = 1)]//Project的Create菜单下可以看到,点击“玩家信息”会自动创建名为“ScriptFile”的文件。order表示显示顺序
public class ScriptObject : ScriptableObject
{
    //添加一些属性
    [Header("ID")]
    public int id=2;
    [Tooltip("Name")]
    public string playerName="张三";
    [Range(1, 90)]
    public int age=40;
    [Tooltip("PlayerHitDamage")]
    public float hitDamage=40;
    //.........常用标签基本都可以使用
    //添加一些非生命周期函数也行
    public void Attack()
    {
        Debug.Log("Attack");
    }
    public void Hurt()
    {
        Debug.Log("Hurt");
    }
    public void ShowInfo()
    {
        Debug.Log($"id:{id},playerName:{playerName},age:{age},hitDamage:{hitDamage}");
    }
}

(2) Method 1: Right-click the Resources folder in the Project window (not necessary, see how you refer to it), create an .asset player information file in the Create menu, and a ScriptFile file will be generated. (need to use CreateAssetMenu tag)

Method 2: ScriptObject files are created through the static methods of ScriptableObject. Although the method is different, there is not much difference in the use effect. (No need to use CreateAssetMenu tag)

(3) Mount the following script (both methods are used in the script), run unity, and view the information output by the console

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

public class ScriptableObjectTest : MonoBehaviour
{
    delegate void DelegateTest();//委托
    public ScriptObject objTest;//方法一:以拖拽复制的方式引用ScriptObjec
    // Start is called before the first frame update
    void Start()
    {
        //方法一:可在Inspector面板上实时调整,使用更方便,需要用到CreateAssetMenu标签
        DelegateTest test = new DelegateTest(objTest.ShowInfo);//注册方法
        test += objTest.Attack;//注册方法
        test += objTest.Hurt;
        test();
        //方法二:不需要用到CreateAssetMenu标签
        ScriptObject obj = ScriptableObject.CreateInstance<ScriptObject>();//创建ScriptObject实例
        AssetDatabase.CreateAsset(obj, "Assets/Resources/ScriptObject.asset"); //在Assets/Resources文件夹创建资源文件ScriptObject.asset
        AssetDatabase.SaveAssets();//保存创建的资源
        AssetDatabase.Refresh();//刷新
        DelegateTest test2 = new DelegateTest(obj.ShowInfo);//注册方法
        test2 += obj.Attack;//注册方法
        test2 += obj.Hurt;
        test2();
    }
}

 Note: Description of ScriptableObject script class: It is often necessary to create multiple identical prefabs in the project, and the same scripts are mounted on these prefabs, which means that data such as attributes and methods in the same script are copied multiple times. It causes a waste of resources, but use a script that inherits ScriptableObject to save data such as properties and methods that need to be reused, and then define a reference to the ScriptableObject script object on the script component of the prefab that needs to use these data to obtain Data can improve performance, especially when there are a lot of such data.
Usually, we input data from the variables exposed by the script component on the Inspector panel to change the project effect. In fact, when you input the data, the unity editor will automatically modify the data in the corresponding .asset scene file in real time to change the project effect. This is inconvenient when a large amount of data needs to be modified frequently, and unity will also be very stuck. Using ScriptableObject scripts can isolate some attributes or methods that need to be modified frequently from the main logic script, which is convenient for planning and modifying data parameters.

reference:

Unity editor extension - label attribute Attribute_mb60e5417d375b8's technical blog_51CTO blog

Unity common tags - CSDN Blog

Unity Advanced: ScriptableObject Usage Guide_YY-nb's Blog-CSDN Blog

Conclusion: The tree that embraces is born at the end of the hair; the nine-story platform starts from the pile of soil; the journey of a thousand miles begins with a single step.

Guess you like

Origin blog.csdn.net/falsedewuxin/article/details/130052271