Skip to content

Commit 8e566a0

Browse files
committed
Update for folded case
1 parent f500d29 commit 8e566a0

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

Assets/Hypertext/Examples/RegexHypertext.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,36 @@ public override void RemoveListeners()
6262
/// </summary>
6363
protected override void AddListeners()
6464
{
65+
var lineCount = CountChar(text, '\n') + 1;
66+
bool hasFolded = lineCount != cachedTextGenerator.lineCount;
67+
6568
foreach (var entry in entries)
6669
{
70+
foreach (Match match in Regex.Matches(text, entry.RegexPattern))
71+
{
6772
#if UNITY_2019_1_OR_NEWER
68-
var _text = text.Replace(" ", "").Replace("\n", "");
73+
if (hasFolded)
74+
{
75+
OnClick(match.Index, match.Value.Length, entry.Color, entry.Callback);
76+
}
77+
else
78+
{
79+
// 折り返していない場合のみ「空白」と「改行」が描画されないため、調整する
80+
var head = text.Substring(0, match.Index);
81+
var count = CountChar(head, ' ') + CountChar(head, '\n');
82+
OnClick(match.Index - count, match.Value.Length, entry.Color, entry.Callback);
83+
}
6984
#else
70-
var _text = text;
71-
#endif
72-
foreach (Match match in Regex.Matches(_text, entry.RegexPattern))
73-
{
7485
OnClick(match.Index, match.Value.Length, entry.Color, entry.Callback);
86+
#endif
7587
}
7688
}
7789
}
90+
91+
// 文字の出現回数をカウント
92+
public static int CountChar(string s, char c)
93+
{
94+
return s.Length - s.Replace(c.ToString(), "").Length;
95+
}
7896
}
7997
}

0 commit comments

Comments
 (0)