Unity3D Editor(一)

1.AddComponentMenu()

AddComponentMenu属性允许您将脚本放置在“Component”菜单中的任何位置,而不仅仅是“Component->Script”菜单。

您可以使用此方法更好地组织组件菜单,从而在添加脚本时改进工作流。重要注意事项:您需要重新启动。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("MyMenu/FirstMenu")]
public class MyScript_01 : MonoBehaviour {


}

在这里插入图片描述

2.RequireComponent()

RequireComponent属性会自动添加必需的组件作为依赖项。
如果已经存在则不再重复添加,并且不能移除

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("MyMenu/FirstMenu")]
[RequireComponent(typeof(Rigidbody))]
public class MyScript_01 : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

在这里插入图片描述

3.ContextMenu()

ContextMenu属性允许向上下文菜单添加命令到这个组件上。
你可以通过右键或者点击设置图标来调用(一般用于函数,)且是在非运行状态下执行该函数。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("MyMenu/FirstMenu")]
[RequireComponent(typeof(Rigidbody))]
public class MyScript_01 : MonoBehaviour {

    public string name;
    public int age;
    [ContextMenu("OutputInfo")]
    void OutputInfo()
    {
        Debug.Log(name + " / " + age);
    }
}

在这里插入图片描述

4.HelpURL()

HelpURL()提供一个自定义的文档链接2,点击组件上的文档图标,就能打开到你指定的链接

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[HelpURL("https://blog.csdn.net/qq_42577542")]
[AddComponentMenu("MyMenu/FirstMenu")]
[RequireComponent(typeof(Rigidbody))]
public class MyScript_01 : MonoBehaviour {

    public string name;
    public int age;
    [ContextMenu("OutputInfo")]
    void OutputInfo()
    {
        Debug.Log(name + " / " + age);
    }
}

在这里插入图片描述

扫描二维码关注公众号,回复: 10187127 查看本文章

5.Range() | Multiline() | header()

  1. Range()
    当使用此属性时,Float或int将显示为检查器中的一个滑块,而不是默认的Number字段。

  2. Multiline()
    给string类型添加多行输入。

  3. Header()
    添加属性的标题

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[HelpURL("https://blog.csdn.net/qq_42577542")]
[AddComponentMenu("MyMenu/FirstMenu")]
[RequireComponent(typeof(Rigidbody))]
public class MyScript_01 : MonoBehaviour {

    [Header("MyTool")]
    [Multiline(5)]
    public string name;
    [Range(0,10)]
    public int age;
    [ContextMenu("OutputInfo")]
    void OutputInfo()
    {
        Debug.Log(name + " / " + age);
    }
}

在这里插入图片描述
在这里插入图片描述

6.Tooltip() | Space()

  1. Tooltip()
    为“检查器”窗口中的字段指定工具提示。
  2. Space()
    用于为在Inspector()面板两属性之间添加指定的距离
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[HelpURL("https://blog.csdn.net/qq_42577542")]
[AddComponentMenu("MyMenu/FirstMenu")]
[RequireComponent(typeof(Rigidbody))]
public class MyScript_01 : MonoBehaviour {

    [Header("MyTool")]
    [Multiline(5)]
    public string name;
    [Range(0,10)]
    public int age;
    [Space(100)]
    [Tooltip("提示:用于设置性别")]
    public string sex;
    [ContextMenu("OutputInfo")]
    void OutputInfo()
    {
        Debug.Log(name + " / " + age + " / " + sex);
    }
}

在这里插入图片描述

资料来源
Unity3D API:
https://docs.unity3d.com/ScriptReference/index.html
微信公众号:
Unity墙外的世界

发布了55 篇原创文章 · 获赞 47 · 访问量 3511

猜你喜欢

转载自blog.csdn.net/qq_42577542/article/details/105004251