- Home /
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: But, when I try to rotate my text and use alignment, my rectangle generated over the Text gets a weird position.
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?
Why go through this trouble when you can just position an empty image component behind the text?
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.
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.