Unity一些基本属性
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()
-
Range()
当使用此属性时,Float或int将显示为检查器中的一个滑块,而不是默认的Number字段。 -
Multiline()
给string类型添加多行输入。 -
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()
- Tooltip()
为“检查器”窗口中的字段指定工具提示。 - 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墙外的世界