何かしらのコマンドを含んだメッセージを一文字ずつ表示するためのスクリプトです。
"あいうえお[ret][waitclick]こんにちわ[wink25,5000]"
というように角括弧[command]で囲まれた部分をコマンドとみなして何らかの処理を行います。
一文字ずつ取り出し、"["が出たらコマンドとみなして半角文字でコマンド名を、カンマで区切ることで複数の数字をパラメータとしてTextParamを返します
TextParam
mes、コマンドではない場合の文字列
isCommand、コマンドかどうか
Command、コマンド名
[]param、数値パラメータ
あくまでテキスト内に含まれたコマンドを取得するだけのスクリプトです。
コマンド処理はコマンド名とパラメータから個別に処理なくてはいけません。
例
[ret]改行挿入
[wait100]0.1秒待つ(数値はミリ秒)
[wink250,5000]0.25秒でウインク、次のウインクまで5秒(数値はミリ秒)
TextControl.cs
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
public class TextControl
{
private Encoding Enc = Encoding.GetEncoding("UTF-8");
private string messageText = "";
private Regex BrecketRange = new Regex(@"\[.*?\]");
private Regex BracketSelector = new Regex(@"\[|\]");
private Regex StringOnly = new Regex(@"[a-zA-Z]+");
private Regex NumOnly = new Regex(@"[0-9,]+");
private int _idx = 0;
public int idx
{
get
{
return _idx;
}
}
public int Length
{
get { return messageText.Length; }
}
public bool isOneByte(string strmes)
{
byte[] StrBytes = Enc.GetBytes(strmes);
return strmes.Length == StrBytes.Length;
}
public void SetMessage(string strmes)
{
messageText = strmes;
_idx = 0;
int i = 0;
while (i < Length)
{
string onestr = messageText[i].ToString();
if (isOneByte(onestr))
{
if (onestr == "[")
{
Match match = BrecketRange.Match(messageText, i);
if (match.Success)
{
string matched = BracketSelector.Replace(match.Value, "");
if (string.IsNullOrWhiteSpace(matched))
{
throw new System.Exception("Error in " + i + " command is null or whitespace");
}
GetCommand(matched, i);
i += match.Length;
continue;
}
else
{
throw new System.Exception("Error in " + i + " missing end \"]\" brancket");
}
}
if (onestr == "]")
{
throw new System.Exception("Error in " + i + " missing start \"[\" brancket");
}
}
i++;
}
}
public class TextParam
{
public string mes = "";
public bool isCommand = false;
public string Command = "";
public int[] param;
}
public TextParam GetCommand(string strcom, int index = 0)
{
TextParam resultText = new TextParam();
resultText.isCommand = true;
Match matchCommand = StringOnly.Match(strcom);
if (matchCommand.Success)
{
resultText.Command = matchCommand.Value;
if (!isOneByte(resultText.Command))
{
throw new System.Exception("Error in " + index + "command cannot analyze 2byte character(s)");
}
}
Match matchNums = NumOnly.Match(strcom);
if (matchNums.Success)
{
string[] numparams = matchNums.Value.Split(',');
List<int> nums = new List<int>();
for (int i = 0; i < numparams.Length; i++)
{
int result = 0;
if (int.TryParse(numparams[i], out result))
{
nums.Add(result);
}
}
resultText.param = nums.ToArray();
}
if (string.IsNullOrEmpty(resultText.Command) && resultText.param == null)
{
throw new System.Exception("Error in " + index + "command is nothing");
}
return resultText;
}
public TextParam value
{
get
{
if (isFinish) { return null; }
string onestr = messageText[_idx].ToString();
if (isOneByte(onestr))
{
if (onestr == "[")
{
Match match = BrecketRange.Match(messageText, _idx);
if (match.Success)
{
_idx += match.Length;
string matched = BracketSelector.Replace(match.Value, "");
return GetCommand(matched);
}
}
}
TextParam txt = new TextParam();
txt.mes = onestr;
_idx++;
return txt;
}
}
public string FullText
{
get
{
string result = "";
int i = 0;
while (i < Length)
{
string onestr = messageText[i].ToString();
if (isOneByte(onestr))
{
if (onestr == "[")
{
Match match = BrecketRange.Match(messageText, i);
if (match.Success)
{
i += match.Length;
continue;
}
}
}
result += onestr;
i++;
}
return result;
}
}
public bool isFinish
{
get
{
return _idx >= Length;
}
}
}