码迷,mamicode.com
首页 > 编程语言 > 详细

unity3d常用属性汇总

时间:2016-03-14 11:57:13      阅读:689      评论:0      收藏:0      [点我收藏+]

标签:

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

 

unity3d常用属性汇总

标签:

原文地址:http://www.cnblogs.com/nsky/p/5275106.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!