Advertisement
Guest User

AdaptiveFont.cs

a guest
Apr 22nd, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine.UI;
  4. using UnityEngine;
  5.  
  6. public class AdaptiveFont : MonoBehaviour
  7. {
  8.     Text txt;
  9.     public bool continualUpdate = true;
  10.  
  11.     public int fontSizeAtDefaultResolution = 24;
  12.     public static float defaultResolution = 2229f;
  13.  
  14.  
  15.     // Use this for initialization
  16.     void Start ()
  17.     {
  18.         //print(Screen.height + Screen.width);
  19.  
  20.         txt = GetComponent<Text>();
  21.  
  22.         if(continualUpdate)
  23.         {
  24.             InvokeRepeating("Adjust", 0f, Random.Range(0.5f, 2f));
  25.         }
  26.         else
  27.         {
  28.             Adjust();
  29.             enabled = false;
  30.         }
  31.  
  32.     }
  33.  
  34.     void Adjust()
  35.     {
  36.         if (!enabled || !gameObject.activeInHierarchy)
  37.         {
  38.             return;
  39.         }
  40.  
  41.         float totalCurrentRes = Screen.height + Screen.width;
  42.         float perc = totalCurrentRes / defaultResolution;
  43.         int fontsize = Mathf.RoundToInt((float)fontSizeAtDefaultResolution * perc);
  44.  
  45.         txt.fontSize = fontsize;
  46.  
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement