/// <summary> /// Destroy this game object on all connected clients and remove it from the server. /// </summary> public void DestroySelf () { StartCoroutine(EnsureDestroy()); TNManager.Destroy(gameObject); }
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> /// Send a new RFC call to the specified target. /// </summary> void SendRFC(byte rfcID, string rfcName, Target target, bool reliable, params object[] objs) { #if UNITY_EDITOR if (!Application.isPlaying) { return; } #endif if (hasBeenDestroyed) { return; } bool executeLocally = false; if (target == Target.Broadcast) { if (TNManager.isConnected) { BinaryWriter writer = TNManager.BeginSend(Packet.Broadcast); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSend(reliable); } else { executeLocally = true; } } else 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(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSend(reliable); } else if (!TNManager.isConnected && (target == Target.All || target == Target.AllSaved)) { // Offline packet executeLocally = true; } if (executeLocally) { if (rfcID != 0) { Execute(rfcID, objs); } else { Execute(rfcName, objs); } } }
public override void OnEnter() { Data.Value = TNManager.GetPlayerData <string>(DataName.Value); Finish(); }
void SpawnNow() { TNManager.Create(prefabToSpawn, transform.position, Quaternion.identity, persistent); }
public override void OnEnter() { TNManager.SetChannelData(Channel.Value, DataName.Value, Data.Value); Finish(); }
/// <summary> /// Send a new RFC call to the specified target. /// </summary> void SendRFC(byte rfcID, string rfcName, Target target, bool reliable, params object[] objs) { #if UNITY_EDITOR if (!Application.isPlaying) { return; } #endif if (mDestroyed) { return; } if ((target == Target.AllSaved || target == Target.Others) && TNManager.IsChannelLocked(channelID)) { #if UNITY_EDITOR Debug.LogError("Can't send persistent RFCs while in a locked channel"); #endif return; } // Some very odd special case... sending a string[] as the only parameter // results in objs[] being a string[] instead, when it should be object[string[]]. if (objs != null && objs.GetType() != typeof(object[])) { objs = new object[] { objs } } ; bool executeLocally = false; bool connected = TNManager.isConnected; if (target == Target.Broadcast) { if (connected) { BinaryWriter writer = TNManager.BeginSend(Packet.Broadcast); writer.Write(TNManager.playerID); writer.Write(channelID); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSend(channelID, reliable); } else { executeLocally = true; } } else if (target == Target.Admin) { if (connected) { BinaryWriter writer = TNManager.BeginSend(Packet.BroadcastAdmin); writer.Write(TNManager.playerID); writer.Write(channelID); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSend(channelID, reliable); } else { executeLocally = true; } } else 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 (!connected || !reliable) { if (target == Target.All) { target = Target.Others; executeLocally = true; } else if (target == Target.AllSaved) { target = Target.OthersSaved; executeLocally = true; } } if (connected && TNManager.IsInChannel(channelID)) { byte packetID = (byte)((int)Packet.ForwardToAll + (int)target); BinaryWriter writer = TNManager.BeginSend(packetID); writer.Write(TNManager.playerID); writer.Write(channelID); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSend(channelID, reliable); } } if (executeLocally) { if (rfcID != 0) { Execute(rfcID, objs); } else { Execute(rfcName, objs); } } } /// <summary> /// Send a new RFC call to the specified target. /// </summary> void SendRFC(byte rfcID, string rfcName, string targetName, bool reliable, params object[] objs) { #if UNITY_EDITOR if (!Application.isPlaying) { return; } #endif if (mDestroyed || string.IsNullOrEmpty(targetName)) { return; } if (targetName == TNManager.playerName) { if (rfcID != 0) { Execute(rfcID, objs); } else { Execute(rfcName, objs); } } else { BinaryWriter writer = TNManager.BeginSend(Packet.ForwardByName); writer.Write(TNManager.playerID); writer.Write(targetName); writer.Write(channelID); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSend(channelID, reliable); } } /// <summary> /// Send a new remote function call to the specified player. /// </summary> void SendRFC(byte rfcID, string rfcName, int target, bool reliable, params object[] objs) { if (mDestroyed) { return; } if (TNManager.isConnected) { BinaryWriter writer = TNManager.BeginSend(Packet.ForwardToPlayer); writer.Write(TNManager.playerID); writer.Write(target); writer.Write(channelID); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSend(channelID, reliable); } else if (target == TNManager.playerID) { if (rfcID != 0) { Execute(rfcID, objs); } else { Execute(rfcName, objs); } } } /// <summary> /// Broadcast a remote function call to all players on the network. /// </summary> void BroadcastToLAN(int port, byte rfcID, string rfcName, params object[] objs) { if (mDestroyed) { return; } BinaryWriter writer = TNManager.BeginSend(Packet.ForwardToAll); writer.Write(TNManager.playerID); writer.Write(channelID); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSendToLAN(port); }
public TNet.Player GetPlayerDetails(int playerID) { return(TNManager.GetPlayer(playerID)); }
public TNet.Player GetMyDetails() { return(TNManager.GetPlayer(TNManager.playerID)); }
public object GetPlayerData(int playerID) { return(TNManager.GetPlayer(playerID).data); }
public string GetPlayerName(int playerID) { return(TNManager.GetPlayer(playerID).name); }
public void OnDisconnect() { StopCoroutine("UpdateChannelList"); TNManager.Disconnect(); }
void OnClick() { TNManager.Destroy(gameObject); }
/// <summary> /// Convenience method mirroring TNManager.Instantiate. /// Instantiate a new game object in the behaviour's channel on all connected players. /// </summary> public void Instantiate(string funcName, string path, bool persistent, params object[] objs) { TNManager.Instantiate(tno.channelID, 0, funcName, path, persistent, objs); }
/// <summary> /// Convenience method mirroring TNManager.Instantiate. /// Instantiate a new game object in the behaviour's channel on all connected players. /// </summary> public void Instantiate(int rccID, string path, bool persistent, params object[] objs) { TNManager.Instantiate(tno.channelID, rccID, null, path, persistent, objs); }
private void button_connect_onClick() { button_connect.interactable = false; TNManager.Connect(connectIp, connectPort); }
/// <summary> /// Make sure we disconnect on exit. /// </summary> void OnDestroy() { if (mInstance == this) { if (isConnected) mClient.Disconnect(); mClient.StopUDP(); mInstance = null; } }
private void button_disconnect_onClick() { button_disconnect.interactable = false; TNManager.Disconnect(); }
public void Ready() { TNManager.SetPlayerData("isReady", true); Debug.Log(TNManager.players.Count); tno.Send("RFC_ReadyGame", Target.Host); }
public override void OnInspectorGUI() { TNObject obj = target as TNObject; if (Application.isPlaying) { EditorGUI.BeginDisabledGroup(true); EditorGUILayout.LabelField("Channel", obj.channelID.ToString()); EditorGUILayout.LabelField("ID", obj.uid.ToString()); if (obj.owner != null) { EditorGUILayout.LabelField("Owner", obj.owner.name + " (" + obj.ownerID + ")"); } else { EditorGUILayout.LabelField("Owner", obj.ownerID.ToString()); } TNet.Player host = TNManager.GetHost(TNManager.lastChannelID); EditorGUILayout.LabelField("Host", (host != null) ? host.name : "<none>"); 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.Instantiate.", 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 RFCs to function properly.", MessageType.Error); break; } } } } }
public void FireProjectile(Vector3 targetPoint) { TNManager.Instantiate(tno.channelID, "FireProjectile", baseWeapon.ProjectilePrefab.PathInResources, false, raycastOrigin.position, (targetPoint - raycastOrigin.position).normalized); }
/// <summary> /// This menu is shown if the client has not yet connected to the server. /// </summary> void DrawConnectMenu() { Rect rect = new Rect(Screen.width * 0.5f - 200f * 0.5f - mAlpha * 120f, Screen.height * 0.5f - 100f, 200f, 220f); // Show a half-transparent box around the upcoming UI GUI.color = new Color(1f, 1f, 1f, 0.5f); GUI.Box(UnityTools.PadRect(rect, 8f), ""); GUI.color = Color.white; GUILayout.BeginArea(rect); { GUILayout.Label("Server Address", text); mAddress = GUILayout.TextField(mAddress, input, GUILayout.Width(200f)); if (GUILayout.Button("Connect", button)) { // We want to connect to the specified destination when the button is clicked on. // "OnNetworkConnect" function will be called sometime later with the result. TNManager.Connect(mAddress); mMessage = "Connecting..."; } if (TNServerInstance.isActive) { GUI.backgroundColor = Color.red; if (GUILayout.Button("Stop the Server", button)) { // Stop the server, saving all the data TNServerInstance.Stop("server.dat"); mMessage = "Server stopped"; } } else { GUI.backgroundColor = Color.green; if (GUILayout.Button("Start a Local Server", button)) { #if UNITY_WEBPLAYER mMessage = "Can't host from the Web Player due to Unity's security restrictions"; #else // Start a local server, loading the saved data if possible // The UDP port of the server doesn't matter much as it's optional, // and the clients get notified of it via Packet.ResponseSetUDP. int udpPort = Random.Range(10000, 40000); TNLobbyClient lobby = GetComponent <TNLobbyClient>(); if (lobby == null) { TNServerInstance.Start(serverTcpPort, udpPort, "server.dat"); } else { TNServerInstance.Type type = (lobby is TNUdpLobbyClient) ? TNServerInstance.Type.Udp : TNServerInstance.Type.Tcp; TNServerInstance.Start(serverTcpPort, udpPort, lobby.remotePort, "server.dat", type); } mMessage = "Server started"; #endif } } GUI.backgroundColor = Color.white; if (!string.IsNullOrEmpty(mMessage)) { GUILayout.Label(mMessage, text); } } GUILayout.EndArea(); if (mAlpha > 0.01f) { rect.x = rect.x + (Screen.width - rect.xMin - rect.xMax) * mAlpha; DrawServerList(rect); } }
public override void OnEnter() { TNManager.GetChannelList(_CallOnChannelList); storeValue.Reset(); }
void Start() { TNManager.Create(prefab, transform.position, transform.rotation, persistent); Destroy(gameObject); }
public void CreateHarpoon(GameObject prefab, Vector3 pos, Quaternion rot, int ownerID, bool persistent = true) { TNManager.CreateEx(255, persistent, prefab, pos, rot, ownerID); Debug.Log("created"); }
void CreateBall() { Debug.Log(TNManager.lastChannelID); TNManager.Instantiate(TNManager.lastChannelID, "CreatePuck", "Puck", true, Vector3.zero); }
public override void OnEnter() { Data.Value = TNManager.GetChannelData <string>(Channel.Value, DataName.Value); Finish(); }
void PrintConfig(string path) { PrintConfig(path, TNManager.GetServerData(path)); }
public override void OnEnter() { TNManager.SetPlayerData(DataName.Value, Data.Value); Finish(); }
/// <summary> /// Ensure that there is only one instance of this class present. /// </summary> void Awake() { if (mInstance != null) { GameObject.Destroy(gameObject); } else { mInstance = this; rebuildMethodList = true; DontDestroyOnLoad(gameObject); mClient.onError += OnError; mClient.onConnect += OnConnect; mClient.onDisconnect += OnDisconnect; mClient.onJoinChannel += OnJoinChannel; mClient.onLeftChannel += OnLeftChannel; mClient.onLoadLevel += OnLoadLevel; mClient.onPlayerJoined += OnPlayerJoined; mClient.onPlayerLeft += OnPlayerLeft; mClient.onRenamePlayer += OnRenamePlayer; mClient.onCreate += OnCreateObject; mClient.onDestroy += OnDestroyObject; mClient.onForwardedPacket += OnForwardedPacket; } }
/// <summary> /// Notification of another player leaving the channel. /// </summary> void OnPlayerLeave(int channelID, Player p) { AddToChat(p.name + " has left channel " + channelID, Color.black); TNManager.JoinChannel(1000, "lobby", true, 500, null, true); }
public override void OnEnter() { TNManager.JoinRandomChannel(levelname.Value, persistent.Value, playerLimit.Value, null, leavecurrentchannel.Value); }