Unity3D Script脚本编辑器属性

Script Attributes:基于IDE的一系列编译器属性

JS	@Script 属性方法()
C#	[属性方法()]

Unity3D Script脚本编辑器属性一共有9种

(1)AddComponentMenu 添加组件菜单

The AddComponentMenu attribute allows you to place a script anywhere in the "Component" menu, instead of just the "Component->Scripts" menu.

You use this to organize the Component menu better, this way improving workflow when adding scripts. Important notice: You need to restart.

[AddComponentMenu("Transform/Follow Transform")]
public class FollowTransform : MonoBehaviour
{
}

(2)ContextMenu 上下文菜单

The ContextMenu attribute allows you to add commands to the context menu.

In the inspector of the attached script. When the user selects the context menu, the function will be executed.

This is most useful for automatically setting up scene data from the script. The function has to be non-static.

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)ExecuteInEditMode 在编辑模式下运行

Makes a script execute in edit mode.

By default, script components are only executed in play mode. By adding this attribute, each script component will also have its callback functions executed while the Editor is not in playmode.

The functions are not called constantly like they are in play mode.
Update is only called when something in the scene changed.
OnGUI is called when the Game View recieves an Event.
OnRenderObject and the other rendering callback functions are called on every repaint of the Scene View or Game View.

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class ExampleClass : MonoBehaviour {
    public Transform target;
    void Update() {
        if (target)
            transform.LookAt(target);
        
    }
}


(4)HideInInspector 在检视面板中隐藏

Makes a variable not show up in the inspector but be serialized.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    [HideInInspector]
    public int p = 5;
}


(5)NonSerialized 非序列化

The NonSerialized attribute marks a variable to not be serialized.

This way you can keep a variable public and Unity will not attempt to serialize it or show it in the inspector


class Test {
	// p will not be shown in the inspector or serialized
	[System.NonSerialized]
	public int p = 5;
}


(6)RPC 远程过程调用

Attribute for setting up RPC functions.

Given an @RPC (javascript) or [RPC] attribute (C#) any function can be called remotely through Unity Networking. The function must exist on both sending and recieving party.

Call a RPC function on all connected peers.

The called function must have the @RPC tag set ([RPC] for C Sharp code). A NetworkView must be attached to the GameObject where the RPC function is being called. It doesn't matter if the NetworkView is being used for something else or just for the RPC function. If it is just for the RPC function, state synchronization should be turned off and the observed property can be set to none. RPC function names should be unique accross the scene, if two RPC functions in different scripts have the same name only one of them is called when RPC is invoked. RPC calls are always guaranteed to be executed in the same order as they are sent. The communication group set for the network view, with NetworkView.group, is used for the RPC call. To get information on the RPC itelf, you can add a NetworkMessageInfo parameter to the function declaration which will automatically contain the information. You don't need to change the way you call the RPC function when you do this. For more information see the RPC section of the manual. Valid RPC parameters are int, float, string, NetworkPlayerNetworkViewIDVector3 and Quaternion.


using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Transform cubePrefab;
    void OnGUI() {
        if (GUILayout.Button("SpawnBox")) {
            NetworkViewID viewID = Network.AllocateViewID();
            networkView.RPC("SpawnBox", RPCMode.AllBuffered, viewID, transform.position);
        }
    }
    [RPC]
    void SpawnBox(NetworkViewID viewID, Vector3 location) {
        Transform clone;
        clone = Instantiate(cubePrefab, location, Quaternion.identity) as Transform as Transform;
        NetworkView nView;
        nView = clone.GetComponent<NetworkView>();
        nView.viewID = viewID;
    }
}


(7)SerializeField 序列化域

Force Unity to serialize a private field.

You will almost never need this. When Unity serializes your scripts, it will only serialize public fields. If in addition to that you also want Unity to serialize one of your private fields you can add the SerializeField attribute to the field.

Unity will serialize all your script components, reload the new assemblies, and recreate your script components from the serialized verions. This serialization does not happen with .NET's serialization functionality, but with an internal Unity one.

The serialization system used can do the following:

- CAN serialize public nonstatic fields (of serializable types)
- CAN serialize nonpublic nonstatic fields marked with the [SerializeField] attribute.
- CANNOT serialize static fields.
- CANNOT serialize properties.

Your field will only serialize if it is of a type that Unity can serialize:

Serializable types are:

- All classes inheriting from UnityEngine.Object, for example GameObject, Component, MonoBehaviour, Texture2D, AnimationClip.. - All basic data types like int, string, float, bool. - Some built-in types like Vector2, Vector3, Vector4, Quaternion, Matrix4x4, Color, Rect, LayerMask.. - Arrays of a serializable type
- List of a serializable type (new in Unity2.6)
- Enums

Headsup note: if you put one element in a list (or array) twice, when the list gets serialized, you'll get two copies of that element, instead of one copy being in the new list twice.

Hint: Unity won't serialize Dictionary, however you could store a List<> for keys and a List<> for values, and sew them up in a non serialized dictionary on Awake(). This doesn't solve the problem of when you want to modify the dictionary and have it "saved" back, but it is a handy trick in a lot of other cases.

For UnityScript users: Fields in c# is a script variable in UnityScript, and [SerializeField] becomes @SerializeField. [Serializable] on a class becomes @script Serializable in a UnityScript.

using UnityEngine;
public class SomePerson : MonoBehaviour {
	//This field gets serialized because it is public.
	public string name = "John";
	//This field does not get serialized because it is private.
	private int age = 40;
	//This field gets serialized even though it is private
	//because it has the SerializeField attribute applied.
	[SerializeField]
	private bool hasHealthPotion = true;
	void Update () {
	}
}


(8)RequireCompenent

The RequireComponent attribute lets automatically add required component as a dependency.

When you add a script which uses RequireComponent, the required component will automatically be added to the game object. This is useful to avoid setup errors. For example a script might require that a rigid body is always added to the same game object. Using RequireComponent this will be done automatically, thus you can never get the setup wrong.


[RequireComponent (typeof (Rigidbody))]
public class PlayerScript : MonoBehaviour {
	void FixedUpdate()  {
		rigidbody.AddForce(Vector3.up);
	}
}


(9)Serializable

The Serializable attribute lets you embed a class with sub properties in the inspector.

You can use this to display variables in the inspector similar to how a Vector3 shows up in the inspector. The name and a triangle to expand its properties. To do this you need create a class that derives from System.Object and give it the Serializable attribute. In JavaScript the Serializable attribute is implicit and not necessary.


[System.Serializable]
class Test
{
	public int p = 5;
	public Color c = Color.white;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值