用于将Visual LTR转换为逻辑RTL的C#算法

时间:2015-08-30 14:38:45

标签: c# unity3d

我在Unity3d中使用C#脚本编写项目,使用希伯来语和英语编写的字符串。 Unity3d的问题是在编写UI文本时不支持RTL语言(如希伯来语)。

问题如下: 原文: מערכתהעטלףהינהמערכת,אשרמתבססתעלטכנולוגיתה-GPR (地面穿透雷达) - ראדארחודרקרקע。

团结一致: GPR-התיגולונכטלעתססבתמרשא,תכרעמהניהףלטעהתכרעמ .עקרקרדוחראדאר - (地面穿透雷达)

我试图破解文本打印到文本框一个月的顺序,我不能正确地得到它。 我编写了以下功能:

    public string CorrectText (string text)
{
    string result = "";
    string temp = "";
    string charListString = "";
    List<Char> charList = new List<char> ();
    char[] charArray = text.ToCharArray ();

    Array.Reverse (charArray);

    //go through each char in charArray
    for (int x = 0; x <= charArray.Length -1; x++) {
        //if the current char we're examing isn't a Space char -
        if (!char.IsWhiteSpace (charArray [x])) {
            //add it to charList
            charList.Add (charArray [x]);
        } else { //if the current char we're examing is a Space char -
            charListString = new string (charList.ToArray ());

            //if charListString doesn't contains English or Numeric chars -
            if (!Regex.IsMatch (charListString, "^[0-9a-zA-Z.,()-]*$")) {
                //go through each char in charList 
                for (int y = 0; y <= charList.Count - 1; y++) {
                    //add the current char to temp as is (not flipped)
                    temp += charList [y];
                }

                //add temp to result
                if (x < charArray.Length - 1)
                    result += temp + " ";
                if (x == charArray.Length - 1)
                    result += temp;

                //clear charList and temp
                charList.Clear ();
                temp = "";
            } else { //if temp contains English or Numeric chars -
                //go through each char in charList - This flipps the order of letters
                for (int y = charList.Count - 1; y >= 0; y--) {
                    //add the current char to temp
                    temp += charList [y];
                }

                //add temp to result
                if (x < charArray.Length - 1)
                    result += temp + " ";
                if (x == charArray.Length - 1)
                    result += temp;
                //clear charList and temp
                charList.Clear ();
                temp = "";
            }
        }

    }

    return result;
}

这几乎解决了问题,但文本是自下而上编写的,例如:

输入: מערכתהעטלףהינהמערכת,אשרמתבססתעלטכנולוגיתה-GPR (地面穿透雷达) - ראדארחודרקרקע。

输出: (地面穿透雷达) - ראדארחודרקרקע。 מערכתהעטלףהינהמערכת,אשרמתבססתעלטכנולוגיתה-GPR

有时括号会在句子中混淆,如下所示: )地面穿透(雷达 - ראדארחודרקרקע。

我在网上找到了一个将Visual Hebrew变成Logic Hebrew的工具,当我将翻转的文本复制到Unity时,它就像魔法一样工作!

但我无法在网上找到有关如何使用C#创建自己的脚本的任何有用信息。

1 个答案:

答案 0 :(得分:0)

我设法克服了这个......那种:

  1. 将字符串分配给文本元素。
  2. 强制使用Canvas.ForceUpdateCanvases()或其他方式更新画布。
  3. 获取文本组件的缓存文本生成器(不用于布局)。如果您没有强制更新或屈服并等待下一帧,那将对您说谎;所以不要跳过第2步。
  4. 使用缓存文本生成器的getLines获取文本换行的信息;它保存每行开始的索引。
  5. 获取这些值的最大差异,并且在包装之前每行可能有多少个字符。
  6. 插入&#39; \ n&#39; (新行)使用您在上一步之前>翻转字符时找到的行长度手动进入原始字符串!
  7. 对包裹的字符串执行反转过程。如果你以不同的顺序做这些事情,你最终会得到第一行,而不是最后一行。所以先包装,倒退第二。
  8. 将字符串设置为文本组件,它应该可以工作。通常。