Unity 的一些特性

  1 using System;
  2 using UnityEngine;
  3 using UnityEditor;
  4 using UnityEngine.Serialization;
  5 using Random = UnityEngine.Random;
  6 
  7 [DisallowMultipleComponent]//  禁止同时给一个物体添加多个NewBehaviourScript脚本
  8 [RequireComponent(typeof(Animator))]//  依赖Animator组件,在物体上绑定有NewBehaviourScript时,Animoter组件不可被移除
  9 [AddComponentMenu("MyScripts/NewBehaviourScript")]//  从Unity菜单栏Component里添加给物体
 10 [ExecuteInEditMode]//  可以在编辑器模式下运行
 11 [SelectionBase]//  当一个GameObject含有使用了该属性的Component的时候,在SceneView中选择该GameObject,Hierarchy上面会自动选中该GameObject的Parent。
 12 public class NewBehaviourScript : MonoBehaviour
 13 {
 14     public Animator animator;
 15 
 16     [Range(1,100)]//  范围滑块1-100
 17     public int num1;
 18 
 19     [Multiline(5)]//  多行输入
 20     public string Multiline;
 21     [TextArea(3,10)]//  文本区域  最小3行 最大10行 超出10行会出现滚动条
 22     public string textArea;
 23 
 24     [ContextMenuItem("Random", "RandomNumber")]//  右键菜单  第一个参数为菜单选项,第二个参数为菜单选项对应的函数
 25     [ContextMenuItem("Reset", "ResetNumber")]
 26     public int number;
 27     void RandomNumber()
 28     {
 29         number = Random.Range(0, 100);
 30     }
 31     void ResetNumber()
 32     {
 33         number = 0;
 34     }
 35 
 36     public Color color1;//  普通Color
 37 
 38     [ColorUsage(false)]//  可以选择是否启用alpha和HDR
 39     public Color color2;
 40 
 41     [ColorUsage(true, true)]
 42     public Color color3;
 43 
 44     [Header("Player Settings")]//  Header提示
 45     public Player player;
 46 
 47     [Serializable]//  序列化
 48     public class Player
 49     {
 50         public string name;
 51 
 52         [Range(1, 100)]
 53         public int hp;
 54     }
 55     [Header("Game Settings")]
 56     public Color Header;
 57 
 58     [Space(48)]//  在字段上方空出相对应的空间
 59     public string Space;
 60 
 61     [Tooltip("你好 你好 你好")]//  鼠标停留提示
 62     public long tooltip;
 63 
 64     [HideInInspector]//  在Inspector面板隐藏
 65     public string HideInInspector;
 66 
 67     [SerializeField]//  序列化数据
 68     [FormerlySerializedAs("hoge")]//  从以前的字段里继承数据防止数据丢失 参数为数据名称
 69     string abcd;
 70 
 71 
 72     [Range(0, 10)]
 73     public int number2;
 74     [ContextMenu("RandomNumber2")]//  在组件的设置(小齿轮)里添加点击选项
 75     void RandomNumber2()
 76     {
 77         number2 = Random.Range(0, 100);
 78     }
 79     [ContextMenu("ResetNumber2")]
 80     void ResetNumber2()
 81     {
 82         number2 = 0;
 83     }
 84 
 85     void Awake()
 86     {
 87         Debug.Log("This is Awake");
 88         animator = GetComponent<Animator>();
 89     }
 90 
 91     void Start()
 92     {
 93         Debug.Log("This is Start");
 94     }
 95 
 96     void Update()
 97     {
 98         Debug.Log("This is Update");
 99     }
100 }

猜你喜欢

转载自www.cnblogs.com/dawenhao/p/9177950.html