Skip to content

Instantly share code, notes, and snippets.

@kanchi0914
Last active July 19, 2022 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanchi0914/5b75671250f4598cb72ab65c5373a453 to your computer and use it in GitHub Desktop.
Save kanchi0914/5b75671250f4598cb72ab65c5373a453 to your computer and use it in GitHub Desktop.
Sample script for adventure games
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