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.
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”).
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.
Hey, I found the issue!
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!
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.
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!
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.