Unityエディタエディタプラグイン制作基盤: 1.スクリプトコンポーネントインスペクタパネルUIコンポーネント機能拡張

メモ

  1. 一般的なスクリプト拡張機能は、Unity API の属性カテゴリにあります。
  2. 現在のスクリプトは MonoBehaviour を継承するだけで使用できます。

2 つのインスペクター パネル UI コンポーネントの実装

2.1 インポートが必要なライブラリ

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

2.2 コンポーネントの制御方法

[同じオブジェクトにスクリプトを繰り返し追加することは禁止されています]

[DisallowMultipleComponent]
public class MapCreator : MonoBehaviour{
    
    
}

[メニューバーのコンポーネントメニューからオブジェクトにスクリプトを追加できるようにします]

[AddComponentMenu("myMenu/test")]
public class MapCreator : MonoBehaviour{
    
    
}

【依存コンポーネント(複数)を自動追加し、削除不可能にする】

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class MapCreator : MonoBehaviour{
    
    
}

[複数のオブジェクトの同時編集を許可]

[CanEditMultipleObjects]
public class MapCreator : MonoBehaviour{
    
    
}

上記機能を重ね合わせて使用​​可能

2.3 インスペクターパネルの UI コンポーネントの描画

Mono を継承する通常のスクリプトは、関連する変数を直接定義して、関連する Inspector UI コンポーネントを呼び出すことができます。

public class MapCreator : MonoBehaviour
{
    
    
	///设置标题 Label
    [Header("Header text")]
    //鼠标停留在下面的变量上就显示提示
    [TooltipAttribute("鼠标停留显示提示")]
    // 使私有变量在inspector中可见,放在class 或struct前可以强制序列化
    [SerializeField]
    private int privateVariable;
    // 隐藏inspector中的共有变量
    [HideInInspector]
    public int publicVariable;
    // 多行输入文本
    [MultilineAttribute(1)]
    public string multilineText;
    // 多行输入文本
    [Multiline(5)]
    public string multiline;
    // 可以自动伸缩的,并且带有滚动条的多行输入框
    [TextArea(3, 5)]//最低3行,最高5行
    public string textArea;
    // 设置数值滑竿
    [RangeAttribute(0.1f, 3)]
    public float rangFloat = 0.5f;
    // 曲线组件
    public AnimationCurve curve;
    // 颜色拾取器
    public Color color1;
    // 贴图
    public Texture texture;
    //单选框
    public TargetType targetType;
    public enum TargetType
    {
    
    
        Type1,
        type2,
        type3
    }
    //可扩展的群组  与enum单选联合,曲线救国的实现了多选
    public List<TargetType> targetTypeList;
    // 在菜单栏中添加工具  路径名;是否需要点击,排序
    [MenuItem("MyTools/tool1", false, 1)]
    static void MyTools()
    {
    
    
        Debug.Log("click menue tools");
    }
    // 当当前脚本更新时,编辑器会运行该脚本
    [InitializeOnLoadMethodAttribute]
    static void OnAfterSceneLoadRuntimeMethod1()
    {
    
    
        Debug.Log("script uploaded");
    }
    // 在当前场景加载前/后 运行该方法    注意只能是静态方法
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
    static void OnAfterSceneLoadRuntimeMethod()
    {
    
    
        Debug.Log("After scene loaded");
    }
    [RuntimeInitializeOnLoadMethod]
    static void OnRuntimeMethodLoad()
    {
    
    
        Debug.Log("RuntimeMethodLoad: After scene loaded");
    }
    // 在inspector中当前脚本组件,添加右键菜单按钮,单击后即可执行非静态方法
    [ContextMenu("Do Something")]
    void DoSomething()
    {
    
    
        Debug.Log("Perform operation");
    }
    private void Update()
    {
    
    
        // 读取曲线
        for (int i = 0; i < curve.length; i++)
        {
    
    
            Debug.Log(curve[i].value);
        }
    }

    // 设置目标物体
    public GameObject originOBJ;

    // 处理方法
    public void BuildObject()
    {
    
    
        // 复制物体
        // Instantiate(originOBJ);
        Debug.Log("just kill it");
    }
}

3 つの完全なスクリプト

using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
// 禁止在同一物体重复添加该脚本
[DisallowMultipleComponent]
// 允许通过工具栏>Component>myMenu>test 为物体添加该脚本
// [AddComponentMenu("myMenu/test")]
//自动添加依赖组件,并且使这些组件无法删除
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class MapCreator : MonoBehaviour
{
    
    
    // 设置小标题 Lable
    [Header("Header text")]
    //鼠标停留在下面的变量上就显示提示
    [TooltipAttribute("鼠标停留显示提示")]
    // 使私有变量在inspector中可见,放在class 或struct前可以强制序列化
    [SerializeField]
    private int privateVariable;
    // 隐藏inspector中的共有变量
    [HideInInspector]
    public int publicVariable;
    // 多行输入文本
    [MultilineAttribute(1)]
    public string multilineText;
    // 多行输入文本
    [Multiline(5)]
    public string multiline;
    // 可以自动伸缩的,并且带有滚动条的多行输入框
    [TextArea(3, 5)]//最低3行,最高5行
    public string textArea;
    // 设置数值滑竿
    [RangeAttribute(0.1f, 3)]
    public float rangFloat = 0.5f;
    // 曲线组件
    public AnimationCurve curve;
    // 颜色拾取器
    public Color color1;
    // 贴图
    public Texture texture;

    //单选框
    public TargetType targetType;
    public enum TargetType
    {
    
    
        Type1,
        type2,
        type3
    }
    //可扩展的群组  与enum单选联合,曲线救国的实现了多选
    public List<TargetType> targetTypeList;
    // 在菜单栏中添加工具  路径名;是否需要点击,排序
    [MenuItem("MyTools/tool1", false, 1)]
    static void MyTools()
    {
    
    
        Debug.Log("click menue tools");
    }
    // 当当前脚本更新时,编辑器会运行该脚本
    [InitializeOnLoadMethodAttribute]
    static void OnAfterSceneLoadRuntimeMethod1()
    {
    
    
        Debug.Log("script uploaded");
    }
    // 在当前场景加载前/后 运行该方法    注意只能是静态方法
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
    static void OnAfterSceneLoadRuntimeMethod()
    {
    
    
        Debug.Log("After scene loaded");
    }
    [RuntimeInitializeOnLoadMethod]
    static void OnRuntimeMethodLoad()
    {
    
    
        Debug.Log("RuntimeMethodLoad: After scene loaded");
    }
    // 在inspector中当前脚本组件,添加右键菜单按钮,单击后即可执行非静态方法
    [ContextMenu("Do Something")]
    void DoSomething()
    {
    
    
        Debug.Log("Perform operation");
    }
    private void Update()
    {
    
    
        // 读取曲线
        for (int i = 0; i < curve.length; i++)
        {
    
    
            Debug.Log(curve[i].value);
        }
    }

    // 设置目标物体
    public GameObject originOBJ;

    // 处理方法
    public void BuildObject()
    {
    
    
        // 复制物体
        // Instantiate(originOBJ);
        Debug.Log("just kill it");
    }
}

おすすめ

転載: blog.csdn.net/lengyoumo/article/details/107555343