简体   繁体   中英

how to Change Font Size of UI Text element using UI Slider in unity C#?

using UnityEngine; using UnityEngine.UI;

public class slide : MonoBehaviour {

Text text;
public Slider slider;

void Start(){

    text = GetComponent<Text> ();

}

public void textscale(int value){

    slider.value     = text.fontSize;

}


}
  1. Replace your current script with the following one:

     public class slide : MonoBehaviour { public Text text; // Drag & drop the Text component inside the Inspector public void ChangeFontSize(float value) { ChangeFontSize( Mathf.RoundToInt( value ) ) ; } public void ChangeFontSize(int value) { text.fontSize = value; } } 
  2. Click on your Slider, add a new entry in the OnValueChanged event.

  3. Drag & drop the gameObject holding the script, and select slide > ChangeFontSize (Dynamic float)
  1. Add script:

     using UnityEngine; using UnityEngine.UI; public class ScalerScript : MonoBehaviour { public Text text; public void TextScale(Slider slider) { text.fontSize = (int)slider.value; } } 
  2. Create empty GameObject. Attach ScalerScript to it and drag and drop your Text object to Text field of the script in the Inspector window:

在此处输入图片说明

在此处输入图片说明

  1. Set Vertical Overflow to Overflow for your Text element: 在此处输入图片说明

  2. Set up Slider. Drag and drop empty GameObject with ScalerScript attached to OnValueChanged slider function. Drag and drop Slider element as a parameter to TextScale method. Set Slider's MaxValue to 200:

在此处输入图片说明

  1. Run the scene.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM