private void ReceiveCreateView(NetMessage message, NetConnection server) { if (!server.IsServer && !server.IsPeer) { return; } int viewId = (int)message.Parameters[0]; int group = (int)message.Parameters[1]; string prefabRoot = (string)message.Parameters[2]; var stream = (NetStream)message.Parameters[3]; NetView.Relation relation = default(NetView.Relation); NetConnection controller = null; switch (message.MessageId) { case (int)ViewCmd.CreateOwnerView: controller = Socket.Self; relation = NetView.Relation.Owner; break; case (int)ViewCmd.CreatePeerView: relation = NetView.Relation.Peer; break; case (int)ViewCmd.CreateCreatorView: server = Socket.Self; relation = NetView.Relation.Creator; break; case (int)ViewCmd.CreateProxyView: relation = NetView.Relation.Proxy; break; } if (relation == NetView.Relation.Creator || relation == NetView.Relation.Peer) { var ipendpoint = (IPEndPoint)message.Parameters[4]; if (ipendpoint != null) { if (Socket.EndpointConnected(ipendpoint)) { controller = Socket.EndpointToConnection(ipendpoint); } else { NetLog.Error("Failed to create view, controller endpoint not connected: " + ipendpoint); } } } var view = CreateView(controller, server, viewId, group, prefabRoot, relation); view.TriggerReadInstantiateData(stream); if (OnNetViewCreated != null) { OnNetViewCreated(view); } }
private NetView InstantiateView(string prefabRoot, NetView.Relation relation) { var viewObject = InstantiatePrefab != null ? InstantiatePrefab(prefabRoot, relation) : (GameObject)Instantiate(Resources.Load(Prefab(prefabRoot, relation))); var view = viewObject.GetComponent <NetView>(); if (view != null) { return(view); } Destroy(viewObject); throw new Exception("Prefab does not have a NetView component attached."); }
private static string Prefab(string prefabRoot, NetView.Relation relation) { if (relation == NetView.Relation.Creator) { return(prefabRoot + "@Creator"); } if (relation == NetView.Relation.Owner) { return(prefabRoot + "@Owner"); } if (relation == NetView.Relation.Peer) { return(prefabRoot + "@Peer"); } return(prefabRoot + "@Proxy"); }
private NetView CreateView(NetConnection controller, NetConnection server, int viewId, int group, string prefabRoot, NetView.Relation relation) { NetView view = null; if (ViewLookup.ContainsKey(viewId)) { NetView oldView = ViewLookup[viewId]; if (oldView.CurrentRelation == relation) { view = oldView; } else if (server == Socket.Self) { NetScope oldScope = oldView.Scope; Vector3 oldPos = oldView.transform.position; if (oldView.Server != server) { SendChangeViewServer(viewId); } DestroyView(oldView); view = InstantiateView(prefabRoot, relation); view.InternalScope = oldScope; view.transform.position = oldPos; } } if (view == null) { view = InstantiateView(prefabRoot, relation); } view.Server = server; view.Id = viewId; view.PrefabRoot = prefabRoot; view.Group = group; view.CurrentRelation = relation; if (controller != null) { view.AddController(controller); controller.AddAuthorization(view.Id); controller.AddToGroup(group); if (controller != Socket.Self) { controller.View = view; if (controller.InternalScope != null && view.Controllers.Count == 1) { view.InternalScope = controller.InternalScope; } controller.InternalScope = null; } } view.Scope.FireInEvent(); RegisterNetView(view); return(view); }