質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

974閲覧

Unityでの文字の扱い(1文字ずつ表示、リッチテキスト)

kagaribisou

総合スコア17

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2018/08/24 05:46

Unityでリッチテキスト+αを考慮して、1文字ずつ表示したいと考えています。
また、文章の作成は。.txtファイルを読み込んで行おうとしています。
実行すると、エラーメッセージが出ることなく、Unityが反応しなくなります。
OutputChar関数が問題を起こしていると思っていますがお手上げです。
アドバイスよろしくお願いします。

Test.txt
[text]<speed=1.00>速度を変える</speed>
[end]

コード using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.IO; using System.Text; using System; public class TextDisplay : MonoBehaviour { public string[] sentences;//文章を格納する public string file;//分ける前の文章 [SerializeField] Text uiText; //UiTextへの参照 [SerializeField] Text uiName; //UiNameへの参照 [SerializeField] [Range(0.001f, 0.3f)] float intervalForCharDisplay = 0.05f;//1文字の表示にかける時間 private int currentSentenceNum = -1;//現在表示している文章番号 private string currentSentence = string.Empty;//現在の文字列 private float timeTmpDisplay = 0.0f;//文字表示の一時的なインターバル private bool isTmpTime = false; private float timeBeganDisplay = 1;//文字列の表示を開始した時間 [SerializeField] public string filename;// = string.Empty;//読み込みたいファイル名 private bool isTimerSet = false; int displayCharCount = 0; string[] endTagStack = {null,null,null,null,null}; int tagNestNum = 0; // Use this for initialization void Start () { SetSentence(); SetNextSentence (); } // Update is called once per frame void Update () { //文章の表示完了/未完了 if (IsDisplayComplete()) { //最後の文章ではない&ボタンが押された if((currentSentenceNum + 1) < sentences.Length && Input.GetKeyUp(KeyCode.Space)) { SetNextSentence (); } } else { //ボタンが押された if (Input.GetKeyUp(KeyCode.Space)) { uiText.text = sentences[currentSentenceNum]; displayCharCount = sentences[currentSentenceNum].Length; } else if (isNextCharTime()) { OutputChar(); } } } //------------------------------------------------------------------------------- //文字を出力する void OutputChar() { Debug.Log("callOutputChar"); displayCharCount++; while(currentSentence.Substring(displayCharCount - 1,1) == "<") { int startIndex = displayCharCount - 1; int endIndex = currentSentence.IndexOf(">", startIndex + 1); int tagLength = (endIndex - startIndex) + 1; bool isCanDChack = (currentSentence.Length - startIndex) > 7; bool isTagDelay = false; if (isCanDChack) { Debug.Log("isCanDChack = true"); isTagDelay = currentSentence.Substring(startIndex + 1, 6) == "/speed" || currentSentence.Substring(startIndex + 1, 5) == "speed"; } Debug.Log("isTagDelay=" + isTagDelay); if(isTagDelay) { Debug.Log("call delay"); if (currentSentence.Substring(startIndex,tagLength).Contains("=")) timeTmpDisplay = float.Parse(currentSentence.Substring(startIndex + 7, 4));//数値に変換 currentSentence.Remove(startIndex, tagLength); isTmpTime = (isTmpTime == false);//スイッチを入れ替える continue; } else if(currentSentence.Substring(startIndex + 1,1) == "/") { displayCharCount += tagLength;//タグの文字数だけ表示を進める endTagStack[tagNestNum - 1] = null; tagNestNum--; continue; } displayCharCount += tagLength;//タグの文字数だけ表示を進める string endTag = "</" + currentSentence.Substring(startIndex + 1, tagLength - 1); if (endTag.Contains("=")) endTag = endTag.Remove(endTag.IndexOf("="), endTag.Length - endTag.IndexOf("=") - 1); endTagStack[tagNestNum] = endTag; tagNestNum++; } uiText.text = currentSentence.Substring(0, displayCharCount); //リッチテキスト用 if(endTagStack[0] != null) { for(int i = 1; i < tagNestNum + 1; i++) { uiText.text += endTagStack[tagNestNum - i]; } } } //------------------------------------------------------------------------------- //次の文章をセットする void SetNextSentence() { Debug.Log("callSetSentence"); currentSentenceNum++; currentSentence = sentences [currentSentenceNum]; displayCharCount = 0; for (int i = 0; i < endTagStack.Length; i++) endTagStack[i] = null; tagNestNum = 0; } //------------------------------------------------------------------------------- // bool IsDisplayComplete() { return displayCharCount >= sentences[currentSentenceNum].Length; } //----------------------------------------------------- // void Readfile() { //"FileName".txtを読み込む TextAsset t = Resources.Load("text/Test", typeof(TextAsset)) as TextAsset; file = t.text; } //------------------------------------------------------------------------------- //読み込んだファイルをセットする void SetSentence() { string Type = ""; int startIndex = 0, endIndex = 0; //ファイルを読み込む Readfile(); int textSize = (file.Length - file.Replace("[text]", "").Length)/6; sentences = new string[textSize]; startIndex = file.IndexOf('['); Debug.Log(startIndex); Debug.Log("file.Length" + file.Length); int numSentence = 0; while (true) { endIndex = file.IndexOf(']', startIndex + 1); Type = file.Substring(startIndex + 1, (endIndex - startIndex - 1)); if (Type == "end") break; startIndex = file.IndexOf('[', endIndex + 1);//次の命令までのインデックス if(Type == "text") { sentences[numSentence] = file.Substring(endIndex + 1, ((startIndex - 1) - endIndex)); numSentence++; Type = ""; } else if (Type == "name") { uiName.text = file.Substring(endIndex + 1, ((startIndex - 1) - endIndex)); Type = ""; } } } //------------------------------------------------------------------------------- //次の文字を表示する時間になったらtrueを返す関数 bool isNextCharTime() { if (isTimerSet) { float T; if (isTmpTime) T = timeTmpDisplay; else { T = intervalForCharDisplay; timeTmpDisplay = 0.0f; } if(Time.time - timeBeganDisplay > T) { isTimerSet = false; timeBeganDisplay = 0; return true; } return false; } isTimerSet = true; timeBeganDisplay = Time.time; return false; } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

代入してませんでした!!!
申し訳ないです。投稿を消したい

C#

1コード 2//文字を出力する 3 void OutputChar() 4 { 5 Debug.Log("callOutputChar"); 6 displayCharCount++; 7 while(currentSentence.Substring(displayCharCount - 1,1) == "<") 8 { 9 int startIndex = displayCharCount - 1; 10 int endIndex = currentSentence.IndexOf(">", startIndex + 1); 11 int tagLength = (endIndex - startIndex) + 1; 12 13 bool isCanDChack = (currentSentence.Length - startIndex) > 7; 14 bool isTagDelay = false; 15 if (isCanDChack) 16 { 17 Debug.Log("isCanDChack = true"); 18 isTagDelay = currentSentence.Substring(startIndex + 1, 6) == "/speed"; 19 if(!isTagDelay) 20 isTagDelay = currentSentence.Substring(startIndex + 1, 5) == "speed"; 21 } 22 Debug.Log("isTagDelay=" + isTagDelay); 23 if(isTagDelay) 24 { 25 Debug.Log("call delay"); 26 if (currentSentence.Substring(startIndex,tagLength).Contains("=")) 27 timeTmpDisplay = float.Parse(currentSentence.Substring(startIndex + 7, 4));//数値に変換 28 29 30 31 32 ====> 33 ====> "currentSentence =" currentSentence.Remove(startIndex, tagLength); 34 ====> 35 36 37 38 39 40 isTmpTime = (isTmpTime == false);//スイッチを入れ替える 41 continue; 42 } 43 else if(currentSentence.Substring(startIndex + 1,1) == "/") 44 { 45 displayCharCount += tagLength;//タグの文字数だけ表示を進める 46 endTagStack[tagNestNum - 1] = null; 47 tagNestNum--; 48 continue; 49 } 50 displayCharCount += tagLength;//タグの文字数だけ表示を進める 51 string endTag = "</" + currentSentence.Substring(startIndex + 1, tagLength - 1); 52 if (endTag.Contains("=")) 53 endTag = endTag.Remove(endTag.IndexOf("="), endTag.Length - endTag.IndexOf("=") - 1); 54 endTagStack[tagNestNum] = endTag; 55 tagNestNum++; 56 } 57 uiText.text = currentSentence.Substring(0, displayCharCount); 58 //リッチテキスト用 59 if(endTagStack[0] != null) 60 { 61 for(int i = 1; i < tagNestNum + 1; i++) 62 { 63 uiText.text += endTagStack[tagNestNum - i]; 64 } 65 } 66 }

投稿2018/08/24 11:36

kagaribisou

総合スコア17

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問