// Load an entire cutscene from a json file give its name with @sceneName public void Load(string sceneName) { string extension = string.Concat("Cutscenes/", sceneName, ".json"); // append the folder directory name and .json to our cutscene file as they are all json files // Path.Combine combines strings into a file path // Application.StreamingAssets points to Assets/StreamingAssets in the Editor, and the StreamingAssets folder in a build string filePath = Path.Combine(Application.streamingAssetsPath, extension); // check to see if the file exists if (File.Exists(filePath)) { Clear(); // reset all values in the editor // Read the json from the file into a string string dataAsJson = File.ReadAllText(filePath); // Pass the json to JsonUtility, and tell it to create a GameData object from it CutsceneData loadedData = JsonUtility.FromJson <CutsceneData>(dataAsJson); // Retrieve the allFrames data from loadedData - must add the elements of the array to the list for (int i = 0; i < loadedData.allCutsceneFrameData.Length; i++) { allFrames.Add(loadedData.allCutsceneFrameData[i]); } GoToFrame(allFrames.Count - 1); // look at the last possible frame Debug.Log("Load successful"); } // let the user know the file doesn't exist else { Debug.LogError("There is no cutscene named " + sceneName); } }
// Save the entire cutscene to a json file with @sceneName as its name public void Save(string sceneName) { // save the frame if it wasn't saved if (currentBatchList.Count > 0) { SaveFrame(); } // set up the CutsceneData CutsceneData cd = new CutsceneData(); cd.allCutsceneFrameData = allFrames.ToArray(); CreateSaveDirectory("Cutscenes"); // create the Cutscenes directory if it doesn't exist // save the data to json string json = JsonUtility.ToJson(cd, true); string endOfPath = string.Concat("Cutscenes/", sceneName, ".json"); // append the file directory and file type to the path we want to save it in string filePath = Path.Combine(Application.streamingAssetsPath, endOfPath); // we're saving the file to the StreamingAssets folder File.WriteAllText(filePath, json); // write and save the file AssetDatabase.Refresh(); // update the asset database }
// Load a cutscene data from a json file public void LoadFromJson() { string extension = string.Concat("Cutscenes/", cutsceneFileName, ".json"); // append the folder directory name and .json to our cutscene file as they are all json files // Path.Combine combines strings into a file path // Application.StreamingAssets points to Assets/StreamingAssets in the Editor, and the StreamingAssets folder in a build string filePath = Path.Combine(Application.streamingAssetsPath, extension); if (File.Exists(filePath)) { // Read the json from the file into a string string dataAsJson = File.ReadAllText(filePath); // Pass the json to JsonUtility, and tell it to create a GameData object from it CutsceneData loadedData = JsonUtility.FromJson <CutsceneData>(dataAsJson); // Retrieve the allRoundData property of loadedData allFrames = loadedData.allCutsceneFrameData; } else { Debug.LogError("Cannot load cutscene data!"); } }
// Start is called before the first frame update void Start() { fadeDir = true; fade = 0f; index = 0; artwork.color = new Color(1f, 1f, 1f, 0f); textBox.color = new Color(1f, 1f, 1f, 0f); CutsceneData cd = FindObjectOfType <CutsceneData>(); autoTime = cd.autoTime; art = cd.art; text = cd.text; textEmphasized = cd.textEmphasized; music = cd.music; musicLoop = cd.musicLoop; setupPrefab = cd.setupPrefab; Destroy(cd.gameObject); jukebox = FindObjectOfType <Jukebox>(); startedPlayingSong = false; autoTimer = 0f; }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.Return)) { state = CutsceneState.END; } #region state management t += Time.deltaTime; if (t >= t_goal) //state transition { #region start if (state == CutsceneState.START) { if (scenes.Count > 0) { state = CutsceneState.FADE_IN; //copy constructor: current scene = scene[0] current_scene = new CutsceneData(((CutsceneData)scenes[0]).t, ((CutsceneData)scenes[0]).text, ((CutsceneData)scenes[0]).img_index); t = 0.0f; t_goal = 1.0f; //constant fade in time text.text = current_scene.text; foreground.texture = fgs[current_scene.img_index]; //delete data scenes.RemoveAt(0); } else { t = 0.0f; t_goal = 1.0f; //constant end fade out time //no new scenes, end. state = CutsceneState.END; } } #endregion #region end else if (state == CutsceneState.END) { //it's over! Do nothing. state = CutsceneState.OFF; } #endregion #region fade in else if (state == CutsceneState.FADE_IN) { t = 0.0f; t_goal = current_scene.t; //wait for the allotted time state = CutsceneState.WAIT; } #endregion #region wait else if (state == CutsceneState.WAIT) { t = 0.0f; t_goal = 1.0f; //constant fade out time state = CutsceneState.FADE_OUT; } #endregion #region fade out else if (state == CutsceneState.FADE_OUT) { if (scenes.Count > 0) { state = CutsceneState.FADE_IN; //copy constructor: current scene = scene[0] current_scene = new CutsceneData(((CutsceneData)scenes[0]).t, ((CutsceneData)scenes[0]).text, ((CutsceneData)scenes[0]).img_index); t = 0.0f; t_goal = 1.0f; //constant start fade in time text.text = current_scene.text; foreground.texture = fgs[current_scene.img_index]; //TODO: img //delete data scenes.RemoveAt(0); } else { t = 0.0f; t_goal = 1.0f; //constant end fade out time //no new scenes, end. state = CutsceneState.END; } } #endregion } #endregion #region alpha control //Blending. if (state == CutsceneState.START) { //fade bg in background.color = new Color(1.0f, 1.0f, 1.0f, t / t_goal); //hide text text.color = new Color(1.0f, 1.0f, 1.0f, 0.0f); //hide fg foreground.color = new Color(1.0f, 1.0f, 1.0f, 0.0f); } else if (state == CutsceneState.END) { //fade bg out background.color = new Color(1.0f, 1.0f, 1.0f, 1.0f - (t / t_goal)); //hide text text.color = new Color(1.0f, 1.0f, 1.0f, 0.0f); //hide fg foreground.color = new Color(1.0f, 1.0f, 1.0f, 0.0f); } else if (state == CutsceneState.FADE_IN) { //full alpha bg background.color = new Color(1.0f, 1.0f, 1.0f, 1.0f); //fade text in text.color = new Color(1.0f, 1.0f, 1.0f, t / t_goal); //fade fg in foreground.color = new Color(1.0f, 1.0f, 1.0f, t / t_goal); } else if (state == CutsceneState.WAIT) { //full alpha bg background.color = new Color(1.0f, 1.0f, 1.0f, 1.0f); //full alpha text text.color = new Color(1.0f, 1.0f, 1.0f, 1.0f); //full alpha fg foreground.color = new Color(1.0f, 1.0f, 1.0f, 1.0f); } else if (state == CutsceneState.FADE_OUT) { //full alpha bg background.color = new Color(1.0f, 1.0f, 1.0f, 1.0f); //fade text out text.color = new Color(1.0f, 1.0f, 1.0f, 1.0f - (t / t_goal)); //fade fg out foreground.color = new Color(1.0f, 1.0f, 1.0f, 1.0f - (t / t_goal)); } else { //off background.color = new Color(1.0f, 1.0f, 1.0f, 0.0f); text.color = new Color(1.0f, 1.0f, 1.0f, 0.0f); foreground.color = new Color(1.0f, 1.0f, 1.0f, 0.0f); } #endregion }
// Update is called once per frame void Update() { if ( Input.GetKeyDown ( KeyCode.Space ) || Input.GetKeyDown ( KeyCode.Return ) ) { state = CutsceneState.END; } #region state management t += Time.deltaTime; if ( t >= t_goal ) //state transition { #region start if ( state == CutsceneState.START ) { if ( scenes.Count > 0 ) { state = CutsceneState.FADE_IN; //copy constructor: current scene = scene[0] current_scene = new CutsceneData( ((CutsceneData)scenes[0]).t, ((CutsceneData)scenes[0]).text, ((CutsceneData)scenes[0]).img_index ); t = 0.0f; t_goal = 1.0f; //constant fade in time text.text = current_scene.text; foreground.texture = fgs[current_scene.img_index]; //delete data scenes.RemoveAt ( 0 ); } else { t = 0.0f; t_goal = 1.0f; //constant end fade out time //no new scenes, end. state = CutsceneState.END; } } #endregion #region end else if ( state == CutsceneState.END ) { //it's over! Do nothing. state = CutsceneState.OFF; } #endregion #region fade in else if ( state == CutsceneState.FADE_IN ) { t = 0.0f; t_goal = current_scene.t; //wait for the allotted time state = CutsceneState.WAIT; } #endregion #region wait else if ( state == CutsceneState.WAIT ) { t = 0.0f; t_goal = 1.0f; //constant fade out time state = CutsceneState.FADE_OUT; } #endregion #region fade out else if ( state == CutsceneState.FADE_OUT ) { if ( scenes.Count > 0 ) { state = CutsceneState.FADE_IN; //copy constructor: current scene = scene[0] current_scene = new CutsceneData( ((CutsceneData)scenes[0]).t, ((CutsceneData)scenes[0]).text, ((CutsceneData)scenes[0]).img_index ); t = 0.0f; t_goal = 1.0f; //constant start fade in time text.text = current_scene.text; foreground.texture = fgs[current_scene.img_index]; //TODO: img //delete data scenes.RemoveAt ( 0 ); } else { t = 0.0f; t_goal = 1.0f; //constant end fade out time //no new scenes, end. state = CutsceneState.END; } } #endregion } #endregion #region alpha control //Blending. if ( state == CutsceneState.START ) { //fade bg in background.color = new Color( 1.0f, 1.0f, 1.0f, t / t_goal ); //hide text text.color = new Color( 1.0f, 1.0f, 1.0f, 0.0f ); //hide fg foreground.color = new Color( 1.0f, 1.0f, 1.0f, 0.0f ); } else if ( state == CutsceneState.END ) { //fade bg out background.color = new Color( 1.0f, 1.0f, 1.0f, 1.0f - ( t / t_goal ) ); //hide text text.color = new Color( 1.0f, 1.0f, 1.0f, 0.0f ); //hide fg foreground.color = new Color( 1.0f, 1.0f, 1.0f, 0.0f ); } else if ( state == CutsceneState.FADE_IN ) { //full alpha bg background.color = new Color( 1.0f, 1.0f, 1.0f, 1.0f ); //fade text in text.color = new Color( 1.0f, 1.0f, 1.0f, t / t_goal ); //fade fg in foreground.color = new Color( 1.0f, 1.0f, 1.0f, t / t_goal ); } else if ( state == CutsceneState.WAIT ) { //full alpha bg background.color = new Color( 1.0f, 1.0f, 1.0f, 1.0f ); //full alpha text text.color = new Color( 1.0f, 1.0f, 1.0f, 1.0f ); //full alpha fg foreground.color = new Color( 1.0f, 1.0f, 1.0f, 1.0f ); } else if ( state == CutsceneState.FADE_OUT ) { //full alpha bg background.color = new Color( 1.0f, 1.0f, 1.0f, 1.0f ); //fade text out text.color = new Color( 1.0f, 1.0f, 1.0f, 1.0f - ( t / t_goal ) ); //fade fg out foreground.color = new Color( 1.0f, 1.0f, 1.0f, 1.0f - ( t / t_goal ) ); } else { //off background.color = new Color( 1.0f, 1.0f, 1.0f, 0.0f ); text.color = new Color( 1.0f, 1.0f, 1.0f, 0.0f ); foreground.color = new Color( 1.0f, 1.0f, 1.0f, 0.0f ); } #endregion }