Unity 对话系统隐藏人物名字中 b 之间的字符
Unity 对话系统隐藏人物名字中 b 之间的字符
在 Unity 对话系统中,为了实现特殊角色名称的隐藏效果,比如人物名称为'Lilybab',实际显示出来是'Lily',可以检测人物名字中是否出现了'b',并隐藏'b'之间的所有字。
以下是实现方法:
- 修改 ChatItem 类: 添加一个 bool 类型的属性 'isHidden',用于标记该对话是否需要隐藏。
[Serializable]
public class ChatItem
{
public Character CharacterName;
public string Dialog;
public ChatPosition Position;
public bool isHidden;
}
- 修改 ShowDialogRow 方法: 在该方法中,根据人物名字检查是否出现了'b',并设置 'isHidden' 属性。
public UIPanel ShowDialogRow(TextAsset _textAsset, string eventCode)
{
dialogRows = _textAsset.text.Split('
');
List<ChatItem> list = new List<ChatItem>();
for (int i = 0; i < dialogRows.Length; i++)
{
string[] cells = dialogRows[i].Split(',');
if (cells[0] == "#" && int.Parse(cells[1]) == dialogIndex)
{
ChatPosition pos;
if (cells[3] == "左")
{
pos = ChatPosition.Left;
}
else
{
pos = ChatPosition.Right;
}
string dialog = cells[4];
bool isHidden = false;
if (dialog.Contains("b"))
{
int startIndex = dialog.IndexOf("b");
int endIndex = dialog.IndexOf("b", startIndex + 1);
if (startIndex >= 0 && endIndex >= 0)
{
isHidden = true;
}
}
list.Add(new ChatItem()
{
CharacterName = ChatSystem.NameToCharacterDic[cells[2]],
Dialog = dialog,
Position = pos,
isHidden = isHidden
});
dialogIndex = int.Parse(cells[5]);
}
else if (cells[0] == "END" && int.Parse(cells[1]) == dialogIndex)
{
dialogRows = new string[0];
dialogIndex = 0;
}
}
return UIKit.OpenPanel<ChatPanel>(new ChatPanelData
{
chatList = list,
EventCode = eventCode
});
}
- 修改 UpdateChat 方法: 在该方法中,根据对话的 'isHidden' 属性来设置是否完全显示对话。
protected void UpdateChat()
{
if (CurrentIndex < chatList.Count)
{
dialogdata.text = "";
isShowingComplete = false;
if (mCoroutine != null)
{
StopCoroutine(mCoroutine);
mCoroutine = null;
}
mCoroutine = StartCoroutine(TypeText(chatList[CurrentIndex].Dialog, chatList[CurrentIndex].isHidden));
if (chatList[CurrentIndex].Position == ChatPosition.Left)
{
ImageLeft.sprite = ChatSystem.Instance.imageDic[chatList[CurrentIndex].CharacterName];
LeftName.text = ChatSystem.Instance.GetCharacterName(chatList[CurrentIndex].CharacterName);
ImageLeft.gameObject.SetActive(true);
ImageRight.gameObject.SetActive(false);
LeftName.gameObject.SetActive(true);
RightName.gameObject.SetActive(false);
}
else
{
ImageRight.sprite = ChatSystem.Instance.imageDic[chatList[CurrentIndex].CharacterName];
RightName.text = ChatSystem.Instance.GetCharacterName(chatList[CurrentIndex].CharacterName);
ImageLeft.gameObject.SetActive(false);
ImageRight.gameObject.SetActive(true);
LeftName.gameObject.SetActive(false);
RightName.gameObject.SetActive(true);
}
CurrentIndex++;
}
else
{
CloseSelf();
}
}
- 修改 TypeText 方法: 在该方法中,根据对话的 'isHidden' 属性来设置是否显示每个字。
private IEnumerator TypeText(string text, bool isHidden)
{
int length = text.Length;
for (int i = 0; i < length; i++)
{
if (isHidden && text[i] != 'b')
{
dialogdata.text += "*"; // 隐藏字可以显示为*或其他符号
}
else
{
dialogdata.text += text[i];
}
yield return new WaitForSeconds(0.07f);
}
isShowingComplete = true;
}
通过以上修改,当人物名字中出现了 'b' 时,对话系统将隐藏 'b' 之间的字符,实现特殊角色名称的隐藏效果。
注意:
- 以上代码仅供参考,具体实现方式可能需要根据实际情况进行调整。
- 你可以根据自己的需求,将隐藏字符的显示方式改为其他符号或空白字符。
- 建议在代码中添加必要的注释,方便后期维护和修改。
希望这篇文章能够帮助你实现 Unity 对话系统中特殊角色名称的隐藏效果。
原文地址: https://www.cveoy.top/t/topic/ph0D 著作权归作者所有。请勿转载和采集!