Text Generator verts/positioning images on letter position

Hey, So i am trying to place UI Images on top of certain letters in a text.
So far I have managed to make it work, but ONLY if the game window is the right size?! if the game window is not the arbitrarily right size, the letters are positioned wrongly.
This is my code:

Text text = GetComponent<Text>();
        TextGenerator textGen = text.cachedTextGenerator;
        for (int i = 19; i < 28; ++i)
        {
            Vector2 locUpperLeft = new Vector2 (textGen.verts [i * 4].position.x, textGen.verts [i * 4].position.y);
            Vector2 locBottomRight = new Vector2 (textGen.verts [i * 4 + 2].position.x, textGen.verts [i * 4 + 2].position.y);
  
            Vector3 mid = (locUpperLeft + locBottomRight) / 2.0f;
            Vector3 size = locBottomRight - locUpperLeft;
  
            Image obj = Instantiate(redactSprite, Vector3.zero, Quaternion.identity) as Image;
            obj.transform.SetParent(this.transform);
            obj.transform.position = transform.TransformPoint(mid);
        }

I am wondering if there is a way to have the images always instantiate on top of the letter, and not having to make the game window a to be a specific size?
Best regards!

I would suggest you also set the anchors and offsets accordingly when you are placing it over text. Make sure you are also using UI components to do the overlay.

Have a look at the editor script for the UI item which clearly shows positioning and layout of child components by setting these:
https://bitbucket.org/Unity-Technologies/ui/src/fd5d3578da8c33883b7c56dd2b2f4d4bbc87095b/UnityEditor.UI/UI/MenuOptions.cs?at=4.6

Hope this helps.

Hey, thanks. I have tried to set the anchor and offset, but it doesnt seem to change the position issues. Maybe I am just overloking something…
This is my current code…

Text text = GetComponent<Text>();
        TextGenerator textGen = text.cachedTextGenerator;

        for (int i = 19; i < 28; ++i)
        {
            Vector2 locUpperLeft = new Vector2 (textGen.verts [i * 4].position.x, textGen.verts [i * 4].position.y);
            Vector2 locBottomRight = new Vector2 (textGen.verts [i * 4 + 2].position.x, textGen.verts [i * 4 + 2].position.y);
       
            Vector3 mid = (locUpperLeft + locBottomRight) / 2.0f;
            Vector3 size = locBottomRight - locUpperLeft;
       
            Image obj = Instantiate(redactSprite, Vector3.zero, Quaternion.identity) as Image;
            obj.transform.SetParent(this.transform);
            obj.GetComponent<RectTransform>().anchoredPosition = mid;
            obj.GetComponent<RectTransform>().anchorMax = Vector2.one;
            obj.GetComponent<RectTransform>().anchorMin = Vector2.zero;
            obj.GetComponent<RectTransform>().offsetMax = new Vector2(obj.GetComponent<RectTransform>().offsetMax.x, locUpperLeft.y);
            obj.GetComponent<RectTransform>().offsetMin = new Vector2(obj.GetComponent<RectTransform>().offsetMin.x, locBottomRight.y);

        }

To visualize what happens:
If the game screen is “too small” the black images are put in too low(they should be on “authentic”).


And here the game view is larger and the images are put correctly onto the text.

Any help would be appreciated!
Thanks

Rather than just position, you might want to use LocalPosition. Not sure it it will help but worth a try.

Your mark does appear to be getting offset somehow there, hmm.

The whole polygons seem to be offset compared to where they should be. On the lower one the mark is over the vertices, as expected. On the upper one, the mark is still over the vertices, but the vertices are not over the letters. that’s weird.

Two questions:

  • Is the redactedSprite a GO with an Image component on it?
  • Use the GetComponent to get a var with the RectTransform, then update the var to avoid repeated GetComponent calls. e.g.
var MyRect = obj.GetComponent<RectTransform>();
MyRect .anchoredPosition = mid;
...

Hey, I found the issue! :slight_smile:
It was with the canvas settings being set to scale with screen, so finding the ratio of the current with vs the canvas component setting and multiplying/dividing the position with that gave the correct position!

Thanks for all the help though :slight_smile:

Good find, glad you got there in the end :smile:

The Vert.x kernel is small and lightweight. Just use the parts you need. It is also fully integrated with your existing applications - it does not force you to structure applications precisely to use Vert.x.

You can use the kernel of any other language that Vert.x supports. But here’s the big part - it does not force you to use the Java API directly with JavaScript or Ruby - otherwise there are different practices and phrases in different languages, and it would be weird to force Java phrases with Ruby Developers (for example) tiny text generator at fontvilla. Instead, we automatically generate the idiomatic equivalent of the Java core APIs for each language.

Thanks.
I’ve got a similar problem while trying to resize the chat bubble with some constraints (padding, max-width, custom line-height…).

My scenario:
Everything works fine in the test scene where CanvasScaler mode is set to Constant Pixel Size.
But it fails to work in the actual scene. I compared the settings, one by one to find out the reason which is the Canvas Scaler (The actual scene has CanvasScaler mode is set to Scale with Screen Size).

My fix:
Cache the Canvas.scaleFactor in a static variable.
Divide required text area size (from TextGenerator.GetPreferred*) by that factor. The end.

The whole polygons seem to be offset compared to where they should be. On the lower one the mark is over the vertices, as expected. On the upper one, the mark is still over the vertices, but the vertices are not over the letters. that’s weird.

Oh Good, I’m so glad because e

With this your code lines. I see you set the true value in lines number 20. Please check again!

I think setting the anchors and offsets with respect to the text would be a better approach, Weird Text generator.

i also use this solution and one of the best solution fount that is weird font

One of the best and easy solution for this is fancy text. So, go ahead and use it for free. Thanks

Congratulation CaptainKirby!
This is also a precious lesson for me as well as others. Besides, you can give me your other problems for me about your job. You can give me the best way to use Joker font for a new WordPress site? ThankS!
Good luck!

The netbook is an enabler and it is applications like this that make it possible that you can use high-quality PC games on your phone as well.

Best Solution ever.
I also used this solution, Great Job for giving this solution… :slight_smile:

Each problem usually has many directions to solve, but among them, there is only one most effective solution. Thanks everyone. I did with this beauty and the beast font.