Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ openupm add com.kwanjoong.textmeshpromax
- `Window` -> `Package Manager` -> `+` button on top left -> `Add package from git URL`
2. Copy and paste this url
- ```https://github.com/kwan3854/TextMeshProMax.git```
- If you need specific version, you can specify like this ```https://github.com/kwan3854/TextMeshProMax.git#v0.2.0```
- If you need specific version, you can specify like this ```https://github.com/kwan3854/TextMeshProMax.git#v0.6.0```

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

> [!IMPORTANT]
> **Coordinate System**
> The returned `Rect` values are in the **local space of the text object's transform**.
>
> **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.

##### Code Example
```csharp
using Runtime.Helper;
Expand Down Expand Up @@ -172,6 +178,13 @@ Attempt to retrieve the **Rect information** for specific strings rendered by a
- **Returns**:
- `bool`: `true` if successful.

> [!IMPORTANT]
> **Coordinate System**
> The returned `Rect` values are in the **local space of the text object's transform**.
>
> **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.


##### Code Example
```csharp
List<TextRectInfo> results;
Expand Down Expand Up @@ -201,6 +214,12 @@ Retrieve **Rect information** for Ruby strings, including body and Ruby text.
- `Rects`: A list of `Rect` objects for the string or line.
- `TargetString`: The concatenated plain text of the Ruby string.

> [!IMPORTANT]
> **Coordinate System**
> The returned `Rect` values are in the **local space of the text object's transform**.
>
> **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.

##### Code Example
```csharp
using Runtime.Helper;
Expand Down Expand Up @@ -255,6 +274,12 @@ var rects = text.GetRubyStringRects(rubyString, TextFindMode.All);
#### 2.1 TryGetRubyStringRects *(Requires RubyTextMeshPro)*
Attempt to retrieve the **Rect information** for complex Ruby strings rendered by a `RubyTextMeshProUGUI` object. Returns `true` if successful, `false` otherwise.

> [!IMPORTANT]
> **Coordinate System**
> The returned `Rect` values are in the **local space of the text object's transform**.
>
> **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.

#### 3. Multi-Line Support
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.

Expand Down
21 changes: 9 additions & 12 deletions Runtime/Helper/TextMeshProExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ public static bool TryGetStringRects(this TextMeshProUGUI text, string targetStr

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

// Split into lines and calculate Rect for each line
var lineRects = CalculateLineBoundingRects(charIndexes, textInfo, textBase.Transform);
var lineRects = CalculateLineBoundingRects(charIndexes, textInfo);

if (lineRects.Count > 0)
{
Expand All @@ -427,8 +426,7 @@ private static bool TryGetStringRectsInternal(
/// </summary>
private static List<Rect> CalculateLineBoundingRects(
List<int> charIndexes,
TMP_TextInfo textInfo,
Transform transform)
TMP_TextInfo textInfo)
{
var rects = new List<Rect>();
var lineGroups = charIndexes
Expand All @@ -438,7 +436,7 @@ private static List<Rect> CalculateLineBoundingRects(
foreach (var lineGroup in lineGroups)
{
var lineCharIndexes = lineGroup.ToList();
var rect = CalculateBoundingRect(lineCharIndexes, textInfo, transform);
var rect = CalculateBoundingRect(lineCharIndexes, textInfo);
if (rect.HasValue)
rects.Add(rect.Value);
}
Expand All @@ -451,21 +449,20 @@ private static List<Rect> CalculateLineBoundingRects(
/// </summary>
private static Rect? CalculateBoundingRect(
List<int> charIndexes,
TMP_TextInfo textInfo,
Transform transform)
TMP_TextInfo textInfo)
{
if (charIndexes == null || charIndexes.Count == 0)
return null;

var firstCharInfo = textInfo.characterInfo[charIndexes[0]];
var bottomLeft = transform.TransformPoint(firstCharInfo.bottomLeft);
var topRight = transform.TransformPoint(firstCharInfo.topRight);
var bottomLeft = (Vector2)firstCharInfo.bottomLeft;
var topRight = (Vector2)firstCharInfo.topRight;

foreach (var index in charIndexes.Skip(1))
{
var charInfo = textInfo.characterInfo[index];
var charBottomLeft = transform.TransformPoint(charInfo.bottomLeft);
var charTopRight = transform.TransformPoint(charInfo.topRight);
var charBottomLeft = (Vector2)charInfo.bottomLeft;
var charTopRight = (Vector2)charInfo.topRight;

bottomLeft = Vector2.Min(bottomLeft, charBottomLeft);
topRight = Vector2.Max(topRight, charTopRight);
Expand Down
11 changes: 10 additions & 1 deletion Runtime/Helper/TextMeshProExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.kwanjoong.textmeshpromax",
"version": "0.5.2",
"version": "0.6.0",
"displayName": "Text Mesh Pro Max",
"description": "The ultimate Text Mesh Pro helper package.",
"unity": "2022.3",
Expand Down