- Home /
Slider and Input Field Value Updates
I am wondering about how to setup a slider and an input field to take values from each other based on whichever one is receiving input.
I found this code that will pass slider data from the slider to the input field :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[ExecuteInEditMode]
public class SliderWithValue : MonoBehaviour {
public Slider slider;
public Text text;
public string unit;
public byte decimals = 2;
void OnEnable ()
{
slider.onValueChanged.AddListener(ChangeValue);
ChangeValue(slider.value);
}
void OnDisable()
{
slider.onValueChanged.RemoveAllListeners();
}
void ChangeValue(float value)
{
text.text = value.ToString("n"+decimals) + " " + unit;
}
}
--- from this discussion: @maikkel
http://answers.unity3d.com/questions/836488/how-to-send-on-value-changed-in-ui-slider-to-text.html
I am wondering how add conditional to allow inputs into the input field to update the slider and slider inputs to update the input field text.
I want to continue to use the editor to drag and drop targets but after adding
public InputField input;
Now I am stuck...
Anybody out there that knows how to write this?
thank you
-ryan
Answer by AurimasBlazulionis · Oct 28, 2016 at 09:25 PM
In the input field, only allow numbers to be entered. Then you set a callback of the input field on value changed to a public function like InputValueChanged
.
Inside InputValueChanged
do something like if (slider.value != float.parse(input.value)) slider.value = float.parse(input.value);
Not quite sure how to implement that but Ill give it a shot:) thx!
Remember to accept the answers if they help you.