- Home /
Help appreciated with new UI system (4.6 beta)
I have this text below which uses unitys old GUI system, when I move the slider, it controls the direction my object is facing, I'm having some trouble incorporating it into the new GUI system.
If anyone has used the new GUI system enough to understand it and could tell me how I could work this into the horizontal slider system, I'd appreciate it.
Is it controlled by code like this? Or does the new method work from the inspector?
public class ProjectileTest : MonoBehaviour {
public float hSliderAngleValue = 0.0f;
public float hSliderPowerValue = 0.0f;
public float sliderDirectionValue = 5.0f;
// Update is called once per frame
void Update ()
{
transform.rotation = Quaternion.Euler (-hSliderAngleValue, sliderDirectionValue, 0);
}
void OnGUI()
{
//GUI.Box(new Rect(Screen.width-120,Screen.height-240,100,50),"Angle");
hSliderAngleValue = GUI.HorizontalSlider (new Rect (Screen.width-120, Screen.height-180, 100, 20), hSliderAngleValue, 0.0f, 80.0f);
}
Answer by HarshadK · Aug 26, 2014 at 12:14 PM
The new GUI is not accessed from the OnGUI. These two are different.
You create the slider from the editor (even though is is possible it is rather cumbersome to create new GUI elements from code).
Then you can call your function that changes the direction the object is facing from the On Value Changed event Event Trigger.
Also on your code that changes the direction of object you can get the reference to your Slider script component (One great thing about new GUI system is that it uses game objects and components itself to construct the GUI) and can change the direction according to that value.
Something like:
using UnityEngine.UI; //Don't forget this
public class RotationTest : MonoBehaviour {
public Slider slider;
public OnSliderValueChanged()
{
// You can access the current value of slider using slider.value
// And set the direction using your slider value accordingly
}
}
You can also check a video on using sliders in the Unity's learn section at UI Slider
Thanks @Harshad$$anonymous$$, that helped a lot, I have it up and running again with the new UI, thanks a million
Your answer
Follow this Question
Related Questions
How to Lock a Slider Value 1 Answer
How to make a Horizontal Slider slide between resolutions? 3 Answers
Horizontal Slider issue 0 Answers
Unity 4.6 InputFields need help 1 Answer
move object with slider 1 Answer