Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MaciekSz · Aug 20, 2018 at 12:26 PM · uimeshtextvertices

Positioning of Text vertices when text is alligned

Hello

What I'm trying to achieve is to put a rectangle over the Text inside the UI Button. For this i use Text.cachedTextGenerator.verts to obtain Text vertices. Until now everything works fine. The generated rectangle over the text liiks like this, as expected: alt text But, when I try to rotate my text and use alignment, my rectangle generated over the Text gets a weird position.

alt text

The code used to generate such rectangle is below:

   var textComponent = (Text)button.GetComponentInChildren(textComponentType);
             var textRect = textComponent.GetComponent<RectTransform>();
             var minXPos = 0.0f;
             var minYPos = 0.0f;
             var maxXPos = 0.0f;
             var maxYPos = 0.0f;
             var rectWorldPosition = textRect.position;
             var rectWorldScale = textRect.localScale;
 
             foreach (var vert in textComponent.cachedTextGenerator.verts)
             {
                 var position = vert.position;
 
                 if (position.x < minXPos)
                 {
                     minXPos = position.x;
                 }
                 if (position.y < minYPos)
                 {
                     minYPos = position.y;
                 }
                 if (position.x > maxXPos)
                 {
                     maxXPos = position.x;
                 }
                 if (position.y > maxYPos)
                 {
                     maxYPos = position.y;
                 }
 
                 //var cub = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 //cub.transform.position = position;
                 //cub.tag = "vert";
             }
             var LTCorner = new Vector3(minXPos, maxYPos, 0);
             var RTCorner = new Vector3(maxXPos, maxYPos, 0);
             var LBCorner = new Vector3(minXPos, minYPos, 0);
             var RBCorner = new Vector3(maxXPos, minYPos, 0);
             var width = Mathf.Abs(maxXPos - minXPos);
             var height = Mathf.Abs(maxYPos - minYPos);
             Debug.Log(textRect.pivot);
             var center = new Vector3(minXPos + width * textRect.pivot.x, minYPos + height * textRect.pivot.y, 0);
             GameObject.DestroyImmediate(GameObject.Find("Center-pivot"));
             //var centerObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
             //centerObject.name = "Center-pivot";
             //centerObject.transform.position = center;
 
             
             var newRotation = new Quaternion
             {
                 eulerAngles = textRect.transform.rotation.eulerAngles //the degrees the vertices are to be rotated, for example (0,90,0)
                 
             };
 
             var plane = new GameObject
             {
                 name = "Plane"
             };
 
             plane.AddComponent<MeshRenderer>();
             Mesh mesh = new Mesh
             {
                 name = "plane Mesh",
                 vertices = new Vector3[]
                 {
                     newRotation * (LTCorner - center) + center,
                     newRotation * (RTCorner - center) + center,
                     newRotation * (LBCorner - center) + center,
                     newRotation * (RBCorner - center) + center
                 },
                 triangles = new int[] { 0, 1, 2, 2, 1, 3 },
                 normals = new Vector3[] { Vector3.up, Vector3.up, Vector3.up, Vector3.up },
                 uv = new Vector2[] { new Vector2(1, 1), new Vector2(1, 0), new Vector2(0, 1), new Vector2(0, 0) },
             };
 
             plane.AddComponent<MeshFilter>().mesh = mesh;
             plane.transform.SetParent(textRect);
             plane.transform.position = rectWorldPosition;
             plane.transform.localScale = rectWorldScale;









Can you explain to me why the plane is positioned incorrectly?

unity-201721p4-personal-64bit-mainunity-unity3d-ba.jpg (33.6 kB)
unity-201721p4-personal-64bit-mainunity-unity3d-ba.jpg (104.3 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vicarian · Aug 20, 2018 at 12:44 PM 0
Share

Why go through this trouble when you can just position an empty image component behind the text?

avatar image MaciekSz Vicarian · Aug 20, 2018 at 01:01 PM 0
Share

I do it like this because I need exact position and dimensions of the rectangle that covers the text. This has to be done like this because I want it to depend on how Unity renders the Text not how user places the empty image over the text.

avatar image Vicarian MaciekSz · Aug 20, 2018 at 01:13 PM 0
Share

The user? So you're developing a store asset? Anyway, you could simply prefab the text and the image. You can make sure that the positioning is set up correctly in the prefab, in that the image dictates the space the text can occupy. $$anonymous$$ake use of a ContentSizeFitter component to size the text element.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

158 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing vertex positions of a uGUI Text Mesh 0 Answers

UnityEngine.UI.Text characters mesh 0 Answers

ui text and quad mesh render priority ? 0 Answers

How Text Verices are created for UI.Text when Vertical overflow is set to Truncate 0 Answers

Text Color Null Ref 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges