Rescale all in-text UI at runtime via a settings menu

13 Views Asked by At

I have been trying to achieve this via multiple ways and am still struggling. I basically want to find all UI text elements across my game (active and inactive, across multiple scenes) and rescale them by a factor of x% for accessibly reasons. This would be done via a slider or a button in the settings menu. Can you please help, with this script or a different suggestion altogether. This is my script. I am currently getting errors I have no idea how to fix (see screenshot Error Screenshot).


using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Gamemanager : MonoBehaviour
{
   [ContextMenuItem("Fetch", nameof(FetchAllTexts))]

    public Text[] uiTexts;
    public float fontSizeModifier = 1f;

    void Start()
    {
        texts = GetComponent<Text>();
    }

    private void FetchAllTexts()
    {
        var tmp = new List<Text>();
        for (var i = 0; i < SceneManager.sceneCount; i++)
        {
            foreach (var root in SceneManager.GetSceneAt(i).GetRootGameObjects())
            {
                tmp.AddRange(root.GetComponentsInChildren<Text>(true));
            }
        }
        Text[] texts = tmp.ToArray();

        uiTexts = texts;
    }

    private void IncreaseUiTexts()
    {
        if (uiTexts.Length > 0)
        {
            foreach (Text ui in uiTexts)
            {
                texts.fontSize = (int)(texts.fontSize * fontSizeModifier);
            }
        }
    }

}
0

There are 0 best solutions below