廣州網(wǎng)站建設(shè)公司興田德潤怎么樣搜收錄網(wǎng)
本文只提及常用的特性,更多特性請查看官方文檔。
AddComponentMenu - Unity 腳本 API
常用特性
AddComponentMenu 添加組件菜單
使用 AddComponentMenu 屬性可在“Component”菜單中的任意位置放置腳本,而不僅是“Component > Scripts”菜單。
使用此屬性可以更好地組織 Component 菜單,從而改進(jìn)添加腳本時(shí)的工作流程。
[AddComponentMenu("Transform/Follow Transform")]
public?class?FollowTransform?: MonoBehaviour
{
}
ContextMenu?向上下文菜單添加命令
ContextMenu 屬性用于向上下文菜單添加命令。
在該附加腳本的 Inspector 中,當(dāng)用戶選擇該上下文菜單時(shí), 將執(zhí)行此函數(shù)。
這對于從該腳本自動設(shè)置場景數(shù)據(jù)非常有用。 此函數(shù)必須是非靜態(tài)的。
public class AttributeTest : MonoBehaviour
{[ContextMenu("Do Something")]void DoSomething(){Debug.Log("Perform operation");}
}
ContextMenuItemAttribute
當(dāng)點(diǎn)擊Reset后,會調(diào)用ResetDesc方法,重置變量的值。即往已經(jīng)存在的上下文菜單中,添加自己的方法。
[ContextMenuItem("Reset", "ResetDesc")]public string playerDesc = "";public int money = 0;void ResetDesc(){playerDesc = "";money = 0;}
CreateAssetMenuAttribute 創(chuàng)建資源菜單
對 ScriptableObject 派生類型進(jìn)行標(biāo)記,使其自動列在 Assets/Create 子菜單中,以便能夠輕松創(chuàng)建該類型的實(shí)例并將其作為“.asset”文件存儲在項(xiàng)目中。
[CreateAssetMenu(fileName = "ScriptableObjectTest.asset", menuName = "ScriptableObject/ScriptableObjectTest")]
public class ScriptableObjectTest : ScriptableObject
{public string desc = "這是個ScriptableObject";
}
HeaderAttribute 添加標(biāo)題
使用該?PropertyAttribute?在 Inspector 中的某些字段上方添加標(biāo)題。
標(biāo)題使用?DecoratorDrawer?完成。
[Header("Health Settings")]public int health = 0;
HideInInspector 隱藏于面板
使變量不顯示在 Inspector 中,但進(jìn)行序列化。public變量默認(rèn)會顯示在Inspector界面中的,加上這個標(biāo)簽,就不會顯示。
InspectorNameAttribute 修改面板顯示的名稱
對枚舉值聲明使用此屬性可更改 Inspector 中顯示的名稱。
public class AttributeTest : MonoBehaviour
{public ModelImporterIndexFormat f;
}public enum ModelImporterIndexFormat
{Auto = 0,[InspectorName("16 bits")]UInt16 = 1,[InspectorName("32 bits")]UInt32 = 2,
}
MinAttribute 最小值限制
用于使腳本中的 float 或 int 變量受限于特定最小值的屬性。
MultilineAttribute 多行文本
用于string類型,可以編輯多行文本。
PropertyAttribute 自定義屬性特性
用于派生自定義屬性特性的基類。這可用于為腳本變量創(chuàng)建特性。
自定義特性可以與自定義?PropertyDrawer?類連接,以控制具有該特性的腳本變量如何在 Inspector 中顯示。
RangeAttribute 值的范圍限制
用于使腳本中的 float 或 int 變量受限于特定范圍的屬性。
使用此屬性時(shí),float 或 int 會在 Inspector 中顯示滑動條。
[Range(1, 6)]public int integerRange;[Range(0.2f, 0.8f)]public float floatRange;
RequireComponent 所需組件
RequireComponent 屬性自動將所需的組件添加為依賴項(xiàng)。
當(dāng)您將使用RequireComponent的腳本添加到GameObject時(shí),所需組件將自動添加到GameObject。這有助于避免設(shè)置錯誤。例如,一個腳本可能要求一個剛體總是被添加到同一個GameObject中。
// PlayerScript requires the GameObject to have a Rigidbody component
[RequireComponent(typeof(Rigidbody))]
public class PlayerScript : MonoBehaviour
{Rigidbody rb;void Start(){rb = GetComponent<Rigidbody>();}void FixedUpdate(){rb.AddForce(Vector3.up);}
}
SerializeField 序列化私有字段
強(qiáng)制 Unity 對私有字段進(jìn)行序列化。
當(dāng) Unity 對腳本進(jìn)行序列化時(shí),僅對公共字段進(jìn)行序列化。 如果還需要 Unity 對私有字段進(jìn)行序列化, 可以將 SerializeField 屬性添加到這些字段。
Unity 將對所有腳本組件進(jìn)行序列化,重新加載新程序集, 并從序列化的版本重新創(chuàng)建腳本組件。此 序列化是通過 Unity 內(nèi)部序列化系統(tǒng)完成的;而不是通過 .NET 的序列化功能來完成。
序列化系統(tǒng)可執(zhí)行以下操作: 可序列化(可序列化類型的)公共非靜態(tài)字段 可序列化標(biāo)記有?SerializeField?屬性的非公共非靜態(tài)字段。 不能序列化靜態(tài)字段。 不能序列化屬性。?可序列化的類型
Unity 可序列化以下類型的字段: 繼承 UnityEngine.Object 的所有類,例如 GameObject、Component、MonoBehaviour、Texture2D、AnimationClip。 所有基本數(shù)據(jù)類型,例如 int、string、float、bool。 某些內(nèi)置類型,例如 Vector2、Vector3、Vector4、Quaternion、Matrix4x4、Color、Rect、LayerMask。 可序列化類型數(shù)組 可序列化類型列表 枚舉 結(jié)構(gòu) 有關(guān)序列化的更多信息,請參閱腳本序列化。
注意:如果在一個列表(或數(shù)組)中將一個元素放置兩次,當(dāng)此 列表被序列化時(shí),將獲得該元素的兩個副本,而不是獲得兩次新列表中的一個副本。
注意:如果要序列化自定義 Struct 字段,則必須為該 Struct 給定 [System.Serializable] 屬性。
提示:Unity 不會序列化 Dictionary,但您可以為鍵存儲一個 List<> 和為值存儲一個 List<>,然后在 Awake() 上將它們組合在非序列化字典中。這不能解決您需要修改字典 并將其“保存”回時(shí)出現(xiàn)的問題,但在許多其他情況下,這是一個方便的技巧。
[SerializeField]private bool hasHealthPotion = true;
SpaceAttribute 間距
可在 Inspector 中添加一些間距
public int health = 0;[Space(10)] // 10 pixels of spacing here.public int shield = 0;
TextAreaAttribute?可滾動的區(qū)域編輯字符串
屬性,用于通過高度靈活且可滾動的區(qū)域編輯字符串。
您可以指定 TextArea 的最小行數(shù)和最大行數(shù),該字段將根據(jù)文本的大小進(jìn)行擴(kuò)展。如果文本大于可用區(qū)域,則會顯示滾動條。
[TextArea]public string MyTextArea;
TooltipAttribute?工具提示
為 Inspector 窗口中的字段指定工具提示。
?下面的腳本中添加了一個?health
。它向用戶提供有關(guān)?health
?變量的值的范圍信息。建議的范圍在?TooltipAttribute
?字符串中提供。
[Tooltip("Health value between 0 and 100.")]public int health = 0;