Beispiel #1
0
    public static void ActivateRoom(GameObject room, bool active)
    {
        if (!NetworkManagerCustom.IsServer)
        {
            room.SetActive(active);
            return;
        }

        if (active)
        {
            room.SetActive(true);
            if (room.GetComponent <Room>().Initialized)
            {
                Vector2Int pos = room.GetComponent <Room>().Position;

                Transform parent = room.transform.Find("Objects");
                foreach (SerializationManager.LoadedWorld.LoadedObject loadedObject in SerializationManager.World.Objects[pos.x, pos.y])
                {
                    GameObject gameObject = ObjectsManager.SpawnGameObject(Utils.FindAssetID(loadedObject.AssetID),
                                                                           Vector2.zero, Vector3.zero, parent, true);
                    SerializationManager.DeserializeObject(gameObject, loadedObject.Data);
                    NetworkServer.Spawn(gameObject);
                }
            }
            else
            {
                InitializeRoom(room);
            }

            SerializationManager.MarkDirtySave = true;
            List <string>         networkIds = new List <string>();
            List <List <string> > data       = new List <List <string> >();
            Transform             parent0    = room.transform.Find("Objects");
            for (int i = 0; i < parent0.childCount; i++)
            {
                if (parent0.GetChild(i).GetComponent <ISerializableObject>() != null)
                {
                    networkIds.Add(parent0.transform.GetChild(i).GetComponent <NetworkIdentity>().netId.ToString());
                    data.Add(SerializationManager.SerializeObject(parent0.GetChild(i).gameObject));
                }
            }
            MessageManager.SetActiveRoomClientMessage.SendToAllClients(new ActiveRoomMessage(room.GetComponent <Room>().Position, networkIds, data));
        }
        else
        {
            SerializationManager.SaveWorld();
            Transform parent = room.transform.Find("Objects");
            for (int i = 0; i < parent.childCount; i++)
            {
                if (parent.GetChild(i).gameObject.GetComponent <NetworkIdentity>() != null)
                {
                    NetworkServer.Destroy(parent.GetChild(i).gameObject);
                }
            }
            room.SetActive(false);
        }
    }