示例#1
0
    // Build the saved game object from the scene
    public void BuildFromScene()
    {
        // Fetch relevant data from scene
        BPM = GameObject.Find("Control").GetComponent <SimulationControl>().BPM;
        GameObject[] gameObjects = Utils.ChildrenOf(GameObject.Find("/Music"));

        List <SaveSoundActivator> saveSoundActivators = new List <SaveSoundActivator>();
        List <SaveSoundPlayer>    saveSoundPlayers    = new List <SaveSoundPlayer>();

        // Go through each game object
        for (int i = 0; i < gameObjects.Length; ++i)
        {
            // Check for sound activators
            SoundActivator soundActivator = gameObjects[i].GetComponent <SoundActivator>();
            if (soundActivator != null)
            {
                saveSoundActivators.Add(
                    new SaveSoundActivator(i, soundActivator.transform.position, System.Array.IndexOf(gameObjects, soundActivator.target),
                                           soundActivator.beatsFromTarget, soundActivator.active));
            }

            // Check for sound players
            SoundPlayer soundPlayer = gameObjects[i].GetComponent <SoundPlayer>();
            if (soundPlayer != null)
            {
                saveSoundPlayers.Add(
                    new SaveSoundPlayer(i, soundPlayer.transform.position, System.Array.IndexOf(gameObjects, soundPlayer.target),
                                        soundPlayer.beatsFromTarget, soundPlayer.sound.name));
            }
        }

        // Convert to arrays
        soundActivators = saveSoundActivators.ToArray();
        soundPlayers    = saveSoundPlayers.ToArray();
    }
示例#2
0
    // Populate the scene based on data in object
    public void PopulateScene()
    {
        // Clear existing music
        GameObject musicObject = GameObject.Find("/Music");

        GameObject[] currentObjects = Utils.ChildrenOf(musicObject);
        foreach (GameObject gameObject in currentObjects)
        {
            Object.Destroy(gameObject);
        }

        // Keep track of <id, gameobject> pairs
        Dictionary <int, GameObject> gameObjectDictionary = new Dictionary <int, GameObject>();

        // Set BPM
        GameObject controlObject = GameObject.Find("Control");

        controlObject.GetComponent <SimulationControl>().SetBPM(BPM);

        // Load prefabs
        GameObject soundActivatorObject = (GameObject)Resources.Load("Prefabs/sound_activator");
        GameObject soundPlayerObject    = (GameObject)Resources.Load("Prefabs/sound_player");

        // Instantiate sound activators
        foreach (SaveSoundActivator saveSoundActivator in soundActivators)
        {
            GameObject gameObject = Object.Instantiate(soundActivatorObject);
            gameObjectDictionary.Add(saveSoundActivator.id, gameObject);
            gameObject.transform.position = saveSoundActivator.GetPosition();
            gameObject.transform.parent   = musicObject.transform;

            SoundActivator soundActivator = gameObject.GetComponent <SoundActivator>();
            soundActivator.beatsFromTarget = saveSoundActivator.beatsToInitialTarget;
            soundActivator.active          = saveSoundActivator.active;
        }

        // Instantiate sound players
        foreach (SaveSoundPlayer saveSoundPlayer in soundPlayers)
        {
            GameObject gameObject = Object.Instantiate(soundPlayerObject);
            gameObjectDictionary.Add(saveSoundPlayer.id, gameObject);
            gameObject.transform.position = saveSoundPlayer.GetPosition();
            gameObject.transform.parent   = musicObject.transform;

            SoundPlayer soundPlayer = gameObject.GetComponent <SoundPlayer>();
            soundPlayer.beatsFromTarget = saveSoundPlayer.beatsFromTarget;
            soundPlayer.sound           = (AudioClip)Resources.Load("Audio/" + saveSoundPlayer.audioClipName);
        }

        // Link sound activators
        foreach (SaveSoundActivator saveSoundActivator in soundActivators)
        {
            GameObject gameObject;
            gameObjectDictionary.TryGetValue(saveSoundActivator.id, out gameObject);

            GameObject other;
            if (gameObjectDictionary.TryGetValue(saveSoundActivator.initialTarget, out other))
            {
                gameObject.GetComponent <SoundActivator>().target = other;
            }
            else
            {
                Debug.LogError("While populating scene. Could not find referenced object with ID " + saveSoundActivator.initialTarget);
            }
        }

        // Link sound players
        foreach (SaveSoundPlayer saveSoundPlayer in soundPlayers)
        {
            GameObject gameObject;
            gameObjectDictionary.TryGetValue(saveSoundPlayer.id, out gameObject);

            GameObject other;
            if (gameObjectDictionary.TryGetValue(saveSoundPlayer.nextTarget, out other))
            {
                gameObject.GetComponent <SoundPlayer>().target = other;
            }
            else
            {
                Debug.LogError("While populating scene. Could not find referenced object with ID " + saveSoundPlayer.nextTarget);
            }
        }

        // Draw transitions
        controlObject.GetComponent <TransitionManager>().DrawTransitionsAfterUpdate();
    }