Unity常用API筆記


一.概述

  Unity使用反射即時獲取和加載腳本信息。Unity所有腳本繼承自Monobehaviour類,這個類的內容如下:

namespace UnityEngine
{
    //
    // 摘要:
    //     MonoBehaviour is the base class from which every Unity script derives.
    [ExtensionOfNativeClass]
    [NativeHeader("Runtime/Mono/MonoBehaviour.h")]
    [NativeHeader("Runtime/Scripting/DelayedCallUtility.h")]
    [RequiredByNativeCode]
    public class MonoBehaviour : Behaviour
    {
        public MonoBehaviour();

        //
        // 摘要:
        //     Disabling this lets you skip the GUI layout phase.
        public bool useGUILayout { get; set; }
        //
        // 摘要:
        //     Allow a specific instance of a MonoBehaviour to run in edit mode (only available
        //     in the editor).
        public bool runInEditMode { get; set; }

        //
        // 摘要:
        //     Logs message to the Unity Console (identical to Debug.Log).
        //
        // 參數:
        //   message:
        public static void print(object message);
        //
        // 摘要:
        //     Cancels all Invoke calls with name methodName on this behaviour.
        //
        // 參數:
        //   methodName:
        public void CancelInvoke(string methodName);
        //
        // 摘要:
        //     Cancels all Invoke calls on this MonoBehaviour.
        public void CancelInvoke();
        //
        // 摘要:
        //     Invokes the method methodName in time seconds.
        //
        // 參數:
        //   methodName:
        //
        //   time:
        public void Invoke(string methodName, float time);
        //
        // 摘要:
        //     Invokes the method methodName in time seconds, then repeatedly every repeatRate
        //     seconds.
        //
        // 參數:
        //   methodName:
        //
        //   time:
        //
        //   repeatRate:
        public void InvokeRepeating(string methodName, float time, float repeatRate);
        //
        // 摘要:
        //     Is any invoke on methodName pending?
        //
        // 參數:
        //   methodName:
        public bool IsInvoking(string methodName);
        //
        // 摘要:
        //     Is any invoke pending on this MonoBehaviour?
        public bool IsInvoking();
        //
        // 摘要:
        //     Starts a coroutine named methodName.
        //
        // 參數:
        //   methodName:
        //
        //   value:
        [ExcludeFromDocs]
        public Coroutine StartCoroutine(string methodName);
        //
        // 摘要:
        //     Starts a Coroutine.
        //
        // 參數:
        //   routine:
        public Coroutine StartCoroutine(IEnumerator routine);
        //
        // 摘要:
        //     Starts a coroutine named methodName.
        //
        // 參數:
        //   methodName:
        //
        //   value:
        public Coroutine StartCoroutine(string methodName, [DefaultValue("null")] object value);
        [Obsolete("StartCoroutine_Auto has been deprecated. Use StartCoroutine instead (UnityUpgradable) -> StartCoroutine([mscorlib] System.Collections.IEnumerator)", false)]
        public Coroutine StartCoroutine_Auto(IEnumerator routine);
        //
        // 摘要:
        //     Stops all coroutines running on this behaviour.
        public void StopAllCoroutines();
        //
        // 摘要:
        //     Stops the first coroutine named methodName, or the coroutine stored in routine
        //     running on this behaviour.
        //
        // 參數:
        //   methodName:
        //     Name of coroutine.
        //
        //   routine:
        //     Name of the function in code, including coroutines.
        public void StopCoroutine(IEnumerator routine);
        //
        // 摘要:
        //     Stops the first coroutine named methodName, or the coroutine stored in routine
        //     running on this behaviour.
        //
        // 參數:
        //   methodName:
        //     Name of coroutine.
        //
        //   routine:
        //     Name of the function in code, including coroutines.
        public void StopCoroutine(Coroutine routine);
        //
        // 摘要:
        //     Stops the first coroutine named methodName, or the coroutine stored in routine
        //     running on this behaviour.
        //
        // 參數:
        //   methodName:
        //     Name of coroutine.
        //
        //   routine:
        //     Name of the function in code, including coroutines.
        public void StopCoroutine(string methodName);
    }
}

可以看到,腳本的生命周期函數,如常用的Awake、OnEnable、Start、FixedUpdate、OnTriggerXXX、OnCollisionXXX、Update、LateUpdate、OnGUI、OnDisable、OnDestroy等並不在里面,因為這些函數不是從父類繼承然后重寫的(也沒有override關鍵字),是Unity通過反射自動獲取我們在腳本中定義了哪些生命周期函數,然后按照Unity定義好的執行順序自動執行這些函數。游戲可以理解為就是一個死循環,在循環中進行了游戲邏輯運算、畫面繪制等,每循環一次就是一幀,而Unity就是幫助我們定義好了這個死循環框架,並提供好了生命周期函數,我們只需要將需要執行的邏輯和繪制的畫面等在相應的生命周期函數中定義好即可。打個比方,Unity的游戲場景就是一個舞台,而游戲物體就是舞台上的演員,游戲物體的具體形狀、物理特性等都是通過腳本定義的,這些腳本就相當於是演員的劇本,而腳本的生命周期函數就是劇本的書寫模式,如准備上台時需要做的動作就在Awake等函數中定義,台上不斷執行的動作在Update等函數中定義。

  常用的生命周期函數:Awake:腳本創建時執行;OnEnable:腳本激活時執行;Start:游戲的第一幀更新之前執行;FixedUpdate:定時更新,游戲中循環執行,每秒執行的次數固定,可以在Unity中設置這個函數每秒執行的次數,一般用於物理邏輯更新;Update:游戲邏輯更新,循環執行,每秒執行的次數不能自定義,和函數的計算量和計算機性能有關;LateUpdate:后更新,循環執行,一般用於處理攝像機的相關行為;OnGUI:游戲的渲染,循環執行;OnDisable:腳本失活時執行;OnDestroy:腳本被銷毀前執行。此外還有OntriggerXXX、OnMouseXXX、OnCollisionXXX等常用函數。一般地,首先執行的時Awake、OnEnable、Start等腳本創建時執行的函數,Awake只執行一次,OnEnable、Start一般也只執行一次,但是如果腳本多次失活激活,也可能執行多次;這部分函數執行完成后進入循環更新的函數,如FixedUpdate、Update、LateUpdate等,這其中物理邏輯先執行,如FixedUpdate,而OnTriggerXXX、OnCollisionXXX等物理碰撞有關的函數在特定情況下執行(有碰撞時),接下來是輸入獲取,然后是游戲的邏輯更新,其中在進行物理更新和游戲的邏輯更新時都會進行動畫更新,最后在游戲邏輯更新后進行各種畫面的渲染如OnGUI函數(GUI渲染),之后重復更新物理情況,進入下一個循環;在腳本需要被銷毀時,就會跳出循環,執行相應的函數如OnDisable、OnDistroy等函數。下圖是官方文檔中的腳本生命周期函數的執行順序:

  在Unity中,並不是所有的效果都需要我們自己寫腳本實現,Unity已經為我們提供了一些寫好的腳本,這些腳本稱之為組件,下圖是Unity中常用類的類圖:

下圖是簡化版的類繼承關系:

  從類關系中可以看到,Unity提供的組件都不是繼承自MonoBehaviour的,但是我們自定義的掛載在游戲物體上的腳本一定是繼承自這個類或者他的子類的。

  PS:Unity中,繼承自MonoBehaviour的類是不能new的,這是Unity根據自身調用這些腳本的方式定義好的規則,所以也不需要構造函數,如果要在Unity中使用腳本,就必須要首先通過一些方式獲取腳本后文會進行介紹。

二.GameObject類、Object類和Component類中的常用方法

  首先要說一說Component類,下面是Component類:

namespace UnityEngine
{
    //
    // 摘要:
    //     Base class for everything attached to GameObjects.
    [NativeClass("Unity::Component")]
    [NativeHeader("Runtime/Export/Scripting/Component.bindings.h")]
    [RequiredByNativeCode]
    public class Component : Object
    {
        public Component();

        //
        // 摘要:
        //     The ParticleSystem attached to this GameObject. (Null if there is none attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property particleSystem has been deprecated. Use GetComponent<ParticleSystem>() instead. (UnityUpgradable)", true)]
        public Component particleSystem { get; }
        //
        // 摘要:
        //     The Transform attached to this GameObject.
        public Transform transform { get; }
        //
        // 摘要:
        //     The game object this component is attached to. A component is always attached
        //     to a game object.
        public GameObject gameObject { get; }
        //
        // 摘要:
        //     The tag of this game object.
        public string tag { get; set; }
        //
        // 摘要:
        //     The Rigidbody attached to this GameObject. (Null if there is none attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property rigidbody has been deprecated. Use GetComponent<Rigidbody>() instead. (UnityUpgradable)", true)]
        public Component rigidbody { get; }
        //
        // 摘要:
        //     The HingeJoint attached to this GameObject. (Null if there is none attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property hingeJoint has been deprecated. Use GetComponent<HingeJoint>() instead. (UnityUpgradable)", true)]
        public Component hingeJoint { get; }
        //
        // 摘要:
        //     The Camera attached to this GameObject. (Null if there is none attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property camera has been deprecated. Use GetComponent<Camera>() instead. (UnityUpgradable)", true)]
        public Component camera { get; }
        //
        // 摘要:
        //     The Rigidbody2D that is attached to the Component's GameObject.
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property rigidbody2D has been deprecated. Use GetComponent<Rigidbody2D>() instead. (UnityUpgradable)", true)]
        public Component rigidbody2D { get; }
        //
        // 摘要:
        //     The Animation attached to this GameObject. (Null if there is none attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property animation has been deprecated. Use GetComponent<Animation>() instead. (UnityUpgradable)", true)]
        public Component animation { get; }
        //
        // 摘要:
        //     The ConstantForce attached to this GameObject. (Null if there is none attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property constantForce has been deprecated. Use GetComponent<ConstantForce>() instead. (UnityUpgradable)", true)]
        public Component constantForce { get; }
        //
        // 摘要:
        //     The Renderer attached to this GameObject. (Null if there is none attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property renderer has been deprecated. Use GetComponent<Renderer>() instead. (UnityUpgradable)", true)]
        public Component renderer { get; }
        //
        // 摘要:
        //     The AudioSource attached to this GameObject. (Null if there is none attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property audio has been deprecated. Use GetComponent<AudioSource>() instead. (UnityUpgradable)", true)]
        public Component audio { get; }
        //
        // 摘要:
        //     The NetworkView attached to this GameObject (Read Only). (null if there is none
        //     attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property networkView has been deprecated. Use GetComponent<NetworkView>() instead. (UnityUpgradable)", true)]
        public Component networkView { get; }
        //
        // 摘要:
        //     The Collider attached to this GameObject. (Null if there is none attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property collider has been deprecated. Use GetComponent<Collider>() instead. (UnityUpgradable)", true)]
        public Component collider { get; }
        //
        // 摘要:
        //     The Collider2D component attached to the object.
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property collider2D has been deprecated. Use GetComponent<Collider2D>() instead. (UnityUpgradable)", true)]
        public Component collider2D { get; }
        //
        // 摘要:
        //     The Light attached to this GameObject. (Null if there is none attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property light has been deprecated. Use GetComponent<Light>() instead. (UnityUpgradable)", true)]
        public Component light { get; }

        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object
        //     or any of its children.
        //
        // 參數:
        //   methodName:
        //     Name of the method to call.
        //
        //   parameter:
        //     Optional parameter to pass to the method (can be any value).
        //
        //   options:
        //     Should an error be raised if the method does not exist for a given target object?
        public void BroadcastMessage(string methodName, SendMessageOptions options);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object
        //     or any of its children.
        //
        // 參數:
        //   methodName:
        //     Name of the method to call.
        //
        //   parameter:
        //     Optional parameter to pass to the method (can be any value).
        //
        //   options:
        //     Should an error be raised if the method does not exist for a given target object?
        [ExcludeFromDocs]
        public void BroadcastMessage(string methodName);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object
        //     or any of its children.
        //
        // 參數:
        //   methodName:
        //     Name of the method to call.
        //
        //   parameter:
        //     Optional parameter to pass to the method (can be any value).
        //
        //   options:
        //     Should an error be raised if the method does not exist for a given target object?
        [ExcludeFromDocs]
        public void BroadcastMessage(string methodName, object parameter);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object
        //     or any of its children.
        //
        // 參數:
        //   methodName:
        //     Name of the method to call.
        //
        //   parameter:
        //     Optional parameter to pass to the method (can be any value).
        //
        //   options:
        //     Should an error be raised if the method does not exist for a given target object?
        [FreeFunction("BroadcastMessage", HasExplicitThis = true)]
        public void BroadcastMessage(string methodName, [Internal.DefaultValue("null")] object parameter, [Internal.DefaultValue("SendMessageOptions.RequireReceiver")] SendMessageOptions options);
        //
        // 摘要:
        //     Is this game object tagged with tag ?
        //
        // 參數:
        //   tag:
        //     The tag to compare.
        public bool CompareTag(string tag);
        //
        // 摘要:
        //     Returns the component of Type type if the game object has one attached, null
        //     if it doesn't.
        //
        // 參數:
        //   type:
        //     The type of Component to retrieve.
        [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
        public Component GetComponent(Type type);
        [SecuritySafeCritical]
        public T GetComponent<T>();
        //
        // 摘要:
        //     Returns the component with name type if the game object has one attached, null
        //     if it doesn't.
        //
        // 參數:
        //   type:
        [FreeFunction(HasExplicitThis = true)]
        public Component GetComponent(string type);
        [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
        public Component GetComponentInChildren(Type t, bool includeInactive);
        //
        // 摘要:
        //     Returns the component of Type type in the GameObject or any of its children using
        //     depth first search.
        //
        // 參數:
        //   t:
        //     The type of Component to retrieve.
        //
        // 返回結果:
        //     A component of the matching type, if found.
        [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
        public Component GetComponentInChildren(Type t);
        public T GetComponentInChildren<T>([Internal.DefaultValue("false")] bool includeInactive);
        [ExcludeFromDocs]
        public T GetComponentInChildren<T>();
        //
        // 摘要:
        //     Returns the component of Type type in the GameObject or any of its parents.
        //
        // 參數:
        //   t:
        //     The type of Component to retrieve.
        //
        // 返回結果:
        //     A component of the matching type, if found.
        [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
        public Component GetComponentInParent(Type t);
        public T GetComponentInParent<T>();
        //
        // 摘要:
        //     Returns all components of Type type in the GameObject.
        //
        // 參數:
        //   type:
        //     The type of Component to retrieve.
        public Component[] GetComponents(Type type);
        public void GetComponents(Type type, List<Component> results);
        public void GetComponents<T>(List<T> results);
        public T[] GetComponents<T>();
        [ExcludeFromDocs]
        public Component[] GetComponentsInChildren(Type t);
        public void GetComponentsInChildren<T>(List<T> results);
        public T[] GetComponentsInChildren<T>();
        public void GetComponentsInChildren<T>(bool includeInactive, List<T> result);
        public T[] GetComponentsInChildren<T>(bool includeInactive);
        //
        // 摘要:
        //     Returns all components of Type type in the GameObject or any of its children.
        //
        // 參數:
        //   t:
        //     The type of Component to retrieve.
        //
        //   includeInactive:
        //     Should Components on inactive GameObjects be included in the found set? includeInactive
        //     decides which children of the GameObject will be searched. The GameObject that
        //     you call GetComponentsInChildren on is always searched regardless.
        public Component[] GetComponentsInChildren(Type t, bool includeInactive);
        public T[] GetComponentsInParent<T>(bool includeInactive);
        [ExcludeFromDocs]
        public Component[] GetComponentsInParent(Type t);
        //
        // 摘要:
        //     Returns all components of Type type in the GameObject or any of its parents.
        //
        // 參數:
        //   t:
        //     The type of Component to retrieve.
        //
        //   includeInactive:
        //     Should inactive Components be included in the found set?
        public Component[] GetComponentsInParent(Type t, [Internal.DefaultValue("false")] bool includeInactive);
        public T[] GetComponentsInParent<T>();
        public void GetComponentsInParent<T>(bool includeInactive, List<T> results);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object.
        //
        // 參數:
        //   methodName:
        //     Name of the method to call.
        //
        //   value:
        //     Optional parameter for the method.
        //
        //   options:
        //     Should an error be raised if the target object doesn't implement the method for
        //     the message?
        [FreeFunction("SendMessage", HasExplicitThis = true)]
        public void SendMessage(string methodName, object value, SendMessageOptions options);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object.
        //
        // 參數:
        //   methodName:
        //     Name of the method to call.
        //
        //   value:
        //     Optional parameter for the method.
        //
        //   options:
        //     Should an error be raised if the target object doesn't implement the method for
        //     the message?
        public void SendMessage(string methodName);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object.
        //
        // 參數:
        //   methodName:
        //     Name of the method to call.
        //
        //   value:
        //     Optional parameter for the method.
        //
        //   options:
        //     Should an error be raised if the target object doesn't implement the method for
        //     the message?
        public void SendMessage(string methodName, object value);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object.
        //
        // 參數:
        //   methodName:
        //     Name of the method to call.
        //
        //   value:
        //     Optional parameter for the method.
        //
        //   options:
        //     Should an error be raised if the target object doesn't implement the method for
        //     the message?
        public void SendMessage(string methodName, SendMessageOptions options);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object
        //     and on every ancestor of the behaviour.
        //
        // 參數:
        //   methodName:
        //     Name of method to call.
        //
        //   value:
        //     Optional parameter value for the method.
        //
        //   options:
        //     Should an error be raised if the method does not exist on the target object?
        [ExcludeFromDocs]
        public void SendMessageUpwards(string methodName, object value);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object
        //     and on every ancestor of the behaviour.
        //
        // 參數:
        //   methodName:
        //     Name of method to call.
        //
        //   value:
        //     Optional parameter value for the method.
        //
        //   options:
        //     Should an error be raised if the method does not exist on the target object?
        public void SendMessageUpwards(string methodName, SendMessageOptions options);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object
        //     and on every ancestor of the behaviour.
        //
        // 參數:
        //   methodName:
        //     Name of method to call.
        //
        //   value:
        //     Optional parameter value for the method.
        //
        //   options:
        //     Should an error be raised if the method does not exist on the target object?
        [ExcludeFromDocs]
        public void SendMessageUpwards(string methodName);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object
        //     and on every ancestor of the behaviour.
        //
        // 參數:
        //   methodName:
        //     Name of method to call.
        //
        //   value:
        //     Optional parameter value for the method.
        //
        //   options:
        //     Should an error be raised if the method does not exist on the target object?
        [FreeFunction(HasExplicitThis = true)]
        public void SendMessageUpwards(string methodName, [Internal.DefaultValue("null")] object value, [Internal.DefaultValue("SendMessageOptions.RequireReceiver")] SendMessageOptions options);
        [SecuritySafeCritical]
        public bool TryGetComponent<T>(out T component);
        [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
        public bool TryGetComponent(Type type, out Component component);
    }
}

在這個類中,可以發現有很多屬性,像gameObject、transform、rigidbody等,這些屬性都對應着組件所在游戲物體身上的其他組件,但是可以看到除了transform、gameObject和tag這3個屬性外,其他屬性都標注了Obsolute特性,表示棄用的,調用時如下圖:

 這些已棄用的組件現在都需要使用GetComponent函數去獲取。沒有棄用的3個屬性使用還是非常普遍的,其中transform是Transform組件對象,是當前物體的位置信息(位置坐標、旋轉、縮放等信息)對象,tag是當前物體的標簽對象,gameObject對象是當前的游戲物體類GameObject的對象,GameObject類繼承自Object類,是Component的“兄弟類”(見上面的簡版類圖),這個類是游戲物體的模板類。下面是GameObject類:

namespace UnityEngine
{
    //
    // 摘要:
    //     Base class for all entities in Unity Scenes.
    [ExcludeFromPreset]
    [NativeHeader("Runtime/Export/Scripting/GameObject.bindings.h")]
    [UsedByNativeCode]
    public sealed class GameObject : Object
    {
        //
        // 摘要:
        //     Creates a new game object, named name.
        //
        // 參數:
        //   name:
        //     The name that the GameObject is created with.
        //
        //   components:
        //     A list of Components to add to the GameObject on creation.
        public GameObject();
        //
        // 摘要:
        //     Creates a new game object, named name.
        //
        // 參數:
        //   name:
        //     The name that the GameObject is created with.
        //
        //   components:
        //     A list of Components to add to the GameObject on creation.
        public GameObject(string name);
        //
        // 摘要:
        //     Creates a new game object, named name.
        //
        // 參數:
        //   name:
        //     The name that the GameObject is created with.
        //
        //   components:
        //     A list of Components to add to the GameObject on creation.
        public GameObject(string name, params Type[] components);

        //
        // 摘要:
        //     The ParticleSystem attached to this GameObject (Read Only). (Null if there is
        //     none attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property particleSystem has been deprecated. Use GetComponent<ParticleSystem>() instead. (UnityUpgradable)", true)]
        public Component particleSystem { get; }
        //
        // 摘要:
        //     The Transform attached to this GameObject.
        public Transform transform { get; }
        //
        // 摘要:
        //     The layer the game object is in.
        public int layer { get; set; }
        [Obsolete("GameObject.active is obsolete. Use GameObject.SetActive(), GameObject.activeSelf or GameObject.activeInHierarchy.")]
        public bool active { get; set; }
        //
        // 摘要:
        //     The local active state of this GameObject. (Read Only)
        public bool activeSelf { get; }
        //
        // 摘要:
        //     Defines whether the GameObject is active in the Scene.
        public bool activeInHierarchy { get; }
        //
        // 摘要:
        //     Gets and sets the GameObject's StaticEditorFlags.
        public bool isStatic { get; set; }
        //
        // 摘要:
        //     The tag of this game object.
        public string tag { get; set; }
        //
        // 摘要:
        //     Scene that the GameObject is part of.
        public Scene scene { get; }
        //
        // 摘要:
        //     Scene culling mask Unity uses to determine which scene to render the GameObject
        //     in.
        public ulong sceneCullingMask { get; }
        public GameObject gameObject { get; }
        //
        // 摘要:
        //     The Rigidbody2D component attached to this GameObject. (Read Only)
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property rigidbody2D has been deprecated. Use GetComponent<Rigidbody2D>() instead. (UnityUpgradable)", true)]
        public Component rigidbody2D { get; }
        //
        // 摘要:
        //     The Camera attached to this GameObject (Read Only). (Null if there is none attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property camera has been deprecated. Use GetComponent<Camera>() instead. (UnityUpgradable)", true)]
        public Component camera { get; }
        //
        // 摘要:
        //     The Light attached to this GameObject (Read Only). (Null if there is none attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property light has been deprecated. Use GetComponent<Light>() instead. (UnityUpgradable)", true)]
        public Component light { get; }
        //
        // 摘要:
        //     The Animation attached to this GameObject (Read Only). (Null if there is none
        //     attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property animation has been deprecated. Use GetComponent<Animation>() instead. (UnityUpgradable)", true)]
        public Component animation { get; }
        //
        // 摘要:
        //     The ConstantForce attached to this GameObject (Read Only). (Null if there is
        //     none attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property constantForce has been deprecated. Use GetComponent<ConstantForce>() instead. (UnityUpgradable)", true)]
        public Component constantForce { get; }
        //
        // 摘要:
        //     The Renderer attached to this GameObject (Read Only). (Null if there is none
        //     attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property renderer has been deprecated. Use GetComponent<Renderer>() instead. (UnityUpgradable)", true)]
        public Component renderer { get; }
        //
        // 摘要:
        //     The AudioSource attached to this GameObject (Read Only). (Null if there is none
        //     attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property audio has been deprecated. Use GetComponent<AudioSource>() instead. (UnityUpgradable)", true)]
        public Component audio { get; }
        //
        // 摘要:
        //     The NetworkView attached to this GameObject (Read Only). (Null if there is none
        //     attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property networkView has been deprecated. Use GetComponent<NetworkView>() instead. (UnityUpgradable)", true)]
        public Component networkView { get; }
        //
        // 摘要:
        //     The Collider attached to this GameObject (Read Only). (Null if there is none
        //     attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property collider has been deprecated. Use GetComponent<Collider>() instead. (UnityUpgradable)", true)]
        public Component collider { get; }
        //
        // 摘要:
        //     The Collider2D component attached to this object.
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property collider2D has been deprecated. Use GetComponent<Collider2D>() instead. (UnityUpgradable)", true)]
        public Component collider2D { get; }
        //
        // 摘要:
        //     The Rigidbody attached to this GameObject (Read Only). (Null if there is none
        //     attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property rigidbody has been deprecated. Use GetComponent<Rigidbody>() instead. (UnityUpgradable)", true)]
        public Component rigidbody { get; }
        //
        // 摘要:
        //     The HingeJoint attached to this GameObject (Read Only). (Null if there is none
        //     attached).
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property hingeJoint has been deprecated. Use GetComponent<HingeJoint>() instead. (UnityUpgradable)", true)]
        public Component hingeJoint { get; }

        //
        // 摘要:
        //     Creates a game object with a primitive mesh renderer and appropriate collider.
        //
        // 參數:
        //   type:
        //     The type of primitive object to create.
        [FreeFunction("GameObjectBindings::CreatePrimitive")]
        public static GameObject CreatePrimitive(PrimitiveType type);
        //
        // 摘要:
        //     Finds a GameObject by name and returns it.
        //
        // 參數:
        //   name:
        [FreeFunction(Name = "GameObjectBindings::Find")]
        public static GameObject Find(string name);
        //
        // 摘要:
        //     Returns an array of active GameObjects tagged tag. Returns empty array if no
        //     GameObject was found.
        //
        // 參數:
        //   tag:
        //     The name of the tag to search GameObjects for.
        [FreeFunction(Name = "GameObjectBindings::FindGameObjectsWithTag", ThrowsException = true)]
        public static GameObject[] FindGameObjectsWithTag(string tag);
        [FreeFunction(Name = "GameObjectBindings::FindGameObjectWithTag", ThrowsException = true)]
        public static GameObject FindGameObjectWithTag(string tag);
        //
        // 摘要:
        //     Returns one active GameObject tagged tag. Returns null if no GameObject was found.
        //
        // 參數:
        //   tag:
        //     The tag to search for.
        public static GameObject FindWithTag(string tag);
        public T AddComponent<T>() where T : Component;
        //
        // 摘要:
        //     Adds a component class named className to the game object.
        //
        // 參數:
        //   className:
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("GameObject.AddComponent with string argument has been deprecated. Use GameObject.AddComponent<T>() instead. (UnityUpgradable).", true)]
        public Component AddComponent(string className);
        //
        // 摘要:
        //     Adds a component class of type componentType to the game object. C# Users can
        //     use a generic version.
        //
        // 參數:
        //   componentType:
        [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
        public Component AddComponent(Type componentType);
        //
        // 參數:
        //   methodName:
        //
        //   options:
        public void BroadcastMessage(string methodName, SendMessageOptions options);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object
        //     or any of its children.
        //
        // 參數:
        //   methodName:
        //
        //   parameter:
        //
        //   options:
        [FreeFunction(Name = "Scripting::BroadcastScriptingMessage", HasExplicitThis = true)]
        public void BroadcastMessage(string methodName, [Internal.DefaultValue("null")] object parameter, [Internal.DefaultValue("SendMessageOptions.RequireReceiver")] SendMessageOptions options);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object
        //     or any of its children.
        //
        // 參數:
        //   methodName:
        //
        //   parameter:
        //
        //   options:
        [ExcludeFromDocs]
        public void BroadcastMessage(string methodName, object parameter);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object
        //     or any of its children.
        //
        // 參數:
        //   methodName:
        //
        //   parameter:
        //
        //   options:
        [ExcludeFromDocs]
        public void BroadcastMessage(string methodName);
        //
        // 摘要:
        //     Is this game object tagged with tag ?
        //
        // 參數:
        //   tag:
        //     The tag to compare.
        [FreeFunction(Name = "GameObjectBindings::CompareTag", HasExplicitThis = true)]
        public bool CompareTag(string tag);
        [SecuritySafeCritical]
        public T GetComponent<T>();
        //
        // 摘要:
        //     Returns the component of Type type if the game object has one attached, null
        //     if it doesn't.
        //
        // 參數:
        //   type:
        //     The type of Component to retrieve.
        [FreeFunction(Name = "GameObjectBindings::GetComponentFromType", HasExplicitThis = true, ThrowsException = true)]
        [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
        public Component GetComponent(Type type);
        //
        // 摘要:
        //     Returns the component with name type if the game object has one attached, null
        //     if it doesn't.
        //
        // 參數:
        //   type:
        //     The type of Component to retrieve.
        public Component GetComponent(string type);
        //
        // 摘要:
        //     Returns the component of Type type in the GameObject or any of its children using
        //     depth first search.
        //
        // 參數:
        //   type:
        //     The type of Component to retrieve.
        //
        //   includeInactive:
        //
        // 返回結果:
        //     A component of the matching type, if found.
        [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
        public Component GetComponentInChildren(Type type);
        //
        // 摘要:
        //     Returns the component of Type type in the GameObject or any of its children using
        //     depth first search.
        //
        // 參數:
        //   type:
        //     The type of Component to retrieve.
        //
        //   includeInactive:
        //
        // 返回結果:
        //     A component of the matching type, if found.
        [FreeFunction(Name = "GameObjectBindings::GetComponentInChildren", HasExplicitThis = true, ThrowsException = true)]
        [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
        public Component GetComponentInChildren(Type type, bool includeInactive);
        public T GetComponentInChildren<T>([Internal.DefaultValue("false")] bool includeInactive);
        [ExcludeFromDocs]
        public T GetComponentInChildren<T>();
        public T GetComponentInParent<T>();
        //
        // 摘要:
        //     Returns the component of Type type in the GameObject or any of its parents.
        //
        // 參數:
        //   type:
        //     Type of component to find.
        [FreeFunction(Name = "GameObjectBindings::GetComponentInParent", HasExplicitThis = true, ThrowsException = true)]
        [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
        public Component GetComponentInParent(Type type);
        //
        // 摘要:
        //     Returns all components of Type type in the GameObject.
        //
        // 參數:
        //   type:
        //     The type of component to retrieve.
        public Component[] GetComponents(Type type);
        public T[] GetComponents<T>();
        public void GetComponents(Type type, List<Component> results);
        public void GetComponents<T>(List<T> results);
        public T[] GetComponentsInChildren<T>();
        //
        // 摘要:
        //     Returns all components of Type type in the GameObject or any of its children.
        //
        // 參數:
        //   type:
        //     The type of Component to retrieve.
        //
        //   includeInactive:
        //     Should Components on inactive GameObjects be included in the found set?
        [ExcludeFromDocs]
        public Component[] GetComponentsInChildren(Type type);
        //
        // 摘要:
        //     Returns all components of Type type in the GameObject or any of its children.
        //
        // 參數:
        //   type:
        //     The type of Component to retrieve.
        //
        //   includeInactive:
        //     Should Components on inactive GameObjects be included in the found set?
        public Component[] GetComponentsInChildren(Type type, [Internal.DefaultValue("false")] bool includeInactive);
        public T[] GetComponentsInChildren<T>(bool includeInactive);
        public void GetComponentsInChildren<T>(bool includeInactive, List<T> results);
        public void GetComponentsInChildren<T>(List<T> results);
        public T[] GetComponentsInParent<T>(bool includeInactive);
        [ExcludeFromDocs]
        public Component[] GetComponentsInParent(Type type);
        public void GetComponentsInParent<T>(bool includeInactive, List<T> results);
        public T[] GetComponentsInParent<T>();
        //
        // 摘要:
        //     Returns all components of Type type in the GameObject or any of its parents.
        //
        // 參數:
        //   type:
        //     The type of Component to retrieve.
        //
        //   includeInactive:
        //     Should inactive Components be included in the found set?
        public Component[] GetComponentsInParent(Type type, [Internal.DefaultValue("false")] bool includeInactive);
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("gameObject.PlayAnimation is not supported anymore. Use animation.Play()", true)]
        public void PlayAnimation(Object animation);
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("GameObject.SampleAnimation(AnimationClip, float) has been deprecated. Use AnimationClip.SampleAnimation(GameObject, float) instead (UnityUpgradable).", true)]
        public void SampleAnimation(Object clip, float time);
        //
        // 參數:
        //   methodName:
        //
        //   options:
        public void SendMessage(string methodName, SendMessageOptions options);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object.
        //
        // 參數:
        //   methodName:
        //     The name of the method to call.
        //
        //   value:
        //     An optional parameter value to pass to the called method.
        //
        //   options:
        //     Should an error be raised if the method doesn't exist on the target object?
        [FreeFunction(Name = "Scripting::SendScriptingMessage", HasExplicitThis = true)]
        public void SendMessage(string methodName, [Internal.DefaultValue("null")] object value, [Internal.DefaultValue("SendMessageOptions.RequireReceiver")] SendMessageOptions options);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object.
        //
        // 參數:
        //   methodName:
        //     The name of the method to call.
        //
        //   value:
        //     An optional parameter value to pass to the called method.
        //
        //   options:
        //     Should an error be raised if the method doesn't exist on the target object?
        [ExcludeFromDocs]
        public void SendMessage(string methodName, object value);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object.
        //
        // 參數:
        //   methodName:
        //     The name of the method to call.
        //
        //   value:
        //     An optional parameter value to pass to the called method.
        //
        //   options:
        //     Should an error be raised if the method doesn't exist on the target object?
        [ExcludeFromDocs]
        public void SendMessage(string methodName);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object
        //     and on every ancestor of the behaviour.
        //
        // 參數:
        //   methodName:
        //     The name of the method to call.
        //
        //   value:
        //     An optional parameter value to pass to the called method.
        //
        //   options:
        //     Should an error be raised if the method doesn't exist on the target object?
        [FreeFunction(Name = "Scripting::SendScriptingMessageUpwards", HasExplicitThis = true)]
        public void SendMessageUpwards(string methodName, [Internal.DefaultValue("null")] object value, [Internal.DefaultValue("SendMessageOptions.RequireReceiver")] SendMessageOptions options);
        //
        // 參數:
        //   methodName:
        //
        //   options:
        public void SendMessageUpwards(string methodName, SendMessageOptions options);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object
        //     and on every ancestor of the behaviour.
        //
        // 參數:
        //   methodName:
        //     The name of the method to call.
        //
        //   value:
        //     An optional parameter value to pass to the called method.
        //
        //   options:
        //     Should an error be raised if the method doesn't exist on the target object?
        [ExcludeFromDocs]
        public void SendMessageUpwards(string methodName);
        //
        // 摘要:
        //     Calls the method named methodName on every MonoBehaviour in this game object
        //     and on every ancestor of the behaviour.
        //
        // 參數:
        //   methodName:
        //     The name of the method to call.
        //
        //   value:
        //     An optional parameter value to pass to the called method.
        //
        //   options:
        //     Should an error be raised if the method doesn't exist on the target object?
        [ExcludeFromDocs]
        public void SendMessageUpwards(string methodName, object value);
        //
        // 摘要:
        //     ActivatesDeactivates the GameObject, depending on the given true or false/ value.
        //
        // 參數:
        //   value:
        //     Activate or deactivate the object, where true activates the GameObject and false
        //     deactivates the GameObject.
        [NativeMethod(Name = "SetSelfActive")]
        public void SetActive(bool value);
        [NativeMethod(Name = "SetActiveRecursivelyDeprecated")]
        [Obsolete("gameObject.SetActiveRecursively() is obsolete. Use GameObject.SetActive(), which is now inherited by children.")]
        public void SetActiveRecursively(bool state);
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("gameObject.StopAnimation is not supported anymore. Use animation.Stop()", true)]
        public void StopAnimation();
        public bool TryGetComponent(Type type, out Component component);
        [SecuritySafeCritical]
        public bool TryGetComponent<T>(out T component);
    }
}

下面是Object類:

namespace UnityEngine
{
    //
    // 摘要:
    //     Base class for all objects Unity can reference.
    [NativeHeader("Runtime/GameCode/CloneObject.h")]
    [NativeHeader("Runtime/Export/Scripting/UnityEngineObject.bindings.h")]
    [NativeHeader("Runtime/SceneManager/SceneManager.h")]
    [RequiredByNativeCode(GenerateProxy = true)]
    public class Object
    {
        public Object();

        //
        // 摘要:
        //     Should the object be hidden, saved with the Scene or modifiable by the user?
        public HideFlags hideFlags { get; set; }
        //
        // 摘要:
        //     The name of the object.
        public string name { get; set; }

        //
        // 摘要:
        //     Removes a GameObject, component or asset.
        //
        // 參數:
        //   obj:
        //     The object to destroy.
        //
        //   t:
        //     The optional amount of time to delay before destroying the object.
        [ExcludeFromDocs]
        public static void Destroy(Object obj);
        //
        // 摘要:
        //     Removes a GameObject, component or asset.
        //
        // 參數:
        //   obj:
        //     The object to destroy.
        //
        //   t:
        //     The optional amount of time to delay before destroying the object.
        [NativeMethod(Name = "Scripting::DestroyObjectFromScripting", IsFreeFunction = true, ThrowsException = true)]
        public static void Destroy(Object obj, [DefaultValue("0.0F")] float t);
        //
        // 摘要:
        //     Destroys the object obj immediately. You are strongly recommended to use Destroy
        //     instead.
        //
        // 參數:
        //   obj:
        //     Object to be destroyed.
        //
        //   allowDestroyingAssets:
        //     Set to true to allow assets to be destroyed.
        [ExcludeFromDocs]
        public static void DestroyImmediate(Object obj);
        //
        // 摘要:
        //     Destroys the object obj immediately. You are strongly recommended to use Destroy
        //     instead.
        //
        // 參數:
        //   obj:
        //     Object to be destroyed.
        //
        //   allowDestroyingAssets:
        //     Set to true to allow assets to be destroyed.
        [NativeMethod(Name = "Scripting::DestroyObjectFromScriptingImmediate", IsFreeFunction = true, ThrowsException = true)]
        public static void DestroyImmediate(Object obj, [DefaultValue("false")] bool allowDestroyingAssets);
        [Obsolete("use Object.Destroy instead.")]
        public static void DestroyObject(Object obj, [DefaultValue("0.0F")] float t);
        [ExcludeFromDocs]
        [Obsolete("use Object.Destroy instead.")]
        public static void DestroyObject(Object obj);
        //
        // 摘要:
        //     Do not destroy the target Object when loading a new Scene.
        //
        // 參數:
        //   target:
        //     An Object not destroyed on Scene change.
        [FreeFunction("GetSceneManager().DontDestroyOnLoad")]
        public static void DontDestroyOnLoad(Object target);
        //
        // 摘要:
        //     Returns the first active loaded object of Type type.
        //
        // 參數:
        //   type:
        //     The type of object to find.
        //
        // 返回結果:
        //     This returns the Object that matches the specified type. It returns null if no
        //     Object matches the type.
        [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)]
        public static Object FindObjectOfType(Type type);
        public static T FindObjectOfType<T>() where T : Object;
        public static T[] FindObjectsOfType<T>() where T : Object;
        //
        // 摘要:
        //     Returns a list of all active loaded objects of Type type.
        //
        // 參數:
        //   type:
        //     The type of object to find.
        //
        //   includeInactive:
        //     If true, components attached to inactive GameObjects are also included.
        //
        // 返回結果:
        //     The array of objects found matching the type specified.
        [FreeFunction("UnityEngineObjectBindings::FindObjectsOfType")]
        [TypeInferenceRule(TypeInferenceRules.ArrayOfTypeReferencedByFirstArgument)]
        public static Object[] FindObjectsOfType(Type type);
        //
        // 摘要:
        //     Returns a list of all active and inactive loaded objects of Type type.
        //
        // 參數:
        //   type:
        //     The type of object to find.
        //
        // 返回結果:
        //     The array of objects found matching the type specified.
        [Obsolete("Please use Resources.FindObjectsOfTypeAll instead")]
        public static Object[] FindObjectsOfTypeAll(Type type);
        //
        // 摘要:
        //     Returns a list of all active and inactive loaded objects of Type type, including
        //     assets.
        //
        // 參數:
        //   type:
        //     The type of object or asset to find.
        //
        // 返回結果:
        //     The array of objects and assets found matching the type specified.
        [FreeFunction("UnityEngineObjectBindings::FindObjectsOfTypeIncludingAssets")]
        [Obsolete("use Resources.FindObjectsOfTypeAll instead.")]
        public static Object[] FindObjectsOfTypeIncludingAssets(Type type);
        [FreeFunction("UnityEngineObjectBindings::FindObjectsOfType")]
        [Obsolete("warning use Object.FindObjectsOfType instead.")]
        public static Object[] FindSceneObjectsOfType(Type type);
        //
        // 摘要:
        //     Clones the object original and returns the clone.
        //
        // 參數:
        //   original:
        //     An existing object that you want to make a copy of.
        //
        //   position:
        //     Position for the new object.
        //
        //   rotation:
        //     Orientation of the new object.
        //
        //   parent:
        //     Parent that will be assigned to the new object.
        //
        //   instantiateInWorldSpace:
        //     When you assign a parent Object, pass true to position the new object directly
        //     in world space. Pass false to set the Object’s position relative to its new parent..
        //
        // 返回結果:
        //     The instantiated clone.
        [TypeInferenceRule(TypeInferenceRules.TypeOfFirstArgument)]
        public static Object Instantiate(Object original);
        //
        // 摘要:
        //     Clones the object original and returns the clone.
        //
        // 參數:
        //   original:
        //     An existing object that you want to make a copy of.
        //
        //   position:
        //     Position for the new object.
        //
        //   rotation:
        //     Orientation of the new object.
        //
        //   parent:
        //     Parent that will be assigned to the new object.
        //
        //   instantiateInWorldSpace:
        //     When you assign a parent Object, pass true to position the new object directly
        //     in world space. Pass false to set the Object’s position relative to its new parent..
        //
        // 返回結果:
        //     The instantiated clone.
        [TypeInferenceRule(TypeInferenceRules.TypeOfFirstArgument)]
        public static Object Instantiate(Object original, Transform parent);
        //
        // 摘要:
        //     Clones the object original and returns the clone.
        //
        // 參數:
        //   original:
        //     An existing object that you want to make a copy of.
        //
        //   position:
        //     Position for the new object.
        //
        //   rotation:
        //     Orientation of the new object.
        //
        //   parent:
        //     Parent that will be assigned to the new object.
        //
        //   instantiateInWorldSpace:
        //     When you assign a parent Object, pass true to position the new object directly
        //     in world space. Pass false to set the Object’s position relative to its new parent..
        //
        // 返回結果:
        //     The instantiated clone.
        [TypeInferenceRule(TypeInferenceRules.TypeOfFirstArgument)]
        public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
        //
        // 摘要:
        //     Clones the object original and returns the clone.
        //
        // 參數:
        //   original:
        //     An existing object that you want to make a copy of.
        //
        //   position:
        //     Position for the new object.
        //
        //   rotation:
        //     Orientation of the new object.
        //
        //   parent:
        //     Parent that will be assigned to the new object.
        //
        //   instantiateInWorldSpace:
        //     When you assign a parent Object, pass true to position the new object directly
        //     in world space. Pass false to set the Object’s position relative to its new parent..
        //
        // 返回結果:
        //     The instantiated clone.
        [TypeInferenceRule(TypeInferenceRules.TypeOfFirstArgument)]
        public static Object Instantiate(Object original, Vector3 position, Quaternion rotation, Transform parent);
        public static T Instantiate<T>(T original, Transform parent, bool worldPositionStays) where T : Object;
        public static T Instantiate<T>(T original, Transform parent) where T : Object;
        public static T Instantiate<T>(T original, Vector3 position, Quaternion rotation, Transform parent) where T : Object;
        public static T Instantiate<T>(T original, Vector3 position, Quaternion rotation) where T : Object;
        public static T Instantiate<T>(T original) where T : Object;
        //
        // 摘要:
        //     Clones the object original and returns the clone.
        //
        // 參數:
        //   original:
        //     An existing object that you want to make a copy of.
        //
        //   position:
        //     Position for the new object.
        //
        //   rotation:
        //     Orientation of the new object.
        //
        //   parent:
        //     Parent that will be assigned to the new object.
        //
        //   instantiateInWorldSpace:
        //     When you assign a parent Object, pass true to position the new object directly
        //     in world space. Pass false to set the Object’s position relative to its new parent..
        //
        // 返回結果:
        //     The instantiated clone.
        [TypeInferenceRule(TypeInferenceRules.TypeOfFirstArgument)]
        public static Object Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);
        public override bool Equals(object other);
        public override int GetHashCode();
        //
        // 摘要:
        //     Returns the instance id of the object.
        [SecuritySafeCritical]
        public int GetInstanceID();
        //
        // 摘要:
        //     Returns the name of the object.
        //
        // 返回結果:
        //     The name returned by ToString.
        public override string ToString();

        public static bool operator ==(Object x, Object y);
        public static bool operator !=(Object x, Object y);

        public static implicit operator bool(Object exists);
    }
}

1.GameObject中的成員變量

name:游戲物體名稱;activeSelf:是否激活;isStatic:是否靜態對象;layer:層級;tag:標簽;transform:位置信息對象(和物體上組件中繼承自Component類的transform對象是同一個對象)

    void Start()
    {
        print(transform.GetHashCode());
        print(gameObject.transform.GetHashCode());
    }

2.靜態方法

GameObject中的靜態方法:

Object中的靜態方法:

 Object類中大多數方法都是靜態方法,Component類中沒有提供靜態方法,但是根據繼承關系,從Object類中繼承了這些靜態方法。下面是這些靜態方法的分類說明:

1)使用CreatePrimitive方法根據系統自帶的游戲物體模板創建游戲物體,這個方法由GameObject類提供。

    void Start()
    {
        //靜態方法創建系統自帶的幾何體,傳入的參數是枚舉類型
        GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
        //可以修改物體的名稱等變量
        go.name = "MyCube";
        go.tag = "MyCube";
    }

2)查找游戲物體的Find系列方法

GameObject類提供的方法:

通過名稱查找:Find方法;通過標簽查找:其他Find方法。

Object類(這個Object和萬物之父Object只是同名,所在命名空間並不相同,Unity中的Object繼承自System中的萬物之父Object)提供的方法:

 Object類中的Find方法基本都是根據類型查找,GameObject類中的Find方法是根據名稱和標簽查找。使用時一般使用泛型查找方法。

說明:在GameObject類中的Find系列方法啊,會遍歷場景中所有對象,查找效率比較低下;Object類中的Find方法是通過類型查找,這個類型一般是腳本類型,因此不僅遍歷對象,還會遍歷對象身上的腳本,效率更低。這兩個類中的Find方法在實際使用中都不推薦。

3)克隆對象(實例化對象)的方法

 Instantiate方法是Object類提供的方法,一般也使用泛型方法,一般用於使用預制體克隆(實例化)對象。

4)刪除對象的方法

 刪除對象的方法也是Object基類提供的方法。一般上圖中第一個方法和第五個方法用得比較多,第五個方法是延遲刪除方法。Destroy方法會在下一幀移除游戲物體或者腳本(參數是Object類對象,代表繼承自Object的類對象都可以移除),不會馬上移除對象,如果要馬上移除,可以使用上面的第三個和第四個方法(DestroyImmediate方法)。

5)過場景不移除

 這個方法也是由Object類提供,在切換場景時會自動移除上一個場景中的游戲物體,使用這個方法指定的target在切換場景時不會被移除。

PS:因為MonoBehaviour繼承自Behaviour,Behaviour繼承自Component,Component繼承自Object,Component和GameObject是“兄弟類”,所以繼承自Object的靜態方法(如根據類型的Find系列方法、Instantiate系列方法、Destroy系列方法、DontDestroyOnLoad方法等)在繼承自MonoBehaviour的腳本中也存在,直接寫方法名就可以調用,而GameObject類提供的靜態方法(如根據名字的Find方法、根據標簽的Find系列方法等)在繼承了MonoBehaviour的腳本中則需要使用GameObject類名點出方法名來調用。

3.成員方法

1)創建一個物體

 GameObject類中提供了三種構造方法,因此可以使用new關鍵字創建空物體,在創建物體時還可以指定物體的名稱和掛載的組件。

    void Start()
    {
        GameObject go = new GameObject("NewCamera", typeof(Camera), typeof(Rigidbody));
    }

 2)添加腳本

 使用AddComponent方法添加腳本,一般使用泛型,這個方法是GameObject類提供的成員方法,一般使用gameObject對象調用。

3)獲取腳本

 獲取腳本的方法是由Component類提供,GameObject類也提供了這些方法,但是Object類中沒有,一般直接由腳本或游戲物體對象調用。

同時還有TryGetComponent方法,返回值為bool類型,代表是否成功獲取組件,使用out參數接收獲取的組件。

4)標簽比較

 可以使用CompareTag方法比較兩個游戲物體的標簽,也可以通過gameObject對象中的tag變量獲得標簽,tag變量是字符串類型,使用字符串的比較方法(==運算符或者重載Equals方法)比較標簽是否相等。GameObject類和Component類都提供了這個方法。

5)設置游戲中對象失活或者激活

 這個方法由GameObject類提供,通過GameObject類對象調用,設置游戲對象是否失活。

    void Start()
    {
        GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Capsule);
        obj.SetActive(false);
    }

 6)不建議使用的成員方法(了解即可,效率低)

 SendMessage系列方法由GameObject類提供,Component類也提供了這類方法,可以廣播方法讓其他腳本執行這個方法,但是這個方法會遍歷所有腳本,還涉及到裝箱拆箱等(傳入了object類型參數),效率低。

 BroadCastMessage系列方法也是GameObject類提供的方法,同樣Component類中也有這一系列方法,和SendMessage類似,效率低,不建議使用。

三.Time類(時間相關類)

下面是Time類:

namespace UnityEngine
{
    //
    // 摘要:
    //     The interface to get time information from Unity.
    [NativeHeader("Runtime/Input/TimeManager.h")]
    [StaticAccessor("GetTimeManager()", StaticAccessorType.Dot)]
    public class Time
    {
        public Time();

        //
        // 摘要:
        //     Slows game playback time to allow screenshots to be saved between frames.
        public static float captureDeltaTime { get; set; }
        //
        // 摘要:
        //     The real time in seconds since the game started (Read Only).
        [NativeProperty("Realtime")]
        public static float realtimeSinceStartup { get; }
        [NativeProperty("RenderFrameCount")]
        public static int renderedFrameCount { get; }
        //
        // 摘要:
        //     The total number of frames that have passed (Read Only).
        public static int frameCount { get; }
        //
        // 摘要:
        //     The scale at which time passes. This can be used for slow motion effects.
        public static float timeScale { get; set; }
        //
        // 摘要:
        //     The maximum time a frame can spend on particle updates. If the frame takes longer
        //     than this, then updates are split into multiple smaller updates.
        public static float maximumParticleDeltaTime { get; set; }
        //
        // 摘要:
        //     A smoothed out Time.deltaTime (Read Only).
        public static float smoothDeltaTime { get; }
        //
        // 摘要:
        //     The maximum time a frame can take. Physics and other fixed frame rate updates
        //     (like MonoBehaviour's MonoBehaviour.FixedUpdate) will be performed only for this
        //     duration of time per frame.
        public static float maximumDeltaTime { get; set; }
        //
        // 摘要:
        //     The interval in seconds at which physics and other fixed frame rate updates (like
        //     MonoBehaviour's MonoBehaviour.FixedUpdate) are performed.
        public static float fixedDeltaTime { get; set; }
        //
        // 摘要:
        //     The timeScale-independent interval in seconds from the last fixed frame to the
        //     current one (Read Only).
        public static float fixedUnscaledDeltaTime { get; }
        //
        // 摘要:
        //     The timeScale-independent interval in seconds from the last frame to the current
        //     one (Read Only).
        public static float unscaledDeltaTime { get; }
        //
        // 摘要:
        //     The TimeScale-independant time the latest MonoBehaviour.FixedUpdate has started
        //     (Read Only). This is the time in seconds since the start of the game.
        public static float fixedUnscaledTime { get; }
        //
        // 摘要:
        //     The timeScale-independant time for this frame (Read Only). This is the time in
        //     seconds since the start of the game.
        public static float unscaledTime { get; }
        //
        // 摘要:
        //     The time the latest MonoBehaviour.FixedUpdate has started (Read Only). This is
        //     the time in seconds since the start of the game.
        public static float fixedTime { get; }
        //
        // 摘要:
        //     The completion time in seconds since the last frame (Read Only).
        public static float deltaTime { get; }
        //
        // 摘要:
        //     The time this frame has started (Read Only). This is the time in seconds since
        //     the last level has been loaded.
        [NativeProperty("TimeSinceSceneLoad")]
        public static float timeSinceLevelLoad { get; }
        //
        // 摘要:
        //     The time at the beginning of this frame (Read Only). This is the time in seconds
        //     since the start of the game.
        [NativeProperty("CurTime")]
        public static float time { get; }
        //
        // 摘要:
        //     The reciprocal of Time.captureDeltaTime.
        public static int captureFramerate { get; set; }
        //
        // 摘要:
        //     Returns true if called inside a fixed time step callback (like MonoBehaviour's
        //     MonoBehaviour.FixedUpdate), otherwise returns false.
        public static bool inFixedTimeStep { get; }
    }
}

可以看到,Time類中的內容都是一些靜態屬性,接下來我們分類說明這些屬性:

1.時間縮放比例

 這個屬性用於控制游戲內時間速度,這個值為0時游戲暫停,為1時游戲內時間流速和真實事件相同,為0~1之間時游戲內時間比真實時間流動速度慢,大於1時游戲內時間流速快於真實時間。可以簡單理解為幾倍速播放。

2.幀時間

 執行當前代碼的最近一幀的時間,單位秒。一般用於計算位移。在Update中使用。

 不受scale影響的幀時間,和deltaTime相比,這個時間和timeScale無關,一般不受游戲暫停影響的游戲物體位移使用這個屬性。

3.游戲開始時間

 從游戲開始到現在的時間。

 不受scale影響的游戲時間。

4.物理幀時間

 在FixedUpdate中使用的物理幀時間。這個時間可以設置,游戲運行時不會變化。

 不受scale影響的物理幀時間。

5.游戲開始后總共多少幀

 計算游戲開始后總共跑了多少次循環(總幀數)。一般在幀同步時使用。

四.Vector3結構體

namespace UnityEngine
{
    //
    // 摘要:
    //     Representation of 3D vectors and points.
    [DefaultMember("Item")]
    [NativeClass("Vector3f")]
    [NativeHeader("Runtime/Math/Vector3.h")]
    [NativeHeader("Runtime/Math/MathScripting.h")]
    [NativeType(Header = "Runtime/Math/Vector3.h")]
    [RequiredByNativeCode(Optional = true, GenerateProxy = true)]
    public struct Vector3 : IEquatable<Vector3>
    {
        public const float kEpsilon = 1E-05F;
        public const float kEpsilonNormalSqrt = 1E-15F;
        //
        // 摘要:
        //     X component of the vector.
        public float x;
        //
        // 摘要:
        //     Y component of the vector.
        public float y;
        //
        // 摘要:
        //     Z component of the vector.
        public float z;

        //
        // 摘要:
        //     Creates a new vector with given x, y components and sets z to zero.
        //
        // 參數:
        //   x:
        //
        //   y:
        public Vector3(float x, float y);
        //
        // 摘要:
        //     Creates a new vector with given x, y, z components.
        //
        // 參數:
        //   x:
        //
        //   y:
        //
        //   z:
        public Vector3(float x, float y, float z);

        public float this[int index] { get; set; }

        //
        // 摘要:
        //     Shorthand for writing Vector3(1, 0, 0).
        public static Vector3 right { get; }
        //
        // 摘要:
        //     Shorthand for writing Vector3(-1, 0, 0).
        public static Vector3 left { get; }
        //
        // 摘要:
        //     Shorthand for writing Vector3(0, 1, 0).
        public static Vector3 up { get; }
        //
        // 摘要:
        //     Shorthand for writing Vector3(0, 0, -1).
        public static Vector3 back { get; }
        //
        // 摘要:
        //     Shorthand for writing Vector3(0, 0, 1).
        public static Vector3 forward { get; }
        //
        // 摘要:
        //     Shorthand for writing Vector3(1, 1, 1).
        public static Vector3 one { get; }
        //
        // 摘要:
        //     Shorthand for writing Vector3(0, 0, 0).
        public static Vector3 zero { get; }
        //
        // 摘要:
        //     Shorthand for writing Vector3(float.NegativeInfinity, float.NegativeInfinity,
        //     float.NegativeInfinity).
        public static Vector3 negativeInfinity { get; }
        //
        // 摘要:
        //     Shorthand for writing Vector3(float.PositiveInfinity, float.PositiveInfinity,
        //     float.PositiveInfinity).
        public static Vector3 positiveInfinity { get; }
        //
        // 摘要:
        //     Shorthand for writing Vector3(0, -1, 0).
        public static Vector3 down { get; }
        [Obsolete("Use Vector3.forward instead.")]
        public static Vector3 fwd { get; }
        //
        // 摘要:
        //     Returns the squared length of this vector (Read Only).
        public float sqrMagnitude { get; }
        //
        // 摘要:
        //     Returns this vector with a magnitude of 1 (Read Only).
        public Vector3 normalized { get; }
        //
        // 摘要:
        //     Returns the length of this vector (Read Only).
        public float magnitude { get; }

        //
        // 摘要:
        //     Returns the angle in degrees between from and to.
        //
        // 參數:
        //   from:
        //     The vector from which the angular difference is measured.
        //
        //   to:
        //     The vector to which the angular difference is measured.
        //
        // 返回結果:
        //     The angle in degrees between the two vectors.
        public static float Angle(Vector3 from, Vector3 to);
        [Obsolete("Use Vector3.Angle instead. AngleBetween uses radians instead of degrees and was deprecated for this reason")]
        public static float AngleBetween(Vector3 from, Vector3 to);
        //
        // 摘要:
        //     Returns a copy of vector with its magnitude clamped to maxLength.
        //
        // 參數:
        //   vector:
        //
        //   maxLength:
        public static Vector3 ClampMagnitude(Vector3 vector, float maxLength);
        //
        // 摘要:
        //     Cross Product of two vectors.
        //
        // 參數:
        //   lhs:
        //
        //   rhs:
        public static Vector3 Cross(Vector3 lhs, Vector3 rhs);
        //
        // 摘要:
        //     Returns the distance between a and b.
        //
        // 參數:
        //   a:
        //
        //   b:
        public static float Distance(Vector3 a, Vector3 b);
        //
        // 摘要:
        //     Dot Product of two vectors.
        //
        // 參數:
        //   lhs:
        //
        //   rhs:
        public static float Dot(Vector3 lhs, Vector3 rhs);
        [Obsolete("Use Vector3.ProjectOnPlane instead.")]
        public static Vector3 Exclude(Vector3 excludeThis, Vector3 fromThat);
        //
        // 摘要:
        //     Linearly interpolates between two points.
        //
        // 參數:
        //   a:
        //
        //   b:
        //
        //   t:
        public static Vector3 Lerp(Vector3 a, Vector3 b, float t);
        //
        // 摘要:
        //     Linearly interpolates between two vectors.
        //
        // 參數:
        //   a:
        //
        //   b:
        //
        //   t:
        public static Vector3 LerpUnclamped(Vector3 a, Vector3 b, float t);
        public static float Magnitude(Vector3 vector);
        //
        // 摘要:
        //     Returns a vector that is made from the largest components of two vectors.
        //
        // 參數:
        //   lhs:
        //
        //   rhs:
        public static Vector3 Max(Vector3 lhs, Vector3 rhs);
        //
        // 摘要:
        //     Returns a vector that is made from the smallest components of two vectors.
        //
        // 參數:
        //   lhs:
        //
        //   rhs:
        public static Vector3 Min(Vector3 lhs, Vector3 rhs);
        //
        // 摘要:
        //     Calculate a position between the points specified by current and target, moving
        //     no farther than the distance specified by maxDistanceDelta.
        //
        // 參數:
        //   current:
        //     The position to move from.
        //
        //   target:
        //     The position to move towards.
        //
        //   maxDistanceDelta:
        //     Distance to move current per call.
        //
        // 返回結果:
        //     The new position.
        public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta);
        //
        // 摘要:
        //     Makes this vector have a magnitude of 1.
        //
        // 參數:
        //   value:
        public static Vector3 Normalize(Vector3 value);
        public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent, ref Vector3 binormal);
        public static void OrthoNormalize(ref Vector3 normal, ref Vector3 tangent);
        //
        // 摘要:
        //     Projects a vector onto another vector.
        //
        // 參數:
        //   vector:
        //
        //   onNormal:
        public static Vector3 Project(Vector3 vector, Vector3 onNormal);
        //
        // 摘要:
        //     Projects a vector onto a plane defined by a normal orthogonal to the plane.
        //
        // 參數:
        //   planeNormal:
        //     The direction from the vector towards the plane.
        //
        //   vector:
        //     The location of the vector above the plane.
        //
        // 返回結果:
        //     The location of the vector on the plane.
        public static Vector3 ProjectOnPlane(Vector3 vector, Vector3 planeNormal);
        //
        // 摘要:
        //     Reflects a vector off the plane defined by a normal.
        //
        // 參數:
        //   inDirection:
        //
        //   inNormal:
        public static Vector3 Reflect(Vector3 inDirection, Vector3 inNormal);
        //
        // 摘要:
        //     Rotates a vector current towards target.
        //
        // 參數:
        //   current:
        //     The vector being managed.
        //
        //   target:
        //     The vector.
        //
        //   maxRadiansDelta:
        //     The maximum angle in radians allowed for this rotation.
        //
        //   maxMagnitudeDelta:
        //     The maximum allowed change in vector magnitude for this rotation.
        //
        // 返回結果:
        //     The location that RotateTowards generates.
        [FreeFunction(IsThreadSafe = true)]
        public static Vector3 RotateTowards(Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta);
        //
        // 摘要:
        //     Multiplies two vectors component-wise.
        //
        // 參數:
        //   a:
        //
        //   b:
        public static Vector3 Scale(Vector3 a, Vector3 b);
        //
        // 摘要:
        //     Returns the signed angle in degrees between from and to.
        //
        // 參數:
        //   from:
        //     The vector from which the angular difference is measured.
        //
        //   to:
        //     The vector to which the angular difference is measured.
        //
        //   axis:
        //     A vector around which the other vectors are rotated.
        public static float SignedAngle(Vector3 from, Vector3 to, Vector3 axis);
        //
        // 摘要:
        //     Spherically interpolates between two vectors.
        //
        // 參數:
        //   a:
        //
        //   b:
        //
        //   t:
        [FreeFunction("VectorScripting::Slerp", IsThreadSafe = true)]
        public static Vector3 Slerp(Vector3 a, Vector3 b, float t);
        //
        // 摘要:
        //     Spherically interpolates between two vectors.
        //
        // 參數:
        //   a:
        //
        //   b:
        //
        //   t:
        [FreeFunction("VectorScripting::SlerpUnclamped", IsThreadSafe = true)]
        public static Vector3 SlerpUnclamped(Vector3 a, Vector3 b, float t);
        public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, [DefaultValue("Mathf.Infinity")] float maxSpeed, [DefaultValue("Time.deltaTime")] float deltaTime);
        [ExcludeFromDocs]
        public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed);
        [ExcludeFromDocs]
        public static Vector3 SmoothDamp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime);
        public static float SqrMagnitude(Vector3 vector);
        public bool Equals(Vector3 other);
        //
        // 摘要:
        //     Returns true if the given vector is exactly equal to this vector.
        //
        // 參數:
        //   other:
        public override bool Equals(object other);
        public override int GetHashCode();
        public void Normalize();
        //
        // 摘要:
        //     Multiplies every component of this vector by the same component of scale.
        //
        // 參數:
        //   scale:
        public void Scale(Vector3 scale);
        //
        // 摘要:
        //     Set x, y and z components of an existing Vector3.
        //
        // 參數:
        //   newX:
        //
        //   newY:
        //
        //   newZ:
        public void Set(float newX, float newY, float newZ);
        //
        // 摘要:
        //     Returns a nicely formatted string for this vector.
        //
        // 參數:
        //   format:
        public string ToString(string format);
        //
        // 摘要:
        //     Returns a nicely formatted string for this vector.
        //
        // 參數:
        //   format:
        public override string ToString();

        public static Vector3 operator +(Vector3 a, Vector3 b);
        public static Vector3 operator -(Vector3 a);
        public static Vector3 operator -(Vector3 a, Vector3 b);
        public static Vector3 operator *(Vector3 a, float d);
        public static Vector3 operator *(float d, Vector3 a);
        public static Vector3 operator /(Vector3 a, float d);
        public static bool operator ==(Vector3 lhs, Vector3 rhs);
        public static bool operator !=(Vector3 lhs, Vector3 rhs);
    }
}

這個結構體是用於表示三維向量的結構體。

1.初始化

可以使用結構體提供的無參構造,同時也提供了兩個構造函數重載:

 提供了x和y的構造函數默認z是0,一般用於表示二維向量(當然Unity也提供了Vector2結構體)。

2.有3個變量,代表向量或位置坐標的三個坐標

 3.提供了索引器訪問坐標和各種靜態屬性

 4.提供向量的運算符重載

 5.提供計算模長平方、向量單位化和取模長的方法

 6.提供了各種其他數學向量運算方法

 五.Transform類

namespace UnityEngine
{
    //
    // 摘要:
    //     Position, rotation and scale of an object.
    [NativeHeader("Runtime/Transform/Transform.h")]
    [NativeHeader("Runtime/Transform/ScriptBindings/TransformScriptBindings.h")]
    [NativeHeader("Configuration/UnityConfigure.h")]
    [RequiredByNativeCode]
    public class Transform : Component, IEnumerable
    {
        protected Transform();

        //
        // 摘要:
        //     Position of the transform relative to the parent transform.
        public Vector3 localPosition { get; set; }
        //
        // 摘要:
        //     The rotation as Euler angles in degrees.
        public Vector3 eulerAngles { get; set; }
        //
        // 摘要:
        //     The rotation as Euler angles in degrees relative to the parent transform's rotation.
        public Vector3 localEulerAngles { get; set; }
        //
        // 摘要:
        //     The red axis of the transform in world space.
        public Vector3 right { get; set; }
        //
        // 摘要:
        //     The green axis of the transform in world space.
        public Vector3 up { get; set; }
        //
        // 摘要:
        //     Returns a normalized vector representing the blue axis of the transform in world
        //     space.
        public Vector3 forward { get; set; }
        //
        // 摘要:
        //     A Quaternion that stores the rotation of the Transform in world space.
        public Quaternion rotation { get; set; }
        //
        // 摘要:
        //     The world space position of the Transform.
        public Vector3 position { get; set; }
        //
        // 摘要:
        //     The rotation of the transform relative to the transform rotation of the parent.
        public Quaternion localRotation { get; set; }
        //
        // 摘要:
        //     The parent of the transform.
        public Transform parent { get; set; }
        //
        // 摘要:
        //     Matrix that transforms a point from world space into local space (Read Only).
        public Matrix4x4 worldToLocalMatrix { get; }
        //
        // 摘要:
        //     Matrix that transforms a point from local space into world space (Read Only).
        public Matrix4x4 localToWorldMatrix { get; }
        //
        // 摘要:
        //     Returns the topmost transform in the hierarchy.
        public Transform root { get; }
        //
        // 摘要:
        //     The number of children the parent Transform has.
        public int childCount { get; }
        //
        // 摘要:
        //     The global scale of the object (Read Only).
        public Vector3 lossyScale { get; }
        //
        // 摘要:
        //     Has the transform changed since the last time the flag was set to 'false'?
        [NativeProperty("HasChangedDeprecated")]
        public bool hasChanged { get; set; }
        //
        // 摘要:
        //     The scale of the transform relative to the GameObjects parent.
        public Vector3 localScale { get; set; }
        //
        // 摘要:
        //     The transform capacity of the transform's hierarchy data structure.
        public int hierarchyCapacity { get; set; }
        //
        // 摘要:
        //     The number of transforms in the transform's hierarchy data structure.
        public int hierarchyCount { get; }

        //
        // 摘要:
        //     Unparents all children.
        [FreeFunction("DetachChildren", HasExplicitThis = true)]
        public void DetachChildren();
        //
        // 摘要:
        //     Finds a child by n and returns it.
        //
        // 參數:
        //   n:
        //     Name of child to be found.
        //
        // 返回結果:
        //     The returned child transform or null if no child is found.
        public Transform Find(string n);
        [Obsolete("FindChild has been deprecated. Use Find instead (UnityUpgradable) -> Find([mscorlib] System.String)", false)]
        public Transform FindChild(string n);
        //
        // 摘要:
        //     Returns a transform child by index.
        //
        // 參數:
        //   index:
        //     Index of the child transform to return. Must be smaller than Transform.childCount.
        //
        // 返回結果:
        //     Transform child by index.
        [FreeFunction("GetChild", HasExplicitThis = true)]
        [NativeThrows]
        public Transform GetChild(int index);
        [NativeMethod("GetChildrenCount")]
        [Obsolete("warning use Transform.childCount instead (UnityUpgradable) -> Transform.childCount", false)]
        public int GetChildCount();
        public IEnumerator GetEnumerator();
        //
        // 摘要:
        //     Gets the sibling index.
        public int GetSiblingIndex();
        //
        // 摘要:
        //     Transforms a direction from world space to local space. The opposite of Transform.TransformDirection.
        //
        // 參數:
        //   direction:
        public Vector3 InverseTransformDirection(Vector3 direction);
        //
        // 摘要:
        //     Transforms the direction x, y, z from world space to local space. The opposite
        //     of Transform.TransformDirection.
        //
        // 參數:
        //   x:
        //
        //   y:
        //
        //   z:
        public Vector3 InverseTransformDirection(float x, float y, float z);
        //
        // 摘要:
        //     Transforms the position x, y, z from world space to local space. The opposite
        //     of Transform.TransformPoint.
        //
        // 參數:
        //   x:
        //
        //   y:
        //
        //   z:
        public Vector3 InverseTransformPoint(float x, float y, float z);
        //
        // 摘要:
        //     Transforms position from world space to local space.
        //
        // 參數:
        //   position:
        public Vector3 InverseTransformPoint(Vector3 position);
        //
        // 摘要:
        //     Transforms a vector from world space to local space. The opposite of Transform.TransformVector.
        //
        // 參數:
        //   vector:
        public Vector3 InverseTransformVector(Vector3 vector);
        //
        // 摘要:
        //     Transforms the vector x, y, z from world space to local space. The opposite of
        //     Transform.TransformVector.
        //
        // 參數:
        //   x:
        //
        //   y:
        //
        //   z:
        public Vector3 InverseTransformVector(float x, float y, float z);
        //
        // 摘要:
        //     Is this transform a child of parent?
        //
        // 參數:
        //   parent:
        [FreeFunction("Internal_IsChildOrSameTransform", HasExplicitThis = true)]
        public bool IsChildOf([NotNull] Transform parent);
        //
        // 摘要:
        //     Rotates the transform so the forward vector points at target's current position.
        //
        // 參數:
        //   target:
        //     Object to point towards.
        //
        //   worldUp:
        //     Vector specifying the upward direction.
        public void LookAt(Transform target, [DefaultValue("Vector3.up")] Vector3 worldUp);
        //
        // 摘要:
        //     Rotates the transform so the forward vector points at worldPosition.
        //
        // 參數:
        //   worldPosition:
        //     Point to look at.
        //
        //   worldUp:
        //     Vector specifying the upward direction.
        public void LookAt(Vector3 worldPosition, [DefaultValue("Vector3.up")] Vector3 worldUp);
        //
        // 摘要:
        //     Rotates the transform so the forward vector points at worldPosition.
        //
        // 參數:
        //   worldPosition:
        //     Point to look at.
        //
        //   worldUp:
        //     Vector specifying the upward direction.
        public void LookAt(Vector3 worldPosition);
        //
        // 摘要:
        //     Rotates the transform so the forward vector points at target's current position.
        //
        // 參數:
        //   target:
        //     Object to point towards.
        //
        //   worldUp:
        //     Vector specifying the upward direction.
        public void LookAt(Transform target);
        public void Rotate(float xAngle, float yAngle, float zAngle);
        //
        // 摘要:
        //     Applies a rotation of eulerAngles.z degrees around the z-axis, eulerAngles.x
        //     degrees around the x-axis, and eulerAngles.y degrees around the y-axis (in that
        //     order).
        //
        // 參數:
        //   eulers:
        //     The rotation to apply.
        //
        //   relativeTo:
        //     Determines whether to rotate the GameObject either locally to the GameObject
        //     or relative to the Scene in world space.
        public void Rotate(Vector3 eulers, [DefaultValue("Space.Self")] Space relativeTo);
        public void Rotate(Vector3 eulers);
        //
        // 摘要:
        //     The implementation of this method applies a rotation of zAngle degrees around
        //     the z axis, xAngle degrees around the x axis, and yAngle degrees around the y
        //     axis (in that order).
        //
        // 參數:
        //   relativeTo:
        //     Determines whether to rotate the GameObject either locally to the GameObject
        //     or relative to the Scene in world space.
        //
        //   xAngle:
        //     Degrees to rotate the GameObject around the X axis.
        //
        //   yAngle:
        //     Degrees to rotate the GameObject around the Y axis.
        //
        //   zAngle:
        //     Degrees to rotate the GameObject around the Z axis.
        public void Rotate(float xAngle, float yAngle, float zAngle, [DefaultValue("Space.Self")] Space relativeTo);
        //
        // 摘要:
        //     Rotates the object around the given axis by the number of degrees defined by
        //     the given angle.
        //
        // 參數:
        //   angle:
        //     The degrees of rotation to apply.
        //
        //   axis:
        //     The axis to apply rotation to.
        //
        //   relativeTo:
        //     Determines whether to rotate the GameObject either locally to the GameObject
        //     or relative to the Scene in world space.
        public void Rotate(Vector3 axis, float angle, [DefaultValue("Space.Self")] Space relativeTo);
        public void Rotate(Vector3 axis, float angle);
        //
        // 摘要:
        //     Rotates the transform about axis passing through point in world coordinates by
        //     angle degrees.
        //
        // 參數:
        //   point:
        //
        //   axis:
        //
        //   angle:
        public void RotateAround(Vector3 point, Vector3 axis, float angle);
        //
        // 參數:
        //   axis:
        //
        //   angle:
        [Obsolete("warning use Transform.Rotate instead.")]
        public void RotateAround(Vector3 axis, float angle);
        [Obsolete("warning use Transform.Rotate instead.")]
        public void RotateAroundLocal(Vector3 axis, float angle);
        //
        // 摘要:
        //     Move the transform to the start of the local transform list.
        public void SetAsFirstSibling();
        //
        // 摘要:
        //     Move the transform to the end of the local transform list.
        public void SetAsLastSibling();
        //
        // 摘要:
        //     Set the parent of the transform.
        //
        // 參數:
        //   parent:
        //     The parent Transform to use.
        //
        //   worldPositionStays:
        //     If true, the parent-relative position, scale and rotation are modified such that
        //     the object keeps the same world space position, rotation and scale as before.
        //
        //   p:
        public void SetParent(Transform p);
        //
        // 摘要:
        //     Set the parent of the transform.
        //
        // 參數:
        //   parent:
        //     The parent Transform to use.
        //
        //   worldPositionStays:
        //     If true, the parent-relative position, scale and rotation are modified such that
        //     the object keeps the same world space position, rotation and scale as before.
        //
        //   p:
        [FreeFunction("SetParent", HasExplicitThis = true)]
        public void SetParent(Transform parent, bool worldPositionStays);
        //
        // 摘要:
        //     Sets the world space position and rotation of the Transform component.
        //
        // 參數:
        //   position:
        //
        //   rotation:
        public void SetPositionAndRotation(Vector3 position, Quaternion rotation);
        //
        // 摘要:
        //     Sets the sibling index.
        //
        // 參數:
        //   index:
        //     Index to set.
        public void SetSiblingIndex(int index);
        //
        // 摘要:
        //     Transforms direction x, y, z from local space to world space.
        //
        // 參數:
        //   x:
        //
        //   y:
        //
        //   z:
        public Vector3 TransformDirection(float x, float y, float z);
        //
        // 摘要:
        //     Transforms direction from local space to world space.
        //
        // 參數:
        //   direction:
        public Vector3 TransformDirection(Vector3 direction);
        //
        // 摘要:
        //     Transforms the position x, y, z from local space to world space.
        //
        // 參數:
        //   x:
        //
        //   y:
        //
        //   z:
        public Vector3 TransformPoint(float x, float y, float z);
        //
        // 摘要:
        //     Transforms position from local space to world space.
        //
        // 參數:
        //   position:
        public Vector3 TransformPoint(Vector3 position);
        //
        // 摘要:
        //     Transforms vector x, y, z from local space to world space.
        //
        // 參數:
        //   x:
        //
        //   y:
        //
        //   z:
        public Vector3 TransformVector(float x, float y, float z);
        //
        // 摘要:
        //     Transforms vector from local space to world space.
        //
        // 參數:
        //   vector:
        public Vector3 TransformVector(Vector3 vector);
        //
        // 摘要:
        //     Moves the transform by x along the x axis, y along the y axis, and z along the
        //     z axis.
        //
        // 參數:
        //   x:
        //
        //   y:
        //
        //   z:
        //
        //   relativeTo:
        public void Translate(float x, float y, float z);
        //
        // 摘要:
        //     Moves the transform by x along the x axis, y along the y axis, and z along the
        //     z axis.
        //
        // 參數:
        //   x:
        //
        //   y:
        //
        //   z:
        //
        //   relativeTo:
        public void Translate(float x, float y, float z, [DefaultValue("Space.Self")] Space relativeTo);
        //
        // 摘要:
        //     Moves the transform in the direction and distance of translation.
        //
        // 參數:
        //   translation:
        //
        //   relativeTo:
        public void Translate(Vector3 translation);
        //
        // 摘要:
        //     Moves the transform in the direction and distance of translation.
        //
        // 參數:
        //   translation:
        //
        //   relativeTo:
        public void Translate(Vector3 translation, [DefaultValue("Space.Self")] Space relativeTo);
        //
        // 摘要:
        //     Moves the transform by x along the x axis, y along the y axis, and z along the
        //     z axis.
        //
        // 參數:
        //   x:
        //
        //   y:
        //
        //   z:
        //
        //   relativeTo:
        public void Translate(float x, float y, float z, Transform relativeTo);
        //
        // 摘要:
        //     Moves the transform in the direction and distance of translation.
        //
        // 參數:
        //   translation:
        //
        //   relativeTo:
        public void Translate(Vector3 translation, Transform relativeTo);
    }
}

下面是常用的一些方法和屬性:

 世界坐標系中的位置。position只能使用Vector3向量賦值,不能使用某個數字賦值某個坐標(如transform.position.x = 1;是不被允許的)

 本地坐標系的位置(相對於父對象的位置,沒有父對象,就以世界坐標原點作為本地坐標原點。)。同樣只能使用Vector3向量賦值。

 使用Translate函數進行位移計算。中間兩個Translate函數運用較多。默認是局部坐標系運算,可以使用第二個參數指定世界坐標系運算位移。

 

 表示物體朝向(從標准朝向(朝向z軸)旋轉到當前朝向的旋轉)使用的是四元數結構體Quaternion,不是Vector3結構體,一般對物體朝向賦值都不是給rotation賦值(四元數計算太麻煩)。

 可以使用歐拉角賦值物體的朝向。和position相同,只能使用Vector3對象給歐拉角賦值,不能單獨改變其中某一個坐標的值。歐拉角的值雖然在inspector面板中顯示有負數,但是通過代碼獲取的角度只能是0~360的范圍。

 使用Rotate函數進行旋轉計算。

 使用RotateAround函數圍繞點旋轉。后兩個方法已經棄用。Rotate可以理解為自傳函數,RotateAround可以理解為公轉函數。

 相對於世界坐標的縮放大小和本地坐標縮放大小屬性。注意:lossyScale屬性只提供了get方法,沒有set方法。修改時同樣只能使用Vector3賦值,不能為某個單獨的坐標賦值。Unity沒有提供縮放的方法,只能通過賦值使縮放變化.

LookAt函數能夠使物體的前方一直朝向某個點或對象。

parent屬性對應了當前Transform所在物體的父物體的transform對象,提供了get和set方法,可以設置父物體。

也可以通過SetParent方法設置父物體,bool參數代表是否保留世界坐標位置、角度、縮放等信息。

使用DetachChildren方法拋棄所有子物體。

通過名稱查找子對象的transform組件,其中FindChild方法已棄用。只能找一層子對象,不能找孫子輩的子對象。

得到所有子對象的數量,失活的子對象也會被計算在內,但是孫子輩的子對象不計算在內。

通過索引值得到相應的子對象,配合子對象的數量GetChildCount函數可以遍歷所有子對象。

判斷是否是子對象的方法。

得到作為子對象的索引。

設定自己作為子對象的索引值為第一個或最后一個。

設置作為子對象的編號,如果給定的編號溢出,始終會被設置為最后一個(不管正溢出還是負溢出)。

坐標轉換相關的函數,這六個方法都是世界坐標系轉本地坐標系,分別是兩個轉換方向的重載、兩個轉換點坐標的重載、兩個轉換方向的重載(不受縮放影響)。

同樣的,這六個方法是本地坐標下轉世界坐標系。

六.Input類

  Input類沒有繼承任何類,是一個用於檢測鼠標輸入的類,這個類中除了一個無參構造以外其他方法和屬性都是靜態的。下面是這個類的元數據:

namespace UnityEngine
{
    //
    // 摘要:
    //     Interface into the Input system.
    [NativeHeader("Runtime/Input/InputBindings.h")]
    public class Input
    {
        public Input();

        //
        // 摘要:
        //     Returns the keyboard input entered this frame. (Read Only)
        [NativeThrows]
        public static string inputString { get; }
        //
        // 摘要:
        //     Controls enabling and disabling of IME input composition.
        public static IMECompositionMode imeCompositionMode { get; set; }
        //
        // 摘要:
        //     The current IME composition string being typed by the user.
        public static string compositionString { get; }
        //
        // 摘要:
        //     Does the user have an IME keyboard input source selected?
        public static bool imeIsSelected { get; }
        //
        // 摘要:
        //     The current text input position used by IMEs to open windows.
        public static Vector2 compositionCursorPos { get; set; }
        //
        // 摘要:
        //     Property indicating whether keypresses are eaten by a textinput if it has focus
        //     (default true).
        [Obsolete("eatKeyPressOnTextFieldFocus property is deprecated, and only provided to support legacy behavior.")]
        public static bool eatKeyPressOnTextFieldFocus { get; set; }
        //
        // 摘要:
        //     Indicates if a mouse device is detected.
        public static bool mousePresent { get; }
        //
        // 摘要:
        //     Number of touches. Guaranteed not to change throughout the frame. (Read Only)
        public static int touchCount { get; }
        //
        // 摘要:
        //     Bool value which let's users check if touch pressure is supported.
        public static bool touchPressureSupported { get; }
        //
        // 摘要:
        //     Returns true when Stylus Touch is supported by a device or platform.
        public static bool stylusTouchSupported { get; }
        //
        // 摘要:
        //     Returns whether the device on which application is currently running supports
        //     touch input.
        public static bool touchSupported { get; }
        //
        // 摘要:
        //     Property indicating whether the system handles multiple touches.
        public static bool multiTouchEnabled { get; set; }
        [Obsolete("isGyroAvailable property is deprecated. Please use SystemInfo.supportsGyroscope instead.")]
        public static bool isGyroAvailable { get; }
        //
        // 摘要:
        //     Device physical orientation as reported by OS. (Read Only)
        public static DeviceOrientation deviceOrientation { get; }
        //
        // 摘要:
        //     Last measured linear acceleration of a device in three-dimensional space. (Read
        //     Only)
        public static Vector3 acceleration { get; }
        //
        // 摘要:
        //     This property controls if input sensors should be compensated for screen orientation.
        public static bool compensateSensors { get; set; }
        //
        // 摘要:
        //     Number of acceleration measurements which occurred during last frame.
        public static int accelerationEventCount { get; }
        //
        // 摘要:
        //     Should Back button quit the application? Only usable on Android, Windows Phone
        //     or Windows Tablets.
        public static bool backButtonLeavesApp { get; set; }
        //
        // 摘要:
        //     Property for accessing device location (handheld devices only). (Read Only)
        public static LocationService location { get; }
        //
        // 摘要:
        //     Property for accessing compass (handheld devices only). (Read Only)
        public static Compass compass { get; }
        //
        // 摘要:
        //     Returns default gyroscope.
        public static Gyroscope gyro { get; }
        //
        // 摘要:
        //     The current mouse scroll delta. (Read Only)
        [NativeThrows]
        public static Vector2 mouseScrollDelta { get; }
        //
        // 摘要:
        //     The current mouse position in pixel coordinates. (Read Only)
        [NativeThrows]
        public static Vector3 mousePosition { get; }
        //
        // 摘要:
        //     Returns list of acceleration measurements which occurred during the last frame.
        //     (Read Only) (Allocates temporary variables).
        public static AccelerationEvent[] accelerationEvents { get; }
        //
        // 摘要:
        //     Returns true the first frame the user hits any key or mouse button. (Read Only)
        [NativeThrows]
        public static bool anyKeyDown { get; }
        //
        // 摘要:
        //     Is any key or mouse button currently held down? (Read Only)
        [NativeThrows]
        public static bool anyKey { get; }
        //
        // 摘要:
        //     Enables/Disables mouse simulation with touches. By default this option is enabled.
        public static bool simulateMouseWithTouches { get; set; }
        //
        // 摘要:
        //     Returns list of objects representing status of all touches during last frame.
        //     (Read Only) (Allocates temporary variables).
        public static Touch[] touches { get; }

        //
        // 摘要:
        //     Returns specific acceleration measurement which occurred during last frame. (Does
        //     not allocate temporary variables).
        //
        // 參數:
        //   index:
        [NativeThrows]
        public static AccelerationEvent GetAccelerationEvent(int index);
        //
        // 摘要:
        //     Returns the value of the virtual axis identified by axisName.
        //
        // 參數:
        //   axisName:
        [NativeThrows]
        public static float GetAxis(string axisName);
        //
        // 摘要:
        //     Returns the value of the virtual axis identified by axisName with no smoothing
        //     filtering applied.
        //
        // 參數:
        //   axisName:
        [NativeThrows]
        public static float GetAxisRaw(string axisName);
        //
        // 摘要:
        //     Returns true while the virtual button identified by buttonName is held down.
        //
        // 參數:
        //   buttonName:
        //     The name of the button such as Jump.
        //
        // 返回結果:
        //     True when an axis has been pressed and not released.
        [NativeThrows]
        public static bool GetButton(string buttonName);
        //
        // 摘要:
        //     Returns true during the frame the user pressed down the virtual button identified
        //     by buttonName.
        //
        // 參數:
        //   buttonName:
        [NativeThrows]
        public static bool GetButtonDown(string buttonName);
        //
        // 摘要:
        //     Returns true the first frame the user releases the virtual button identified
        //     by buttonName.
        //
        // 參數:
        //   buttonName:
        [NativeThrows]
        public static bool GetButtonUp(string buttonName);
        //
        // 摘要:
        //     Returns an array of strings describing the connected joysticks.
        [NativeThrows]
        public static string[] GetJoystickNames();
        //
        // 摘要:
        //     Returns true while the user holds down the key identified by name.
        //
        // 參數:
        //   name:
        public static bool GetKey(string name);
        //
        // 摘要:
        //     Returns true while the user holds down the key identified by the key KeyCode
        //     enum parameter.
        //
        // 參數:
        //   key:
        public static bool GetKey(KeyCode key);
        //
        // 摘要:
        //     Returns true during the frame the user starts pressing down the key identified
        //     by the key KeyCode enum parameter.
        //
        // 參數:
        //   key:
        public static bool GetKeyDown(KeyCode key);
        //
        // 摘要:
        //     Returns true during the frame the user starts pressing down the key identified
        //     by name.
        //
        // 參數:
        //   name:
        public static bool GetKeyDown(string name);
        //
        // 摘要:
        //     Returns true during the frame the user releases the key identified by the key
        //     KeyCode enum parameter.
        //
        // 參數:
        //   key:
        public static bool GetKeyUp(KeyCode key);
        //
        // 摘要:
        //     Returns true during the frame the user releases the key identified by name.
        //
        // 參數:
        //   name:
        public static bool GetKeyUp(string name);
        //
        // 摘要:
        //     Returns whether the given mouse button is held down.
        //
        // 參數:
        //   button:
        [NativeThrows]
        public static bool GetMouseButton(int button);
        //
        // 摘要:
        //     Returns true during the frame the user pressed the given mouse button.
        //
        // 參數:
        //   button:
        [NativeThrows]
        public static bool GetMouseButtonDown(int button);
        //
        // 摘要:
        //     Returns true during the frame the user releases the given mouse button.
        //
        // 參數:
        //   button:
        [NativeThrows]
        public static bool GetMouseButtonUp(int button);
        //
        // 摘要:
        //     Call Input.GetTouch to obtain a Touch struct.
        //
        // 參數:
        //   index:
        //     The touch input on the device screen.
        //
        // 返回結果:
        //     Touch details in the struct.
        [NativeThrows]
        public static Touch GetTouch(int index);
        //
        // 摘要:
        //     Determine whether a particular joystick model has been preconfigured by Unity.
        //     (Linux-only).
        //
        // 參數:
        //   joystickName:
        //     The name of the joystick to check (returned by Input.GetJoystickNames).
        //
        // 返回結果:
        //     True if the joystick layout has been preconfigured; false otherwise.
        public static bool IsJoystickPreconfigured(string joystickName);
        //
        // 摘要:
        //     Resets all input. After ResetInputAxes all axes return to 0 and all buttons return
        //     to 0 for one frame.
        [FreeFunction("ResetInput")]
        public static void ResetInputAxes();
    }
}

同樣,我們分類來總結這個類中的屬性和方法。

1.鼠標相關

鼠標位置mousePosition屬性。

每幀鼠標中鍵滾動量,是一個二維向量值,鼠標滾動時其中的y值會發生變化,y=-1代表向下滾、y=0代表沒有滾動、y=1代表向上滾。

 

檢測鼠標輸入,分別代表鼠標按住時觸發、鼠標剛按下時觸發、鼠標抬起時觸發,其中參數0代表鼠標左鍵,1代表鼠標右鍵,2代表鼠標中鍵。

2.鍵盤輸入。

和鼠標狀態相似,分別代表鍵盤按住、鍵盤剛按下、鍵盤抬起時觸發的函數。注意:傳入參數為鍵的名稱時傳入的一定是小寫字符串,一般不傳入名稱,而是使用KeyCode參數的重載,KeyCode是一個所有鍵盤上鍵的枚舉類型。

3.默認軸輸入

Unity提供了軸輸入,其中GetAxis的返回值是-1到1之間,按住相應的鍵不動軸的值會逐漸向-1或者1變化,值是漸變的。GetAxisRaw也是獲得軸的返回值,但是這個返回的軸的值只能是-1,0和1三個值,沒有中間值,返回值直接變化。

軸的名稱和對應的鍵可以在Unity中設置:

添加或者刪除軸只需要直接更改軸的數量即可。

4.其他輸入

1)任意鍵或鼠標

是否有任意鍵按下和按住的屬性。在設置游戲的改建功能時可以使用。

2)手柄輸入

獲取所有手柄的鍵的名稱。

獲取手柄的鍵是否按住、按下和抬起操作。

3)觸摸輸入

當前觸摸點個數。

所有觸摸點。一般和觸摸點個數配合使用,先判斷是否有觸摸,再獲取觸摸點。

多點觸控是否開啟。

4)陀螺儀(重力感應)

 

 

 獲取陀螺儀類對象,在其中開啟陀螺儀和獲取陀螺儀的值。

七.屏幕相關Screen類

  Screen類是一個sealed關鍵字修飾的類,這個類不能被繼承。這個類中的屬性和方法也都是靜態的,這個類用於記錄當前屏幕的信息。

namespace UnityEngine
{
    //
    // 摘要:
    //     Access to display information.
    public sealed class Screen
    {
        public Screen();

        //
        // 摘要:
        //     Set this property to one of the values in FullScreenMode to change the display
        //     mode of your application.
        public static FullScreenMode fullScreenMode { get; set; }
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property GetResolution has been deprecated. Use resolutions instead (UnityUpgradable) -> resolutions", true)]
        public static Resolution[] GetResolution { get; }
        //
        // 摘要:
        //     The current brightness of the screen.
        public static float brightness { get; set; }
        //
        // 摘要:
        //     A power saving setting, allowing the screen to dim some time after the last active
        //     user interaction.
        public static int sleepTimeout { get; set; }
        //
        // 摘要:
        //     Specifies logical orientation of the screen.
        public static ScreenOrientation orientation { get; set; }
        //
        // 摘要:
        //     Allow auto-rotation to landscape right?
        public static bool autorotateToLandscapeRight { get; set; }
        //
        // 摘要:
        //     Allow auto-rotation to landscape left?
        public static bool autorotateToLandscapeLeft { get; set; }
        //
        // 摘要:
        //     Allow auto-rotation to portrait, upside down?
        public static bool autorotateToPortraitUpsideDown { get; set; }
        //
        // 摘要:
        //     Allow auto-rotation to portrait?
        public static bool autorotateToPortrait { get; set; }
        //
        // 摘要:
        //     Returns a list of screen areas that are not functional for displaying content
        //     (Read Only).
        public static Rect[] cutouts { get; }
        //
        // 摘要:
        //     Returns the safe area of the screen in pixels (Read Only).
        public static Rect safeArea { get; }
        //
        // 摘要:
        //     Should the cursor be locked?
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Use Cursor.lockState and Cursor.visible instead.", false)]
        public static bool lockCursor { get; set; }
        //
        // 摘要:
        //     Is the game running full-screen?
        public static bool fullScreen { get; set; }
        //
        // 摘要:
        //     All full-screen resolutions supported by the monitor (Read Only).
        public static Resolution[] resolutions { get; }
        //
        // 摘要:
        //     The current screen resolution (Read Only).
        public static Resolution currentResolution { get; }
        //
        // 摘要:
        //     The current DPI of the screen / device (Read Only).
        public static float dpi { get; }
        //
        // 摘要:
        //     The current height of the screen window in pixels (Read Only).
        public static int height { get; }
        //
        // 摘要:
        //     The current width of the screen window in pixels (Read Only).
        public static int width { get; }
        //
        // 摘要:
        //     Should the cursor be visible?
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Property showCursor has been deprecated. Use Cursor.visible instead (UnityUpgradable) -> UnityEngine.Cursor.visible", true)]
        public static bool showCursor { get; set; }

        //
        // 摘要:
        //     Switches the screen resolution.
        //
        // 參數:
        //   width:
        //
        //   height:
        //
        //   fullscreen:
        //
        //   preferredRefreshRate:
        //
        //   fullscreenMode:
        public static void SetResolution(int width, int height, bool fullscreen);
        //
        // 摘要:
        //     Switches the screen resolution.
        //
        // 參數:
        //   width:
        //
        //   height:
        //
        //   fullscreen:
        //
        //   preferredRefreshRate:
        //
        //   fullscreenMode:
        public static void SetResolution(int width, int height, bool fullscreen, [Internal.DefaultValue("0")] int preferredRefreshRate);
        public static void SetResolution(int width, int height, FullScreenMode fullscreenMode);
        //
        // 摘要:
        //     Switches the screen resolution.
        //
        // 參數:
        //   width:
        //
        //   height:
        //
        //   fullscreen:
        //
        //   preferredRefreshRate:
        //
        //   fullscreenMode:
        public static void SetResolution(int width, int height, FullScreenMode fullscreenMode, [Internal.DefaultValue("0")] int preferredRefreshRate);
    }
}

下面同樣分類整理其中常用的屬性和方法:

1.當前屏幕分辨率

currentResolution是游戲當前所在的屏幕的信息,如在Unity中制作游戲時就是顯示Unity軟件的屏幕的寬高,和Unity中Game窗口設置的寬高無關。Resolution是一個屏幕信息結構體,記錄了當前屏幕的基本信息(寬、高、刷新率)。

 

 2.游戲屏幕寬高

 

 游戲實際占用的寬高信息直接由Screen類中的height和width屬性記錄,如Unity制作游戲時height和width得到的就是Game窗口的寬高。一般作計算時使用這個寬高信息。

3.息屏設置

可以設置息屏時間,如果想設置不息屏,可以將值設置為SleepTimeout類中的NeverSleep常量。

4.其他

1)是否全屏

2)窗口模式

設置窗口的模式,FullScreenMode是一個枚舉,有四個值:

 

 0:獨占全屏;1:全屏窗口化;2:最大化窗口;3:窗口模式

一般屏幕信息在發布時設置,不在代碼中設置。

3)屏幕旋轉

 

 分別代表左橫向、右橫向、豎屏倒置、豎屏正放,設置為true代表允許屏幕設置為此模式。同樣地,一般在發布時設置這類信息,在制作游戲內內容時不用設置這類信息。

指定屏幕顯示方向。ScreenOrientation是一個枚舉類型:

4)設置分辨率

使用SetResolution靜態方法設置分辨率,但是移動設備一般不會使用此方法。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM