Exemple #1
0
    /// <summary>
    /// Deserializes a set of serialized frames into the scene
    /// </summary>
    /// <param name="frames"></param>
    internal static void DeserializeFrames(SerializableGame gameData, int sceneBuildIndex)
    {
        SerializableGame data = GameController.instance.currentGame;

        // Create a dictionarry with ALL saved frames
        foreach (SerializedFrame serializedFrame in gameData.frames)
        {
            data[serializedFrame.id] = serializedFrame;
        }

        _frames = new List <Frame>();
        _frames.AddRange(FindObjectsOfType <Frame>());

        // Check the current scene of all stationary frames
        foreach (Frame frame in _frames)
        {
            if (data.ContainsKey(frame.id))
            {
                SerializedFrame serializedFrame = data[frame.id];

                // stationary is not in the scene..
                if (serializedFrame.scene != sceneBuildIndex)
                {
                    Destroy(frame.gameObject);
                }
            }
        }

        SerializedFrame[] sceneFrames = gameData.GetFramesBySceneBuildIndex(sceneBuildIndex);

        // Create all the frames needed in the scene
        foreach (SerializedFrame serializedFrame in sceneFrames)
        {
            Frame frame = FindFrameById(serializedFrame.id);

            // If the frame doesn't exist in the level yet, create it!
            if (frame == null)
            {
                // If the frame has been destroyed in the last session, we won't need it ever again!
                if (serializedFrame.destroyed)
                {
                    data.Remove(serializedFrame.id);
                    continue;
                }

                frame = Instantiate(NeverdawnDatabase.GetPrefab(serializedFrame.prefab));
            }

            if (serializedFrame.destroyed)
            {
                Destroy(frame.gameObject);
                continue;
            }

            frame.id                    = serializedFrame.id;
            frame.prefab                = serializedFrame.prefab;
            frame.transform.position    = serializedFrame.position.ToVector3();
            frame.transform.eulerAngles = serializedFrame.rotation.ToVector3();

            frame.Init();
        }

        // Load the frame components
        foreach (SerializedFrame serializedFrame in sceneFrames)
        {
            Frame frame = FindFrameById(serializedFrame.id);
            frame.DeserializeComponents(serializedFrame.componentData);
        }
    }