/// <summary> /// Invoke the specified function. It's unlikely that you will need to call this function yourself. /// </summary> static public void FindAndExecute (uint objID, string funcName, params object[] parameters) { TNObject obj = TNObject.Find(objID); if (obj != null) { if (!obj.Execute(funcName, parameters)) { #if UNITY_EDITOR Debug.LogError("[TNet] Unable to execute function '" + funcName + "'. Did you forget an [RFC] prefix, perhaps?\n" + "GameObject: " + GetHierarchy(obj.gameObject), obj.gameObject); #endif } } else if (TNManager.isInChannel) { DelayedCall dc = new DelayedCall(); dc.objID = objID; dc.funcName = funcName; dc.parameters = parameters; mDelayed.Add(dc); } #if UNITY_EDITOR else Debug.LogError("[TNet] Trying to execute a function '" + funcName + "' on TNObject #" + objID + " before it has been created."); #endif }
/// <summary> /// Register the object with the lists. /// </summary> void Start () { if (id == 0) { mParent = FindParent(transform.parent); if (!TNManager.isConnected) return; if (mParent == null && Application.isPlaying) { Debug.LogError("Objects that are not instantiated via TNManager.Create must have a non-zero ID.", this); return; } } else { Register(); // Have there been any delayed function calls for this object? If so, execute them now. for (int i = 0; i < mDelayed.size; ) { DelayedCall dc = mDelayed[i]; if (dc.objID == uid) { if (!string.IsNullOrEmpty(dc.funcName)) Execute(dc.funcName, dc.parameters); else Execute(dc.funcID, dc.parameters); mDelayed.RemoveAt(i); continue; } ++i; } } }
/// <summary> /// Retrieve the Tasharen Network Object by ID. /// </summary> static public TNObject Find (uint tnID) { if (mDictionary == null) return null; TNObject tno = null; mDictionary.TryGetValue(tnID, out tno); return tno; }
/// <summary> /// Invoke the specified function. It's unlikely that you will need to call this function yourself. /// </summary> static public void FindAndExecute (uint objID, byte funcID, params object[] parameters) { TNObject obj = TNObject.Find(objID); if (obj != null) { if (!obj.Execute(funcID, parameters)) { #if UNITY_EDITOR Debug.LogError("[TNet] Unable to execute function with ID of '" + funcID + "'. Make sure there is a script that can receive this call.\n" + "GameObject: " + GetHierarchy(obj.gameObject), obj.gameObject); #endif } } else if (TNManager.isInChannel) { DelayedCall dc = new DelayedCall(); dc.objID = objID; dc.funcID = funcID; dc.parameters = parameters; mDelayed.Add(dc); } #if UNITY_EDITOR else Debug.LogError("[TNet] Trying to execute a function " + funcID + " on TNObject #" + objID + " before it has been created."); #endif }
/// <summary> /// Make sure that this object's ID is actually unique. /// </summary> void UniqueCheck() { if (id < 0) { id = -id; } TNObject tobj = Find(uid); if (id == 0 || tobj != null) { if (Application.isPlaying && TNManager.isInChannel) { if (tobj != null) { Debug.LogError("Network ID " + id + " is already in use by " + GetHierarchy(tobj.gameObject) + ".\nPlease make sure that the network IDs are unique.", this); } else { Debug.LogError("Network ID of 0 is used by " + GetHierarchy(gameObject) + "\nPlease make sure that a unique non-zero ID is given to all objects.", this); } } uid = GetUniqueID(); } }
/// <summary> /// If custom functionality is needed, all unrecognized packets will arrive here. /// </summary> void OnForwardedPacket(BinaryReader reader) { uint objID; byte funcID; TNObject.DecodeUID(reader.ReadUInt32(), out objID, out funcID); if (funcID == 0) { string funcName = ""; try { funcName = reader.ReadString(); TNObject.FindAndExecute(objID, funcName, reader.ReadArray()); } catch (System.Exception ex) { Debug.LogError(objID + " " + funcID + " " + funcName + "\n" + ex.Message + "\n" + ex.StackTrace); } } else { try { TNObject.FindAndExecute(objID, funcID, reader.ReadArray()); } catch (System.Exception ex) { Debug.LogError(objID + " " + funcID + "\n" + ex.Message + "\n" + ex.StackTrace); } } }
/// <summary> /// Send a new RFC call to the specified target. /// </summary> static void SendRFC(uint objID, byte rfcID, string rfcName, Target target, bool reliable, params object[] objs) { #if UNITY_EDITOR if (!Application.isPlaying) { return; } #endif bool executeLocally = false; if (target == Target.Host && TNManager.isHosting) { // We're the host, and the packet should be going to the host -- just echo it locally executeLocally = true; } else if (TNManager.isInChannel) { // We want to echo UDP-based packets locally instead of having them bounce through the server if (!reliable) { if (target == Target.All) { target = Target.Others; executeLocally = true; } else if (target == Target.AllSaved) { target = Target.OthersSaved; executeLocally = true; } } byte packetID = (byte)((int)Packet.ForwardToAll + (int)target); BinaryWriter writer = TNManager.BeginSend(packetID); writer.Write(GetUID(objID, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } UnityTools.Write(writer, objs); TNManager.EndSend(reliable); } else if (!TNManager.isConnected && (target == Target.All || target == Target.AllSaved)) { // Offline packet executeLocally = true; } if (executeLocally) { if (rfcID != 0) { TNObject.FindAndExecute(objID, rfcID, objs); } else { TNObject.FindAndExecute(objID, rfcName, objs); } } }
public virtual void Awake() { //we're guaranteed to have this rb = gameObject.GetOrAdd <Rigidbody> (); rb.interpolation = RigidbodyInterpolation.None; rb.useGravity = false; rb.isKinematic = true; gameObject.layer = Globals.LayerNumBodyPart; NObject = gameObject.GetComponent <TNObject> (); MovementPivot = transform; if (RotationPivot == null) { RotationPivot = MovementPivot; } MovementPivot.localRotation = Quaternion.identity; RotationPivot.localRotation = Quaternion.identity; // _worldBodyNetworkUpdateTime = NetworkManager.WorldBodyUpdateRate; // _bodyAnimatorNetworkUpdateTime = NetworkManager.BodyAnimatorUpdateRate; Animator = gameObject.GetComponent <BodyAnimator> (); Animator.animator = gameObject.GetComponent <Animator> (); if (Animator.animator == null) { Animator.animator = RotationPivot.gameObject.GetComponent <Animator> (); } Transforms = gameObject.GetComponent <BodyTransforms> (); Sounds = gameObject.GetComponent <BodySounds> (); if (Sounds != null) { Sounds.Animator = Animator; } SetVisible(false); IgnoreCollisions(true); }
/// <summary> /// Register the object with the lists. /// </summary> void Start() { if (id == 0) { mParent = FindParent(transform.parent); } Register(); // Have there been any delayed function calls for this object? If so, execute them now. for (int i = 0; i < mDelayed.size;) { DelayedCall dc = mDelayed[i]; if (dc.objID == uid) { if (!string.IsNullOrEmpty(dc.funcName)) { Execute(dc.funcName, dc.parameters); } else { Execute(dc.funcID, dc.parameters); } mDelayed.RemoveAt(i); continue; } ++i; } }
/// <summary> /// Invoke the specified function. It's unlikely that you will need to call this function yourself. /// </summary> static public void FindAndExecute(uint objID, byte funcID, params object[] parameters) { TNObject obj = TNObject.Find(objID); if (obj != null) { if (!obj.Execute(funcID, parameters)) { #if UNITY_EDITOR Debug.LogError("[TNet] Unable to execute function with ID of '" + funcID + "'. Make sure there is a script that can receive this call.\n" + "GameObject: " + GetHierarchy(obj.gameObject), obj.gameObject); #endif } } #if UNITY_EDITOR else if (TNManager.isJoiningChannel) { Debug.Log("[TNet] Trying to execute RFC #" + funcID + " on TNObject #" + objID + " before it has been created."); } else { Debug.LogWarning("[TNet] Trying to execute RFC #" + funcID + " on TNObject #" + objID + " before it has been created."); } #endif }
public WeaponInstance(Weapon w, Cooldown_counter fire, Cooldown_counter reload, Transform t, TNObject tnObject) { FireCooldown = fire; ReloadCooldown = reload; baseWeapon = w; raycastOrigin = t; tno = tnObject; }
/// <summary> /// Notification of a network object being destroyed. /// </summary> void OnDestroyObject(uint objID) { TNObject obj = TNObject.Find(objID); if (obj) { GameObject.Destroy(obj.gameObject); } }
/// <summary> /// Clicking on the object should change its color. /// </summary> void OnClick () { Color color = Color.red; if (GetComponent<Renderer>().material.color == Color.red) color = Color.green; else if (GetComponent<Renderer>().material.color == Color.green) color = Color.blue; TNObject tno = GetComponent<TNObject>(); tno.Send("OnColor", Target.AllSaved, color); }
/// <summary> /// Finds the specified component on the game object or one of its parents. /// </summary> static TNObject FindParent (Transform t) { while (t != null) { TNObject tno = t.gameObject.GetComponent<TNObject>(); if (tno != null) return tno; t = t.parent; } return null; }
/// <summary> /// RequestCreate flag. /// 0 = Local-only object. Only echoed to other clients. /// 1 = Saved on the server, assigned a new owner when the existing owner leaves. /// 2 = Saved on the server, destroyed when the owner leaves. /// </summary> static byte GetFlag(GameObject go, bool persistent) { TNObject tno = go.GetComponent <TNObject>(); if (tno == null) { return(0); } return(persistent ? (byte)1 : (byte)2); }
public void RequestData(TNObject callOrigin, Target target, FrontiersData action) { _requests.Add(_requestsCount, callOrigin); // Tells us who wants it back action.ResponseTargetID = _requestsCount; action.ResponsePlayerID = GetMyDetails().id; _requestsCount++; tno.Send("GetData", target, action); }
/// <summary> /// Get a new unique object identifier. /// </summary> static uint GetUniqueID () { TNObject[] tns = (TNObject[])FindObjectsOfType(typeof(TNObject)); for (int i = 0, imax = tns.Length; i < imax; ++i) { TNObject ts = tns[i]; if (ts != null && ts.uid > mLastID && ts.uid < 32768) mLastID = ts.uid; } return ++mLastID; }
// Start is called before the first frame update void Start() { tno = GetComponent <TNObject>(); var coll = gameObject.AddComponent <CapsuleCollider>(); coll.height = verticalRange; coll.center = new Vector3(0, verticalRange * .5f, 0); coll.radius = PickupRadius; coll.isTrigger = true; offset = new Vector3(0, verticalRange + pickupTransform.localScale.y, 0); }
public override void OnInspectorGUI() { TNObject obj = target as TNObject; if (Application.isPlaying) { EditorGUI.BeginDisabledGroup(true); EditorGUILayout.LabelField("ID", obj.uid.ToString()); EditorGUILayout.LabelField("Owner", obj.isMine ? "Me" : obj.ownerID.ToString()); EditorGUILayout.LabelField("Host", TNManager.isHosting ? "Me" : TNManager.hostID.ToString()); if (obj.parent != null) { EditorGUILayout.ObjectField("Parent", obj.parent, typeof(TNObject), true); } EditorGUI.EndDisabledGroup(); } else { serializedObject.Update(); SerializedProperty sp = serializedObject.FindProperty("id"); EditorGUILayout.PropertyField(sp, new GUIContent("ID")); serializedObject.ApplyModifiedProperties(); PrefabType type = PrefabUtility.GetPrefabType(obj.gameObject); if (type == PrefabType.Prefab) { return; } if (obj.uid == 0) { EditorGUILayout.HelpBox("Object ID of '0' means this object must be dynamically instantiated via TNManager.Create.", MessageType.Info); } else { TNObject[] tnos = FindObjectsOfType <TNObject>(); foreach (TNObject o in tnos) { if (o == obj || o.parent != null) { continue; } if (o.uid == obj.uid) { EditorGUILayout.HelpBox("This ID is shared with other TNObjects. A unique ID is required in order for Remote Function Calls to function properly.", MessageType.Error); break; } } } } }
public void Mount(uint battlePlayerID, int mountPoint) { var battlePlayerTno = TNObject.Find(tno.channelID, battlePlayerID); if (battlePlayerTno == null) { Debug.LogFormat("unable to find prefab ID {0}", battlePlayerID); return; } var bp = battlePlayerTno.GetComponent <BattlePlayer>(); bp.PlaceGameObject(gameObject, mountPoint); }
/// <summary> /// Destroy the specified game object. /// </summary> static public void Destroy(GameObject go) { if (isConnected) { TNObject obj = go.GetComponent <TNObject>(); if (obj != null) { obj.DestroySelf(); return; } } GameObject.Destroy(go); }
protected override void Awake() { base.Awake(); tno = GetComponent <TNObject>(); if (GameObject.Find("MyText") != null) { myText = GameObject.Find("MyText").GetComponent <Text>(); } if (myText != null) { myText.text = "Debug\n"; } }
/// <summary> /// If custom functionality is needed, all unrecognized packets will arrive here. /// </summary> void OnForwardedPacket(BinaryReader reader) { uint objID; byte funcID; TNObject.DecodeUID(reader.ReadUInt32(), out objID, out funcID); if (funcID == 0) { TNObject.FindAndExecute(objID, reader.ReadString(), UnityTools.Read(reader)); } else { TNObject.FindAndExecute(objID, funcID, UnityTools.Read(reader)); } }
/// <summary> /// Destroy the specified game object. /// </summary> static public void Destroy(GameObject go) { if (isConnected) { TNObject obj = go.GetComponent <TNObject>(); if (obj != null) { BinaryWriter writer = mInstance.mClient.BeginSend(Packet.RequestDestroy); writer.Write(obj.uid); mInstance.mClient.EndSend(); return; } } GameObject.Destroy(go); }
/// <summary> /// Notification of a new object being created. /// </summary> void OnCreateObject(int creator, int index, uint objectID, BinaryReader reader) { mObjectOwner = creator; GameObject go = null; if (index == 65535) { // Load the object from the resources folder go = LoadGameObject(reader.ReadString()); } else if (index >= 0 && index < objects.Length) { // Reference the object from the provided list go = objects[index]; } else { Debug.LogError("Attempting to create an invalid object. Index: " + index); return; } // Create the object go = CreateGameObject(go, reader); // If an object ID was requested, assign it to the TNObject if (go != null && objectID != 0) { TNObject obj = go.GetComponent <TNObject>(); if (obj != null) { obj.uid = objectID; obj.Register(); } else { Debug.LogWarning("[TNet] The instantiated object has no TNObject component. Don't request an ObjectID when creating it.", go); } } mObjectOwner = -1; }
/// <summary> /// Register the object with the lists. /// </summary> void Start() { if (id == 0) { mParent = FindParent(transform.parent); if (!TNManager.isConnected) { return; } if (mParent == null && Application.isPlaying) { Debug.LogError("Objects that are not instantiated via TNManager.Create must have a non-zero ID.", this); return; } } else { Register(); } }
static void AddRequiredComponents() { foreach (GameObject selectedObject in Selection.gameObjects) { selectedObject.GetOrAdd <Animator> (); selectedObject.GetOrAdd <Rigidbody> (); selectedObject.GetOrAdd <CharacterAnimator> (); selectedObject.GetOrAdd <BodyTransforms> (); selectedObject.GetOrAdd <CharacterBody> (); selectedObject.GetOrAdd <CharacterSounds> (); CapsuleCollider cc = selectedObject.GetComponent <CapsuleCollider> (); if (cc != null) { GameObject.DestroyImmediate(cc); } if (selectedObject.GetComponent <Animation>() != null) { GameObject.DestroyImmediate(selectedObject.GetComponent <Animation>()); } TNObject tnObject = selectedObject.GetOrAdd <TNObject> (); TNAutoSync tnAutoSync = selectedObject.GetOrAdd <TNAutoSync> (); } }
/// <summary> /// Register the object with the lists. /// </summary> void Start() { if (id == 0) mParent = FindParent(transform.parent); Register(); // Have there been any delayed function calls for this object? If so, execute them now. for (int i = 0; i < mDelayed.size; ) { DelayedCall dc = mDelayed[i]; if (dc.objID == uid) { if (!string.IsNullOrEmpty(dc.funcName)) Execute(dc.funcName, dc.parameters); else Execute(dc.funcID, dc.parameters); mDelayed.RemoveAt(i); continue; } ++i; } }
/// <summary> /// Register the object with the lists. /// </summary> void Start() { if (id == 0) { mParent = FindParent(transform.parent); if (!TNManager.isConnected) return; if (mParent == null && Application.isPlaying) { Debug.LogError("Objects that are not instantiated via TNManager.Create must have a non-zero ID.", this); return; } } else Register(); }
protected override void Awake() { base.Awake(); tno = GetComponent <TNObject>(); }
public override void OnInspectorGUI() { TNObject obj = target as TNObject; if (Application.isPlaying) { EditorGUI.BeginDisabledGroup(true); EditorGUILayout.LabelField("Channel", obj.channelID.ToString("N0")); EditorGUILayout.LabelField("ID", obj.uid.ToString("N0")); EditorGUILayout.LabelField("Creator", obj.creatorPlayerID.ToString("N0")); if (obj.owner != null) { EditorGUILayout.LabelField("Owner", obj.owner.name + " (" + obj.ownerID.ToString("N0") + ")"); } else { EditorGUILayout.LabelField("Owner", obj.ownerID.ToString("N0")); } var host = TNManager.GetHost(obj.channelID); EditorGUILayout.LabelField("Host", (host != null) ? host.name : "<none>"); if (obj.parent != null) { EditorGUILayout.ObjectField("Parent", obj.parent, typeof(TNObject), true); } var data = obj.dataNode; if (data != null && data.children.size > 0) { Print(data); } EditorGUI.EndDisabledGroup(); } else { serializedObject.Update(); var staticID = serializedObject.FindProperty("mStaticID"); EditorGUILayout.PropertyField(staticID, new GUIContent("ID")); var sp = serializedObject.FindProperty("ignoreWarnings"); EditorGUILayout.PropertyField(sp, new GUIContent("Ignore Warnings")); var type = PrefabUtility.GetPrefabType(obj.gameObject); if (type == PrefabType.Prefab) { serializedObject.ApplyModifiedProperties(); return; } if (staticID.intValue == 0) { EditorGUILayout.HelpBox("Object ID of '0' means this object must be dynamically instantiated via TNManager.Instantiate.", MessageType.Info); if (GUILayout.Button("Assign Unique ID")) { staticID.intValue = (int)TNObject.GetUniqueID(false); } } else { var tnos = FindObjectsOfType <TNObject>(); foreach (TNObject o in tnos) { if (o == obj || o.parent != null) { continue; } if (o.uid == obj.uid) { EditorGUILayout.HelpBox("This ID is shared with other TNObjects. A unique ID is required in order for RFCs to function properly.", MessageType.Error); break; } } } serializedObject.ApplyModifiedProperties(); } }
/// <summary> /// Notification of a new object being created. /// </summary> void OnCreateObject(int creator, int index, uint objectID, BinaryReader reader) { if (index >= objects.Length) { Debug.LogError("Attempting to create an invalid object. Index: " + index); return; } GameObject go = null; mObjectOwner = creator; int type = reader.ReadByte(); if (type == 2) { Vector3 pos = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); Quaternion rot = new Quaternion(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); Vector3 vel = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); Vector3 ang = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); go = Instantiate(objects[index], pos, rot) as GameObject; Rigidbody rb = go.rigidbody; if (rb != null) { if (rb.isKinematic) { rb.isKinematic = false; rb.velocity = vel; rb.angularVelocity = ang; rb.isKinematic = true; } else { rb.velocity = vel; rb.angularVelocity = ang; } } } else if (type == 1) { Vector3 pos = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); Quaternion rot = new Quaternion(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); go = Instantiate(objects[index], pos, rot) as GameObject; } else { go = Instantiate(objects[index]) as GameObject; } if (go != null && objectID != 0) { TNObject obj = go.GetComponent <TNObject>(); if (obj != null) { obj.uid = objectID; obj.Register(); } else { Debug.LogWarning("The instantiated object has no TNObject component. Don't request a ObjectID when creating it.", go); } } }