Skip to content

Instantly share code, notes, and snippets.

@lTyl
Last active June 14, 2022 16:59
Show Gist options
  • Save lTyl/73c5cd1a828571d51e0f58d1f527211b to your computer and use it in GitHub Desktop.
Save lTyl/73c5cd1a828571d51e0f58d1f527211b to your computer and use it in GitHub Desktop.
CutsceneInterpreter.cs
using System;
using System.Collections;
using UniRx;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
namespace Behaviours.Tests {
public enum CutsceneStatus {
Running,
Complete
}
public class CutsceneData {
public CutsceneStatus status;
public string data;
}
public class CutsceneInterpreter : MonoBehaviour {
public static string WebBuildIdentifier = "://";
public Subject<CutsceneData> lineObs = new Subject<CutsceneData>();
private string[] currentCutsceneData;
private int currentPointer = 0;
void Start() {
// This handles loading. Recommended to use your own loader
LoadCutscene("fancy.txt", OnCutsceneStart);
// Client-side usage. Callback will emit for each line.
// TakeWhile automagically handles cleanup. When a status other than "Running" is emitted, the observer will self-dispose.
lineObs
.TakeWhile(d => d.status == CutsceneStatus.Running)
.Subscribe(cutscene => {
Debug.Log(cutscene.status);
Debug.Log(cutscene.data);
// Add special command handling here.
});
}
// Instead of being a void, you can return the inner Observable. This allows you to do a "cold" observable and control exactly when you want the internal input/timer behaviour to start.
private void OnCutsceneStart(string[] lines) {
// Do whatever here. I am emitting a new entry each second.
// Make sure to add code that checks for the end of the scene and emits a onComplete.
Observable
.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(1))
.Subscribe((t) => {
CutsceneStatus status = CutsceneStatus.Running;
currentPointer++;
if (currentPointer >= lines.Length) {
currentPointer = lines.Length - 1;
status = CutsceneStatus.Complete;
}
lineObs.OnNext(new CutsceneData{status = status, data = lines[currentPointer]});
});
}
// Ideally this returns an Observable. Then you can hook into OnNext, Onerror and OnComplete to handle specific state flow.
private void LoadCutscene(string pathAndFile, Action<string[]> onSuccess) {
string downloadPath = Path.Combine(Application.streamingAssetsPath, pathAndFile);
// Certain mobile platforms and WebGL will return a web resource that must be retrieved via a HTTP request
if (downloadPath.Contains(WebBuildIdentifier) || downloadPath.Contains(WebBuildIdentifier + '/')) {
UnityWebRequest www = UnityWebRequest.Get(downloadPath);
www.SendWebRequest().AsObservable().Subscribe(d => Debug.Log(d.progress), () => {
if (www.isNetworkError || www.isHttpError) {
throw new Exception("I boomed.");
} else {
onSuccess(www.downloadHandler.text.Split('\n'));
}
});
}
// Verify file exists
if (!File.Exists(downloadPath)) {
throw new FileNotFoundException("I boomed.");
} else {
// Use an async/non-blocking loading approach
onSuccess(File.ReadAllLines(downloadPath));
}
}
}
}
Hello
These messages
Are delayed
By One second
Each.
Copyright 2022 Tyler Deren
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment