Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active August 29, 2015 14:10
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tsubaki/f09f11728636232ef12e to your computer and use it in GitHub Desktop.
超簡単なテキスト制御4
using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
[RequireComponent(typeof( TextController))]
public class ScenarioManager : SingletonMonoBehaviourFast<ScenarioManager> {
public string LoadFileName;
private string[] m_scenarios;
private int m_currentLine = 0;
private bool m_isCallPreload = false;
private TextController m_textController;
private CommandController m_commandController;
void RequestNextLine ()
{
var currentText = m_scenarios[m_currentLine];
m_textController.SetNextLine(CommandProcess(currentText));
m_currentLine ++;
m_isCallPreload = false;
}
public void UpdateLines(string fileName)
{
var scenarioText = Resources.Load<TextAsset>("Scenario/" + fileName);
if( scenarioText == null ){
Debug.LogError("シナリオファイルが見つかりませんでした");
Debug.LogError("ScenarioManagerを無効化します");
enabled = false;
return;
}
m_scenarios = scenarioText.text.Split(new string[]{"@br"}, System.StringSplitOptions.None);
m_currentLine = 0;
Resources.UnloadAsset(scenarioText);
}
private string CommandProcess(string line)
{
var lineReader = new StringReader(line);
var lineBuilder = new StringBuilder();
var text = string.Empty;
while( (text = lineReader.ReadLine()) != null )
{
var commentCharacterCount = text.IndexOf("//");
if( commentCharacterCount != -1 ){
text = text.Substring(0, commentCharacterCount );
}
if(! string.IsNullOrEmpty( text ) ){
if( text[0] == '@' && m_commandController.LoadCommand(text))
continue;
lineBuilder.AppendLine(text);
}
}
return lineBuilder.ToString();
}
#region UNITY_CALLBACK
void Start () {
m_textController = GetComponent<TextController>();
m_commandController = GetComponent<CommandController>();
UpdateLines(LoadFileName);
RequestNextLine();
}
void Update ()
{
if( m_textController.IsCompleteDisplayText ){
if( m_currentLine < m_scenarios.Length)
{
if( !m_isCallPreload )
{
m_commandController.PreloadCommand(m_scenarios[m_currentLine]);
m_isCallPreload = true;
}
if( Input.GetMouseButtonDown(0)){
RequestNextLine();
}
}
}else{
if(Input.GetMouseButtonDown(0)){
m_textController.ForceCompleteDisplayText();
}
}
}
#endregion
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
// Messageboxのテキストを制御する
public class TextController : MonoBehaviour
{
[SerializeField][Range(0.001f, 0.3f)]
public float intervalForCharacterDisplay = 0.05f;
private string currentText = string.Empty;
private float timeUntilDisplay = 0;
private float timeElapsed = 1;
private int lastUpdateCharacter = -1;
[SerializeField]
private Text _uiText;
public bool IsCompleteDisplayText
{
get { return Time.time > timeElapsed + timeUntilDisplay; }
}
// 強制的に全文表示する
public void ForceCompleteDisplayText ()
{
timeUntilDisplay = 0;
}
// 次に表示する文字列をセットする
public void SetNextLine(string text)
{
currentText = text;
timeUntilDisplay = currentText.Length * intervalForCharacterDisplay;
timeElapsed = Time.time;
lastUpdateCharacter = -1;
}
#region UNITY_CALLBACK
void Update ()
{
int displayCharacterCount = (int)(Mathf.Clamp01((Time.time - timeElapsed) / timeUntilDisplay) * currentText.Length);
if( displayCharacterCount != lastUpdateCharacter ){
_uiText.text = currentText.Substring(0, displayCharacterCount);
lastUpdateCharacter = displayCharacterCount;
}
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment