Question by
Szustarol · Jan 01, 2017 at 05:25 PM ·
c#transformscalebasic programming
Scaling a text to fill 50% of screen width and 50% of height, no matter whats inside C#
Hi!
As the tittle says, I just want to scale a text.
Here Is code i use:
void Start () { rend = GetComponent (); this.text = GetComponent (); screenh = Screen.height; screenw = Screen.width; float tsizx = Tools.BoundsToScreenRect (this.rend.bounds).width; float tsizy = Tools.BoundsToScreenRect (this.rend.bounds).height; float actpx = screenw / tsizx; float actpy = screenh / tsizy; this.text.transform.localScale = new Vector3 (0.5f/actpx, 0.5f/actpy , 1); UnityEngine.Debug.Log ("Tw:" + tsizx); }Also, this is the BoundsToScreenRect method i got somewhere in the internet:
public static Rect BoundsToScreenRect(Bounds bounds) { // Get mesh origin and farthest extent (this works best with simple convex meshes) Vector3 origin = Camera.main.WorldToScreenPoint(new Vector3(bounds.min.x, bounds.max.y, 0f)); Vector3 extent = Camera.main.WorldToScreenPoint(new Vector3(bounds.max.x, bounds.min.y, 0f)); // Create rect in screen space and return - does not account for camera perspective return new Rect(origin.x, Screen.height - origin.y, extent.x - origin.x, origin.y - extent.y); }I need the scaling because I am trying to make an app for android and it should look nice on every screen. Sadly the code above scales the text not the way I would like it to scale. Its smaller and looks quite bad. Thanks in advance for any help.
Comment
Are you using Unity UI Text? If so, you need no code for this, there's everything in the builtin components to make a responsive UI.