Search Unity

Text Generator verts/positioning images on letter position

Discussion in 'UGUI & TextMesh Pro' started by CaptainKirby, Apr 20, 2015.

Thread Status:
Not open for further replies.
  1. CaptainKirby

    CaptainKirby

    Joined:
    Dec 12, 2011
    Posts:
    36
    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:
    Code (csharp):
    1.  
    2. Text text = GetComponent<Text>();
    3.         TextGenerator textGen = text.cachedTextGenerator;
    4.         for (int i = 19; i < 28; ++i)
    5.         {
    6.             Vector2 locUpperLeft = new Vector2 (textGen.verts [i * 4].position.x, textGen.verts [i * 4].position.y);
    7.             Vector2 locBottomRight = new Vector2 (textGen.verts [i * 4 + 2].position.x, textGen.verts [i * 4 + 2].position.y);
    8.  
    9.             Vector3 mid = (locUpperLeft + locBottomRight) / 2.0f;
    10.             Vector3 size = locBottomRight - locUpperLeft;
    11.  
    12.             Image obj = Instantiate(redactSprite, Vector3.zero, Quaternion.identity) as Image;
    13.             obj.transform.SetParent(this.transform);
    14.             obj.transform.position = transform.TransformPoint(mid);
    15.         }
    16.  
    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!
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
  3. CaptainKirby

    CaptainKirby

    Joined:
    Dec 12, 2011
    Posts:
    36
    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...
    Code (csharp):
    1.  
    2. Text text = GetComponent<Text>();
    3.         TextGenerator textGen = text.cachedTextGenerator;
    4.  
    5.         for (int i = 19; i < 28; ++i)
    6.         {
    7.             Vector2 locUpperLeft = new Vector2 (textGen.verts [i * 4].position.x, textGen.verts [i * 4].position.y);
    8.             Vector2 locBottomRight = new Vector2 (textGen.verts [i * 4 + 2].position.x, textGen.verts [i * 4 + 2].position.y);
    9.        
    10.             Vector3 mid = (locUpperLeft + locBottomRight) / 2.0f;
    11.             Vector3 size = locBottomRight - locUpperLeft;
    12.        
    13.             Image obj = Instantiate(redactSprite, Vector3.zero, Quaternion.identity) as Image;
    14.             obj.transform.SetParent(this.transform);
    15.             obj.GetComponent<RectTransform>().anchoredPosition = mid;
    16.             obj.GetComponent<RectTransform>().anchorMax = Vector2.one;
    17.             obj.GetComponent<RectTransform>().anchorMin = Vector2.zero;
    18.             obj.GetComponent<RectTransform>().offsetMax = new Vector2(obj.GetComponent<RectTransform>().offsetMax.x, locUpperLeft.y);
    19.             obj.GetComponent<RectTransform>().offsetMin = new Vector2(obj.GetComponent<RectTransform>().offsetMin.x, locBottomRight.y);
    20.  
    21.         }
    22.  
    To visualize what happens:
    If the game screen is "too small" the black images are put in too low(they should be on "authentic").
    cts_redacthelp01.png
    And here the game view is larger and the images are put correctly onto the text. cts_redacthelp02.png

    Any help would be appreciated!
    Thanks
     
  4. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    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.
     
  5. Democide

    Democide

    Joined:
    Jan 29, 2013
    Posts:
    315
    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.
     
  6. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    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.
      Code (CSharp):
      1. var MyRect = obj.GetComponent<RectTransform>();
      2. MyRect .anchoredPosition = mid;
      3. ...
     
  7. CaptainKirby

    CaptainKirby

    Joined:
    Dec 12, 2011
    Posts:
    36
    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!

    Thanks for all the help though :)
     
  8. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Good find, glad you got there in the end :D
     
  9. JossesMan

    JossesMan

    Joined:
    Aug 22, 2019
    Posts:
    2
    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.
     
    Last edited: Aug 23, 2019
  10. HoangVuPhuong

    HoangVuPhuong

    Joined:
    Jun 30, 2016
    Posts:
    1
    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.
     
  11. Justin128

    Justin128

    Joined:
    Jun 18, 2020
    Posts:
    1
    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.
     
  12. bkleak2020

    bkleak2020

    Joined:
    Jul 27, 2020
    Posts:
    1
    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!
     
  13. Loretta_Smith

    Loretta_Smith

    Joined:
    Aug 13, 2020
    Posts:
    1
    I think setting the anchors and offsets with respect to the text would be a better approach, Weird Text generator.
     
  14. textgenerator1

    textgenerator1

    Joined:
    Dec 16, 2020
    Posts:
    1
    i also use this solution and one of the best solution fount that is weird font
     
  15. freetextandfonts

    freetextandfonts

    Joined:
    Apr 16, 2021
    Posts:
    1
    One of the best and easy solution for this is fancy text. So, go ahead and use it for free. Thanks
     
  16. brunos2021

    brunos2021

    Joined:
    Jun 30, 2021
    Posts:
    1
    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!
     
  17. robmiller12

    robmiller12

    Joined:
    Jun 11, 2021
    Posts:
    1
    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.
     
  18. marvinjames

    marvinjames

    Joined:
    Dec 30, 2021
    Posts:
    1
    Best Solution ever.
    I also used this solution, Great Job for giving this solution.. :)
     
  19. TanChang2022

    TanChang2022

    Joined:
    Jan 1, 2022
    Posts:
    1
    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.
     
Thread Status:
Not open for further replies.