/// <summary> /// Load a gameobject from a file /// </summary> /// <param name="filePath"></param> /// <param name="roomToInstantiateIn"></param> /// <param name="position"> </param> /// <param name="rotation"> </param> /// <param name="allowNetworkInstantiateIfHasNetworkView"></param> /// <param name="visibleToAll">makes all players in the room subscribed to the object</param> /// <param name="owner">owner of the loaded object if network instantiated. By default, it is the server</param> /// <returns></returns> public static GameObject Load(string filePath, Room roomToInstantiateIn, bool allowNetworkInstantiateIfHasNetworkView = false, Vector3? position = null, Quaternion? rotation = null, bool visibleToAll = true, Player owner = null) { var dser = new GameObject(); dser.Room = roomToInstantiateIn; var awakes = new List<Action>(); var config = new YamlConfig(); var actualFilePath = Path.Combine(ResourceFolder, filePath + ".prefab"); config.AddActivator<GameObject>(() => { //be returning an object we've already created, the AddComponent will work properly return dser; }); //config.AddActivator < List<ComponentTracker>>(() => //{ // return dser.components; //}); var trackers = new Stack<GameObject.ComponentTracker>(); config.AddActivator<GameObject.ComponentTracker>(() => { var ntrack = new GameObject.ComponentTracker(); trackers.Push(ntrack); return ntrack; }); foreach (Type t in GetComponentTypes()) { Type tLocal = t; if (tLocal == typeof(NetworkView) && !allowNetworkInstantiateIfHasNetworkView) { Debug.LogWarning("[Resources.Load] file {0} has a NetworkView component on it, but was run as to not network instantiate. It will not be networked.", actualFilePath); } GameObject dser1 = dser; config.AddActivator(tLocal, () => { Action awake; var ntrack = trackers.Pop(); var ret = dser1.DeserializeAddComponent(tLocal, out awake, ntrack); awakes.Add(awake); return ret; }); } var serializer = new YamlSerializer(config); serializer.DeserializeFromFile(actualFilePath, typeof(GameObject)); if (dser.Resource == null) dser.Resource = filePath; if (position.HasValue) dser.Position = position.Value; if (rotation.HasValue) dser.Rotation = rotation.Value; roomToInstantiateIn.OnGameObjectAdded(dser); foreach (var awake in awakes) if (awake != null) awake(); dser.OnComponentAfterDeserialization(); if (allowNetworkInstantiateIfHasNetworkView && dser.GetComponent<NetworkView>() != null) roomToInstantiateIn.ResourceNetworkInstantiate(dser, visibleToAll, owner); return dser; }
void SendNetworkInstantiate(List<NetConnection> connections, GameObject gobj) { var netView = gobj.GetComponent<NetworkView>(); if (netView == null) { Debug.Log("[Instantiate] the specified object {0} does not have a network view to actually use for network instantiation", gobj.Resource); } var message = PNetServer.peer.CreateMessage(33 + (gobj.Resource.Length * 2)); message.Write(RPCUtils.Instantiate); message.Write(gobj.Resource); message.Write(netView.viewID.guid); message.Write(netView.owner.Id); var vs = new Vector3Serializer(gobj.Position); vs.OnSerialize(message); var qs = new QuaternionSerializer(gobj.Rotation); qs.OnSerialize(message); if (connections.Count > 0) PNetServer.peer.SendMessage(message, connections , NetDeliveryMethod.ReliableOrdered, Channels.STATIC_UTILS); }