如果你想让对话系统检测到人物名字中有'b'出现时,隐藏直到下一个'b'出现的中间所有字,你可以在GetCharacterName方法中进行处理。以下是修改后的代码示例:

public string GetCharacterName(Character character)
{
    string characterName = CharacterNameDic[character];
    
    int startIndex = characterName.IndexOf('b');
    int endIndex = characterName.IndexOf('b', startIndex + 1);
    
    if (startIndex >= 0 && endIndex > startIndex)
    {
        characterName = characterName.Substring(0, startIndex) + characterName.Substring(endIndex + 1);
    }
    
    return characterName;
}

在上述代码中,我们使用IndexOf方法找到人物名字中第一个'b'的索引,并使用Substring方法将其之前的部分和之后的部分拼接起来,从而实现隐藏中间所有字的效果。注意,该方法只会隐藏第一个'b'和第二个'b'之间的字,如果有多个'b'出现,你需要根据具体需求进行修改。

然后,在UpdateChat方法中调用GetCharacterName方法:

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);
}

这样,当人物名字中有'b'出现时,显示的名字将被隐藏中间的所有字。

C# 对话系统中隐藏人物名字包含'b'的中间部分

原文地址: https://www.cveoy.top/t/topic/ph1z 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录