Last active
July 19, 2022 07:37
-
-
Save kanchi0914/5b75671250f4598cb72ab65c5373a453 to your computer and use it in GitHub Desktop.
Sample script for adventure games
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class GameController : MonoBehaviour { | |
[SerializeField] | |
Text scenarioMessage; | |
List<Scenario> scenarios = new List<Scenario>(); | |
Scenario currentScenario; | |
int index = 0; | |
class Scenario | |
{ | |
public string ScenarioID; | |
public List<string> Texts; | |
public List<string> Options; | |
public string NextScenarioID; | |
} | |
void Start () | |
{ | |
var scenario01 = new Scenario() | |
{ | |
ScenarioID = "scenario01", | |
Texts = new List<string>() | |
{ | |
"テスト文章1", | |
"テスト文章2", | |
"テスト文章3", | |
"テスト文章4", | |
"テスト文章5" | |
} | |
}; | |
SetScenario(scenario01); | |
} | |
void Update() | |
{ | |
if (currentScenario != null) | |
{ | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
SetNextMessage(); | |
} | |
} | |
} | |
void SetScenario(Scenario scenario) | |
{ | |
currentScenario = scenario; | |
scenarioMessage.text = currentScenario.Texts[0]; | |
} | |
void SetNextMessage() | |
{ | |
if (currentScenario.Texts.Count > index + 1) | |
{ | |
index++; | |
scenarioMessage.text = currentScenario.Texts[index]; | |
} | |
else | |
{ | |
ExitScenario(); | |
} | |
} | |
void ExitScenario() | |
{ | |
scenarioMessage.text = ""; | |
index = 0; | |
if (string.IsNullOrEmpty(currentScenario.NextScenarioID)) | |
{ | |
currentScenario = null; | |
} | |
else | |
{ | |
var nextScenario = scenarios.Find | |
(s => s.ScenarioID == currentScenario.NextScenarioID); | |
currentScenario = nextScenario; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment