【发布时间】:2012-03-07 10:05:55
【问题描述】:
我对编程/Unity 很陌生,并试图弄清楚如何使用 OnGUI 水平滑块。 我有三个滑块,范围为 0-100,并且想要一个名为 pointsLeft 的值在用户移动滑块时增加/减少。另外三个滑块的总价值不能超过100。如果有人能帮助新手,我将不胜感激!详情请查看代码。
using UnityEngine;
using System.Collections;
public class Slider : MonoBehaviour {
public float sliderA = 0.0f;
public float sliderB = 0.0f;
public float sliderC = 0.0f;
public float startingPoints = 100f;
public float pointsLeft;
void Start() {
pointsLeft = startingPoints;
}
void OnGUI () {
GUI.Label(new Rect(250, 10, 100, 25), "Points Left: " + pointsLeft.ToString());
GUI.Label (new Rect (25, 25, 100, 30), "Strength: " + sliderA.ToString());
sliderA = GUI.HorizontalSlider (new Rect (25, 50, 500, 30), (int)sliderA, 0.0f, 100.0f);
GUI.Label (new Rect (25, 75, 100, 30), "Agility: " + sliderB.ToString());
sliderB = GUI.HorizontalSlider (new Rect (25, 100, 500, 30), (int)sliderB, 0.0f, 100.0f);
GUI.Label (new Rect (25, 125, 100, 30), "Intelligence: " + sliderC.ToString());
sliderC = GUI.HorizontalSlider (new Rect (25, 150, 500, 30), (int)sliderC, 0.0f, 100.0f);
/*if(sliderA < pointsLeft) {
pointsLeft = (int)pointsLeft - sliderA; //this is not doing the magic
}
*/
//decrease pointsLeft when the slider increases or increase pointsLeft if slider decreases
//store the value from each slider when all points are spent and the user pressess a button
}
}
【问题讨论】: