unity3d常用屬性匯總


unity常用的是C#語言。而C#語言有Attribute屬性。特別強大。所以unity開發的時候。可以在變量加Attribute屬性來達到開發人員想要的效果

 RequireComponent:約束組件

[RequireComponent(typeof(Rigidbody))]
  當附加該組件的時候。會強制自動添加組件typeof(Rigidbody)
 RequireComponent約束的組件。是不能刪除的。除非先刪除當前腳本

[RequireComponent(typeof(Rigidbody), typeof(BoxCollider))]
public class prot : MonoBehaviour
{
 /*
     * SerializeField序列化字段。
     * Range:該字段值的范圍。
   
     */
    [SerializeField, Range(0, 5)]
    int cont;

 

    /*
       * 可以適用於多個變量
     * 
     */
    [SerializeField, Range(1, 5)]
    int cont1, sum1;

 

 

 /*
     數組
     */
    [SerializeField, Range(1, 5)]
    int[] array;

 

 

/*
     * Tooltip:鼠標放到字段上顯示提示
     */
    [SerializeField, Tooltip("年齡")]
    int age;

 

/*
     * Space:設置字段和字段之間的間距
     * 即。距上個字段的間距
     */
    [SerializeField, Space(60)]
    string name;

 

 

  /*
     Header:字段的頭部。也可以用作顯示
     */
    [SerializeField, Header("用戶地址")]
    string address;

 

 /*
     Multiline:多行文本框
     */
    [SerializeField, Multiline(5)]
    string message;

 

 /*
     * TextArea:超過了最大值行。就會顯示滑動條
     */
    [SerializeField, TextArea(1, 7)]
    string tips;

 

/*
     ContextMenu:單擊組件右鍵菜單
     */
    [ContextMenu("callBack")]
    void CallBack()
    {
        Debug.Log("back");
    }

 

 

 

DisallowMultipleComponent:不能在一個對象上重復添加該腳本。

[DisallowMultipleComponent]
public class prot : MonoBehaviour
{

當重發添加的時候。會提示

 

 AddComponentMenu:在Component菜單中添加一項

[AddComponentMenu("腳本/prot.cs"), SelectionBase]

public class prot : MonoBehaviour
{

 

測試代碼:

  1 using UnityEngine;
  2 using System.Collections;
  3 using UnityEngine.Serialization;
  4 
  5 /*Attribute
  6  RequireComponent:約束組件
  7  * 當附加該組件的時候。會強制自動添加組件typeof(Rigidbody)
  8  * RequireComponent約束的組件。是不能刪除的。除非先刪除當前腳本
  9  */
 10 //[RequireComponent(typeof(Rigidbody))]
 11 
 12 /*
 13  DisallowMultipleComponent:不能在一個對象上重復添加該腳本。
 14  */
 15 //[DisallowMultipleComponent]
 16 //[AddComponentMenu("init")]
 17 
 18 
 19 /*
 20  屬性過多。可以換行。
 21  * AddComponentMenu:在Component菜單中添加一項
 22  */
 23 [RequireComponent(typeof(Rigidbody), typeof(BoxCollider)),
 24 DisallowMultipleComponent,
 25 AddComponentMenu("腳本/prot.cs"), SelectionBase]
 26 
 27 public class prot : MonoBehaviour
 28 {
 29     /*
 30      * SerializeField序列化字段。
 31      * Range:該字段值的范圍。
 32    
 33      */
 34     [SerializeField, Range(0, 5)]
 35     int cont;
 36 
 37     /*
 38        * 可以適用於多個變量
 39      * 
 40      */
 41     [SerializeField, Range(1, 5)]
 42     int cont1, sum1;
 43 
 44     /*
 45      數組
 46      */
 47     [SerializeField, Range(1, 5)]
 48     int[] array;
 49 
 50 
 51     /*
 52      * Tooltip:鼠標放到字段上顯示提示
 53      */
 54     [SerializeField, Tooltip("年齡")]
 55     int age;
 56 
 57     /*
 58      * Space:設置字段和字段之間的間距
 59      * 即。距上個字段的間距
 60      */
 61     [SerializeField, Space(60)]
 62     string name;
 63 
 64     /*
 65      Header:字段的頭部。也可以用作顯示
 66      */
 67     [SerializeField, Header("用戶地址")]
 68     string address;
 69 
 70     /*
 71      Multiline:多行文本框
 72      */
 73     [SerializeField, Multiline(5)]
 74     string message;
 75 
 76     /*
 77      * TextArea:超過了最大值行。就會顯示滑動條
 78      */
 79     [SerializeField, TextArea(1, 7)]
 80     string tips;
 81 
 82     /*
 83      NonSerialized:不序列化,並且不顯示在inspector中。
 84      * 
 85      * HideInInspector:也表示隱藏
 86      */
 87     //[System.NonSerialized]
 88     [FormerlySerializedAs("b")]
 89     public int a;
 90 
 91     [FormerlySerializedAs("a")]
 92     public int b;
 93     void Start()
 94     {
 95 
 96 
 97     }
 98 
 99     // Update is called once per frame
100     void Update()
101     {
102 
103     }
104     /*
105      ContextMenu:單擊組件右鍵菜單
106      */
107     [ContextMenu("callBack")]
108     void CallBack()
109     {
110         Debug.Log("back");
111     }
112 }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM