C++RAW #include "UnityPrefix.h" #include "Configuration/UnityConfigure.h" #include "Runtime/Graphics/Transform.h" #include "Runtime/Mono/MonoBehaviour.h" #include "Runtime/BaseClasses/Tags.h" #include "Runtime/Scripting/ScriptingUtility.h" #include "Runtime/Export/GameObjectExport.h" #if ENABLE_AUDIO #include "Runtime/Audio/AudioSource.h" #endif #include "Runtime/Animation/Animation.h" #include "Runtime/Camera/Camera.h" #if ENABLE_PHYSICS #include "Runtime/Dynamics/RigidBody.h" #include "Runtime/Dynamics/ConstantForce.h" #include "Runtime/Dynamics/HingeJoint.h" #include "Runtime/Dynamics/Collider.h" #endif #if ENABLE_2D_PHYSICS #include "Runtime/Physics2D/RigidBody2D.h" #include "Runtime/Physics2D/Collider2D.h" #endif #include "Runtime/Camera/Light.h" #include "Runtime/Filters/Renderer.h" #if ENABLE_NETWORK #include "Runtime/Network/NetworkView.h" #endif #include "Runtime/Camera/RenderLayers/GUIText.h" #include "Runtime/Camera/RenderLayers/GUITexture.h" #include "Runtime/Filters/Particles/ParticleEmitter.h" #include "Runtime/Graphics/ParticleSystem/ParticleSystem.h" #include "Runtime/Misc/GameObjectUtility.h" #include "Runtime/Misc/GOCreation.h" #include "Runtime/Scripting/ScriptingExportUtility.h" #include "Runtime/Scripting/GetComponent.h" #include "Runtime/Scripting/Scripting.h" using namespace Unity; /* Mono defines a bool as either 1 or 2 bytes. On windows a bool on the C++ side needs to be 2 bytes. We use the typemap to map bool's to short's. When using the C++ keyword and you want to export a bool value to mono you have to use a short on the C++ side. */ using namespace std; CSRAW using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Collections; using UnityEngineInternal; namespace UnityEngine { // Base class for all entities in Unity scenes. CSRAW CLASS GameObject : Object // Creates a game object with a primitive mesh renderer and appropriate collider. CSRAW CUSTOM static GameObject CreatePrimitive (PrimitiveType type) { return Scripting::ScriptingWrapperFor(CreatePrimitive(type)); } // Returns the component of Type /type/ if the game object has one attached, null if it doesn't. You can access both builtin components or scripts with this function. CSRAW [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)] CUSTOM Component GetComponent (Type type) { return ScriptingGetComponentOfType (*self, type); } CSRAW #if ENABLE_GENERICS // Generic version. See the [[wiki:Generic Functions|Generic Functions]] page for more details. public T GetComponent() where T : Component { return GetComponent(typeof(T)) as T; } #endif // Returns the component with name /type/ if the game object has one attached, null if it doesn't. CSRAW public Component GetComponent (string type) { return GetComponentByName(type); } CUSTOM private Component GetComponentByName (string type) { return Scripting::GetScriptingWrapperOfComponentOfGameObject (*self, type); } // Returns the component of Type /type/ in the GameObject or any of its children using depth first search. CSRAW [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)] CSRAW public Component GetComponentInChildren (Type type) { if( activeInHierarchy ) { Component attachedCom = GetComponent (type); if ( attachedCom != null ) return attachedCom; } Transform transform = this.transform; if ( transform != null ) { foreach (Transform child in transform) { Component childCom = child.gameObject.GetComponentInChildren (type); if (childCom != null) return childCom; } } return null; } CSRAW #if ENABLE_GENERICS // Generic version. See the [[wiki:Generic Functions|Generic Functions]] page for more details. public T GetComponentInChildren() where T : Component { return GetComponentInChildren(typeof(T)) as T; } #endif // Editor only API that specifies if a game object is static. AUTO_PROP bool isStatic GetIsStaticDeprecated SetIsStaticDeprecated CUSTOM_PROP internal bool isStaticBatchable { return self->IsStaticBatchable (); } // Returns all components of Type /type/ in the GameObject. CSRAW [CanConvertToFlash] CSRAW public Component[] GetComponents (Type type) { return (Component[])GetComponentsInternal (type, false, false, true); } CSRAW #if ENABLE_GENERICS // Generic version. See the [[wiki:Generic Functions|Generic Functions]] page for more details. public T[] GetComponents() where T : Component { return (T[]) GetComponentsInternal(typeof(T), true, false, true); } #endif // Returns all components of Type /type/ in the GameObject or any of its children. CSRAW public Component[] GetComponentsInChildren (Type type, bool includeInactive = false) { return (Component[])GetComponentsInternal(type, false, true, includeInactive); } CSRAW #if ENABLE_GENERICS // Generic version. See the [[wiki:Generic Functions|Generic Functions]] page for more details. public T[] GetComponentsInChildren(bool includeInactive) where T : Component { return (T[])GetComponentsInternal(typeof(T), true, true, includeInactive); } #endif CSRAW #if ENABLE_GENERICS // Manually supplying the overload because lucas cannot figure out how to patch cspreprocess to add template support to the overload generation code. // Generic version. See the [[wiki:Generic Functions|Generic Functions]] page for more details. public T[] GetComponentsInChildren() where T : Component { return GetComponentsInChildren(false); } #endif CUSTOM private Component[] GetComponentsInternal(Type type, bool isGenericTypeArray, bool recursive, bool includeInactive) { DISALLOW_IN_CONSTRUCTOR return ScriptingGetComponentsOfType (*self, type, isGenericTypeArray, recursive, includeInactive); } C++RAW #define FASTGO_QUERY_COMPONENT(x) GameObject& go = *self; return Scripting::GetComponentObjectToScriptingObject (go.QueryComponent (x), go, ClassID (x)); // The [[Transform]] attached to this GameObject. (null if there is none attached) CSRAW CUSTOM_PROP Transform transform { FASTGO_QUERY_COMPONENT(Transform) } CONDITIONAL ENABLE_PHYSICS // The [[Rigidbody]] attached to this GameObject (RO). (null if there is none attached) CUSTOM_PROP Rigidbody rigidbody { FASTGO_QUERY_COMPONENT(Rigidbody) } CONDITIONAL ENABLE_2D_PHYSICS CUSTOM_PROP Rigidbody2D rigidbody2D { FASTGO_QUERY_COMPONENT(Rigidbody2D) } // The [[Camera]] attached to this GameObject (RO). (null if there is none attached) CUSTOM_PROP Camera camera { FASTGO_QUERY_COMPONENT(Camera) } // The [[Light]] attached to this GameObject (RO). (null if there is none attached) CUSTOM_PROP Light light { FASTGO_QUERY_COMPONENT(Light) } // The [[Animation]] attached to this GameObject (RO). (null if there is none attached) CUSTOM_PROP Animation animation { FASTGO_QUERY_COMPONENT(Animation) } // The [[ConstantForce]] attached to this GameObject (RO). (null if there is none attached) CONDITIONAL ENABLE_PHYSICS CUSTOM_PROP ConstantForce constantForce { FASTGO_QUERY_COMPONENT(ConstantForce) } // The [[Renderer]] attached to this GameObject (RO). (null if there is none attached) CUSTOM_PROP Renderer renderer { FASTGO_QUERY_COMPONENT(Renderer) } // The [[AudioSource]] attached to this GameObject (RO). (null if there is none attached) CONDITIONAL ENABLE_AUDIO CUSTOM_PROP AudioSource audio { FASTGO_QUERY_COMPONENT(AudioSource) } // The [[GUIText]] attached to this GameObject (RO). (null if there is none attached) CUSTOM_PROP GUIText guiText { FASTGO_QUERY_COMPONENT(GUIText) } CONDITIONAL ENABLE_NETWORK // The [[NetworkView]] attached to this GameObject (RO). (null if there is none attached) CUSTOM_PROP NetworkView networkView { #if ENABLE_NETWORK FASTGO_QUERY_COMPONENT(NetworkView) #else return SCRIPTING_NULL; #endif } FLUSHCONDITIONS CSRAW #if ENABLE_NETWORK OBSOLETE warning Please use guiTexture instead #endif CUSTOM_PROP GUIElement guiElement { FASTGO_QUERY_COMPONENT(GUIElement) } // The [[GUITexture]] attached to this GameObject (RO). (null if there is none attached) CUSTOM_PROP GUITexture guiTexture { FASTGO_QUERY_COMPONENT(GUITexture) } // The [[Collider]] attached to this GameObject (RO). (null if there is none attached) CONDITIONAL ENABLE_PHYSICS CUSTOM_PROP Collider collider { FASTGO_QUERY_COMPONENT(Collider) } CONDITIONAL ENABLE_2D_PHYSICS CUSTOM_PROP Collider2D collider2D { FASTGO_QUERY_COMPONENT(Collider2D) } // The [[HingeJoint]] attached to this GameObject (RO). (null if there is none attached) CONDITIONAL ENABLE_PHYSICS CUSTOM_PROP HingeJoint hingeJoint { FASTGO_QUERY_COMPONENT(HingeJoint) } // The [[ParticleEmitter]] attached to this GameObject (RO). (null if there is none attached) CUSTOM_PROP ParticleEmitter particleEmitter { FASTGO_QUERY_COMPONENT(ParticleEmitter) } // The [[ParticleSystem]] attached to this GameObject (RO). (null if there is none attached) CUSTOM_PROP ParticleSystem particleSystem { FASTGO_QUERY_COMPONENT(ParticleSystem) } // The layer the game object is in. A layer is in the range [0...31] AUTO_PROP int layer GetLayer SetLayer OBSOLETE warning GameObject.active is obsolete. Use GameObject.SetActive(), GameObject.activeSelf or GameObject.activeInHierarchy. CSRAW CUSTOM_PROP bool active { return self->IsActive (); } { self->SetSelfActive (value); } // Activates/Deactivates the GameObject. CUSTOM void SetActive (bool value) { self->SetSelfActive (value); } // The local active state of this GameObject. CUSTOM_PROP public bool activeSelf { return self->IsSelfActive (); } // Is the GameObject active in the scene? CUSTOM_PROP public bool activeInHierarchy { return self->IsActive (); } OBSOLETE warning gameObject.SetActiveRecursively() is obsolete. Use GameObject.SetActive(), which is now inherited by children. CUSTOM public void SetActiveRecursively (bool state) { self->SetActiveRecursivelyDeprecated (state); } // The tag of this game object. CUSTOM_PROP string tag { const string& tag = TagToString (self->GetTag ()); if (!tag.empty ()) return scripting_string_new(tag); else { Scripting::RaiseMonoException ("GameObject has undefined tag!"); return SCRIPTING_NULL; } } { self->SetTag (ExtractTagThrowing (value)); } // Is this game object tagged with /tag/? CUSTOM public bool CompareTag (string tag) { return ExtractTagThrowing (tag) == self->GetTag (); } //*undocumented* CUSTOM static GameObject FindGameObjectWithTag (string tag) { DISALLOW_IN_CONSTRUCTOR int tagInt = ExtractTagThrowing (tag); return Scripting::ScriptingWrapperFor(FindGameObjectWithTag (tagInt)); } // Returns one active GameObject tagged /tag/. Returns null if no GameObject was found. CSRAW static public GameObject FindWithTag (string tag) { return FindGameObjectWithTag(tag); } // Returns a list of active GameObjects tagged /tag/. Returns null if no GameObject was found. CUSTOM static GameObject[] FindGameObjectsWithTag (string tag) { DISALLOW_IN_CONSTRUCTOR static vector go; go.clear (); int tagInt = ExtractTagThrowing (tag); FindGameObjectsWithTag (tagInt, go); ScriptingClassPtr goClass = Scripting::ClassIDToScriptingType (ClassID (GameObject)); return CreateScriptingArrayFromUnityObjects(go,goClass); } // Calls the method named /methodName/ on every [[MonoBehaviour]] in this game object and on every ancestor of the behaviour CUSTOM void SendMessageUpwards (string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver) { Scripting::SendScriptingMessageUpwards(*self, methodName, value, options); } //*undocumented* Function is for convenience and avoid coming mistakes. CSRAW public void SendMessageUpwards (string methodName, SendMessageOptions options) { SendMessageUpwards (methodName, null, options); } // Calls the method named /methodName/ on every [[MonoBehaviour]] in this game object. CUSTOM void SendMessage (string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver) { Scripting::SendScriptingMessage(*self, methodName, value, options); } //*undocumented* Function is for convenience and avoid coming mistakes. CSRAW public void SendMessage (string methodName, SendMessageOptions options) { SendMessage (methodName, null, options); } // Calls the method named /methodName/ on every [[MonoBehaviour]] in this game object or any of its children. CUSTOM void BroadcastMessage (string methodName, object parameter = null, SendMessageOptions options = SendMessageOptions.RequireReceiver) { Scripting::BroadcastScriptingMessage(*self, methodName, parameter, options); } //*undocumented* Function is for convenience and avoid coming mistakes. CSRAW public void BroadcastMessage (string methodName, SendMessageOptions options) { BroadcastMessage (methodName, null, options); } // Adds a component class named /className/ to the game object. CUSTOM Component AddComponent (string className) { return MonoAddComponent (*self, className.AsUTF8().c_str()); } // Adds a component class of type /componentType/ to the game object. C# Users can use a generic version CSRAW [TypeInferenceRule(TypeInferenceRules.TypeReferencedByFirstArgument)] CSRAW public Component AddComponent (Type componentType) { return Internal_AddComponentWithType(componentType); } CSRAW CUSTOM private Component Internal_AddComponentWithType (Type componentType) { return MonoAddComponentWithType(*self, componentType); } CSRAW #if ENABLE_GENERICS // Generic version. See the [[wiki:Generic Functions|Generic Functions]] page for more details. public T AddComponent() where T : Component { return AddComponent(typeof(T)) as T; } #endif CUSTOM private static void Internal_CreateGameObject ([Writable]GameObject mono, string name) { DISALLOW_IN_CONSTRUCTOR GameObject* go = MonoCreateGameObject (name.IsNull() ? NULL : name.AsUTF8().c_str() ); Scripting::ConnectScriptingWrapperToObject (mono.GetScriptingObject(), go); } // Creates a new game object, named __name__. CSRAW public GameObject (string name) { Internal_CreateGameObject (this, name); } // Creates a new game object. CSRAW public GameObject () { Internal_CreateGameObject (this, null); } // Creates a game object and attaches the specified components. CSRAW public GameObject (string name, params Type[] components) { Internal_CreateGameObject (this, name); foreach (Type t in components) AddComponent (t); } OBSOLETE warning gameObject.PlayAnimation is not supported anymore. Use animation.Play CUSTOM void PlayAnimation (AnimationClip animation) { ErrorString("gameObject.PlayAnimation(); is not supported anymore. Use animation.Stop();"); } OBSOLETE warning gameObject.StopAnimation is not supported anymore. Use animation.Stop CUSTOM void StopAnimation () { ErrorString("gameObject.StopAnimation(); is not supported anymore. Use animation.Stop();"); } // Finds a game object by /name/ and returns it. CSRAW CUSTOM static GameObject Find (string name) { DISALLOW_IN_CONSTRUCTOR Transform* transform = FindActiveTransformWithPath (name.AsUTF8().c_str ()); GameObject* go = NULL; if (transform) go = transform->GetGameObjectPtr (); return Scripting::ScriptingWrapperFor (go); } //*undocumented - just for convenience CSRAW public GameObject gameObject { get { return this; } } } END