public Vector2 getBounds() { Vector2 bounds = new Vector2(0, 0); Vector3 renderSize = new Vector3(1, 1, 1); Vector2 renderSize2 = new Vector2(renderSize.x, renderSize.y); Vector3 position = new Vector3(0, 0, 0); if (renderedText != Text) { bounds = Font.CalculateSize(Text, renderSize2); bounds.x *= Font.Size; bounds.y *= Font.Size; } return(bounds); }
public void OnGUI() { if (Event.current.type == EventType.Repaint) { if (Font != null && Text != null) { //Convert scale to pixels Vector2 scale = new Vector2(transform.lossyScale.x, transform.lossyScale.y); if (ScaleUnits == TextUnits.ScreenNormalized) { scale.x *= Screen.width; scale.y *= Screen.height; } if (KeepAspectRatio) { scale.y = scale.x; } //Calculate bounding box of rendered text Vector2 size = Font.CalculateSize(Text, scale); //Convert position to pixels Vector2 pos = new Vector2(transform.position.x, transform.position.y); if (PositionUnits == TextUnits.ScreenNormalized) { pos.x *= Screen.width; pos.y *= Screen.height; } //Default origin is top left if (Origin == TextOrigin.BottomLeft || Origin == TextOrigin.BottomRight) { pos.y = Screen.height - pos.y; } if (Origin == TextOrigin.TopRight || Origin == TextOrigin.BottomRight) { pos.x = Screen.width - pos.x; } if (Origin == TextOrigin.Center) { pos.x = pos.x + Screen.width / 2; pos.y = pos.y + Screen.height / 2; } Vector2 offset = new Vector2(0, 0); if (Anchor == TextAnchor.LowerCenter || Anchor == TextAnchor.LowerLeft || Anchor == TextAnchor.LowerRight) { offset.y = size.y; } if (Anchor == TextAnchor.MiddleCenter || Anchor == TextAnchor.MiddleLeft || Anchor == TextAnchor.MiddleRight) { offset.y = size.y / 2; } if (Anchor == TextAnchor.LowerRight || Anchor == TextAnchor.MiddleRight || Anchor == TextAnchor.UpperRight) { offset.x = size.x; } if (Anchor == TextAnchor.LowerCenter || Anchor == TextAnchor.MiddleCenter || Anchor == TextAnchor.UpperCenter) { offset.x = size.x / 2; } Font.Render(pos - offset, Text, scale); } } }
// Update is called once per frame void Update() { if (Font == null) { return; } Vector3 renderSize = new Vector3(1, 1, 1); Vector2 renderSize2 = new Vector2(renderSize.x, renderSize.y); Vector3 position = new Vector3(0, 0, 0); if (renderedText != Text) { //Calculate bounding box of rendered text Vector2 bounds = Font.CalculateSize(Text, renderSize2); Vector3 offset = new Vector3(0, 0); if (Anchor == TextAnchor.UpperCenter || Anchor == TextAnchor.UpperLeft || Anchor == TextAnchor.UpperRight) { offset.y = bounds.y; } if (Anchor == TextAnchor.MiddleCenter || Anchor == TextAnchor.MiddleLeft || Anchor == TextAnchor.MiddleRight) { offset.y = bounds.y / 2; } if (Anchor == TextAnchor.UpperRight || Anchor == TextAnchor.MiddleRight || Anchor == TextAnchor.LowerRight) { offset.x = bounds.x; } if (Anchor == TextAnchor.UpperCenter || Anchor == TextAnchor.MiddleCenter || Anchor == TextAnchor.LowerCenter) { offset.x = bounds.x / 2; } //Replace mesh Mesh mesh = GenerateLineMesh(position - offset, Text, renderSize); MeshFilter meshFilter = GetComponent <MeshFilter>(); if (meshFilter.sharedMesh != null) { if (Application.isPlaying) { Destroy(meshFilter.sharedMesh); } else { DestroyImmediate(meshFilter.sharedMesh); } } meshFilter.mesh = mesh; //Get materials for each texture page Material[] mats = new Material[mesh.subMeshCount]; for (int i = 0; i < mesh.subMeshCount; i++) { mats[i] = Font.GetPageMaterial(i); } renderer.materials = mats; renderedText = Text; } }