Skip to content

Commit e6a83f2

Browse files
authored
Merge pull request #27 from kwan3854/v0.6.0
Updates coordinate system in TMP rect computations to text object's local space
2 parents a5cf6ea + 899df31 commit e6a83f2

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ openupm add com.kwanjoong.textmeshpromax
9898
- `Window` -> `Package Manager` -> `+` button on top left -> `Add package from git URL`
9999
2. Copy and paste this url
100100
- ```https://github.com/kwan3854/TextMeshProMax.git```
101-
- If you need specific version, you can specify like this ```https://github.com/kwan3854/TextMeshProMax.git#v0.2.0```
101+
- If you need specific version, you can specify like this ```https://github.com/kwan3854/TextMeshProMax.git#v0.6.0```
102102

103103
> [!TIP]
104104
> You can see inline comments in the code editor by enabling this option in the Unity Editor: `Edit` -> `Preferences` -> `External Tools` -> `Generate .csproj files`
@@ -123,6 +123,12 @@ Retrieve the **Rect information** for specific strings rendered by a `TMP_Text`
123123
- **Returns**:
124124
- A list of `TextRectInfo` containing `Rects` and the `TargetString`.
125125

126+
> [!IMPORTANT]
127+
> **Coordinate System**
128+
> The returned `Rect` values are in the **local space of the text object's transform**.
129+
>
130+
> **Breaking Change Notice**: In versions prior to `0.6.0`, the `Rect` for `TextMeshProUGUI` was returned in the Canvas's local space. This has been changed to consistently use the text object's local space for all `TMP_Text` types. Please update your implementation accordingly.
131+
126132
##### Code Example
127133
```csharp
128134
using Runtime.Helper;
@@ -172,6 +178,13 @@ Attempt to retrieve the **Rect information** for specific strings rendered by a
172178
- **Returns**:
173179
- `bool`: `true` if successful.
174180

181+
> [!IMPORTANT]
182+
> **Coordinate System**
183+
> The returned `Rect` values are in the **local space of the text object's transform**.
184+
>
185+
> **Breaking Change Notice**: In versions prior to `0.6.0`, the `Rect` for `TextMeshProUGUI` was returned in the Canvas's local space. This has been changed to consistently use the text object's local space for all `TMP_Text` types. Please update your implementation accordingly.
186+
187+
175188
##### Code Example
176189
```csharp
177190
List<TextRectInfo> results;
@@ -201,6 +214,12 @@ Retrieve **Rect information** for Ruby strings, including body and Ruby text.
201214
- `Rects`: A list of `Rect` objects for the string or line.
202215
- `TargetString`: The concatenated plain text of the Ruby string.
203216

217+
> [!IMPORTANT]
218+
> **Coordinate System**
219+
> The returned `Rect` values are in the **local space of the text object's transform**.
220+
>
221+
> **Breaking Change Notice**: In versions prior to `0.6.0`, the `Rect` for `TextMeshProUGUI` was returned in the Canvas's local space. This has been changed to consistently use the text object's local space for all `TMP_Text` types. Please update your implementation accordingly.
222+
204223
##### Code Example
205224
```csharp
206225
using Runtime.Helper;
@@ -255,6 +274,12 @@ var rects = text.GetRubyStringRects(rubyString, TextFindMode.All);
255274
#### 2.1 TryGetRubyStringRects *(Requires RubyTextMeshPro)*
256275
Attempt to retrieve the **Rect information** for complex Ruby strings rendered by a `RubyTextMeshProUGUI` object. Returns `true` if successful, `false` otherwise.
257276

277+
> [!IMPORTANT]
278+
> **Coordinate System**
279+
> The returned `Rect` values are in the **local space of the text object's transform**.
280+
>
281+
> **Breaking Change Notice**: In versions prior to `0.6.0`, the `Rect` for `TextMeshProUGUI` was returned in the Canvas's local space. This has been changed to consistently use the text object's local space for all `TMP_Text` types. Please update your implementation accordingly.
282+
258283
#### 3. Multi-Line Support
259284
The library can calculate `Rect` values for text that spans multiple lines. Whether the line breaks are due to manual newlines (`\n`) or automatic text wrapping applied by TextMesh Pro, the library handles them seamlessly.
260285

Runtime/Helper/TextMeshProExtensions.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ public static bool TryGetStringRects(this TextMeshProUGUI text, string targetStr
8282

8383
/// <summary>
8484
/// Retrieves the Rect information for a specific string within a TMP_Text object.
85-
/// For TextMeshPro (3D), the Rect values are in the text object's local space.
86-
/// For TextMeshProUGUI (UI), the Rect values are in the canvas's local space.
85+
/// The returned Rect values are in the local space of the text object's transform.
8786
/// </summary>
8887
/// <param name="text">The target TMP_Text object.</param>
8988
/// <param name="targetString">The string to find within the text.</param>
@@ -407,7 +406,7 @@ private static bool TryGetStringRectsInternal(
407406
if (charIndexes.Count == 0) continue;
408407

409408
// Split into lines and calculate Rect for each line
410-
var lineRects = CalculateLineBoundingRects(charIndexes, textInfo, textBase.Transform);
409+
var lineRects = CalculateLineBoundingRects(charIndexes, textInfo);
411410

412411
if (lineRects.Count > 0)
413412
{
@@ -427,8 +426,7 @@ private static bool TryGetStringRectsInternal(
427426
/// </summary>
428427
private static List<Rect> CalculateLineBoundingRects(
429428
List<int> charIndexes,
430-
TMP_TextInfo textInfo,
431-
Transform transform)
429+
TMP_TextInfo textInfo)
432430
{
433431
var rects = new List<Rect>();
434432
var lineGroups = charIndexes
@@ -438,7 +436,7 @@ private static List<Rect> CalculateLineBoundingRects(
438436
foreach (var lineGroup in lineGroups)
439437
{
440438
var lineCharIndexes = lineGroup.ToList();
441-
var rect = CalculateBoundingRect(lineCharIndexes, textInfo, transform);
439+
var rect = CalculateBoundingRect(lineCharIndexes, textInfo);
442440
if (rect.HasValue)
443441
rects.Add(rect.Value);
444442
}
@@ -451,21 +449,20 @@ private static List<Rect> CalculateLineBoundingRects(
451449
/// </summary>
452450
private static Rect? CalculateBoundingRect(
453451
List<int> charIndexes,
454-
TMP_TextInfo textInfo,
455-
Transform transform)
452+
TMP_TextInfo textInfo)
456453
{
457454
if (charIndexes == null || charIndexes.Count == 0)
458455
return null;
459456

460457
var firstCharInfo = textInfo.characterInfo[charIndexes[0]];
461-
var bottomLeft = transform.TransformPoint(firstCharInfo.bottomLeft);
462-
var topRight = transform.TransformPoint(firstCharInfo.topRight);
458+
var bottomLeft = (Vector2)firstCharInfo.bottomLeft;
459+
var topRight = (Vector2)firstCharInfo.topRight;
463460

464461
foreach (var index in charIndexes.Skip(1))
465462
{
466463
var charInfo = textInfo.characterInfo[index];
467-
var charBottomLeft = transform.TransformPoint(charInfo.bottomLeft);
468-
var charTopRight = transform.TransformPoint(charInfo.topRight);
464+
var charBottomLeft = (Vector2)charInfo.bottomLeft;
465+
var charTopRight = (Vector2)charInfo.topRight;
469466

470467
bottomLeft = Vector2.Min(bottomLeft, charBottomLeft);
471468
topRight = Vector2.Max(topRight, charTopRight);

Runtime/Helper/TextMeshProExtensions.cs.meta

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.kwanjoong.textmeshpromax",
3-
"version": "0.5.2",
3+
"version": "0.6.0",
44
"displayName": "Text Mesh Pro Max",
55
"description": "The ultimate Text Mesh Pro helper package.",
66
"unity": "2022.3",

0 commit comments

Comments
 (0)