unity editor 编辑器插件制作基础:一、脚本组件inspector面板UI组件功能扩展

一 说明

  1. 常规脚本扩展在unity api中的Attribute分类中,
  2. 当前脚本只需要继承MonoBehaviour即可使用

二 Inspector面板ui组件的实现

2.1 需要引入的库
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
2.2 组件控制方法

【禁止在同一物体重复添加该脚本】

[DisallowMultipleComponent]
public class MapCreator : MonoBehaviour{
}

【允许通过菜单栏上的Component菜单为物体添加该脚本】

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

【 自动添加依赖组件(多个),并使其无法删除】

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

【允许同时编辑多个物体】

[CanEditMultipleObjects]
public class MapCreator : MonoBehaviour{
}

以上功能可以叠加使用

2.3 Inspector面板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");
    }
}

三 完整脚本

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");
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千年奇葩

从来没受过打赏,这玩意好吃吗?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值