超簡単なテキスト制御4
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 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 | |
} |
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 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