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);
}
}
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牆外的世界