【转载】脚本里面一些属性的用法
1。AddComponentMenu 在编辑器菜单中加入菜单,
可以是多层菜单。可以是新建的菜单或者是已有菜单的自菜单,
这个在Editor中比较有用
// Javascript example
@script AddComponentMenu ("Transform/Follow Transform")
class FollowTransform extends MonoBehaviour {
}
// C# example:
[AddComponentMenu("Transform/Follow Transform")]
public class FollowTransform : MonoBehaviour
{
}
2。ContextMenu 加入到上下文菜单中,在右键菜单会出现,
点击就会执行相应的函数,
一般可以用于自动给场景准备数据的脚本当中,不能是静态函数
// Javascript example
// C# example:
public class ContextTesting : MonoBehaviour {
/// Add a context menu named "Do Something" in the inspector
/// of the attached script.
[ContextMenu ("Do Something")]
void DoSomething () {
Debug.Log ("Perform operation");
}
}
public class ContextTesting : MonoBehaviour {
/// Add a context menu named "Do Something" in the inspector
/// of the attached script.
[ContextMenu ("Do Something")]
void DoSomething () {
Debug.Log ("Perform operation");
}
}
3。HideInInspector 在Inspector中不显示,但是支持序列化
// Javascript example@HideInInspectorvar p = 5;using UnityEngine;
using System.Collections;
// C# example:
public class example : MonoBehaviour {
[HideInInspector]
public int p = 5;
}
4。NonSerialized 不在inspector中显示,也不序列化
// Javascript example
// p will not be shown in the inspector or serialized
@System.NonSerialized
var p = 5;
@System.NonSerialized
var p = 5;
// C# Example
class Test {
// p will not be shown in the inspector or serialized
[System.NonSerialized]
public int p = 5;
}
5。RequireComponent 需求控件,当把此脚本加到一个GO中的时候,class Test {
// p will not be shown in the inspector or serialized
[System.NonSerialized]
public int p = 5;
}
需要GO中有所需要的其他控件,不然会报错
// Javascript example
[RequireComponent (typeof (Rigidbody))]
public class PlayerScript : MonoBehaviour {
void FixedUpdate() {
rigidbody.AddForce(Vector3.up);
}
}
6. Serializable 这个可以让你的变量数据在inspector中显示,
// Mark the PlayerScript as requiring a rigidbody in the game object.
@script RequireComponent(Rigidbody)
function FixedUpdate() {
rigidbody.AddForce(Vector3.up);
}
// C# Example
@script RequireComponent(Rigidbody)
function FixedUpdate() {
rigidbody.AddForce(Vector3.up);
}
[RequireComponent (typeof (Rigidbody))]
public class PlayerScript : MonoBehaviour {
void FixedUpdate() {
rigidbody.AddForce(Vector3.up);
}
}
需要继承自System.Object,并且加上Serializable
// Javascript example
浙公网安备 33010602011771号