风动破

导航

【转载】脚本里面一些属性的用法

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
@ContextMenu ("Do Something")
function DoSomething () {
Debug.Log ("Perform operation");
}
// 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");
}
}
3。HideInInspector 在Inspector中不显示,但是支持序列化
// Javascript example@HideInInspector
var 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;

// C# Example
class Test {
// p will not be shown in the inspector or serialized
[System.NonSerialized]
public int p = 5;
}
5。RequireComponent 需求控件,当把此脚本加到一个GO中的时候,
需要GO中有所需要的其他控件,不然会报错
// Javascript example
// Mark the PlayerScript as requiring a rigidbody in the game object.
@script RequireComponent(Rigidbody)

function FixedUpdate() {
rigidbody.AddForce(Vector3.up);
}
// C# Example

[RequireComponent (typeof (Rigidbody))]
public class PlayerScript : MonoBehaviour {
void FixedUpdate() {
rigidbody.AddForce(Vector3.up);
}
}
6. Serializable 这个可以让你的变量数据在inspector中显示,
需要继承自System.Object,并且加上Serializable
// Javascript example
class Test extends System.Object {
var p = 5;
var c = Color.white;
}
var test = Test ();
// C# Example
[System.Serializable]
class Test
{
public int p = 5;
public Color c = Color.white;
}

posted on 2013-03-19 23:17  风动破  阅读(157)  评论(0)    收藏  举报