代码改变世界

unity3d常用属性汇总

  糯米粥  阅读(3446)  评论(1编辑  收藏  举报

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
复制代码

 

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
'
点击右上角即可分享
微信分享提示