I want to create a series of knobs that the user clicks on, then left or right mouse movement changes a value, then releases to set it. Same concept as a slider, except visually they will see a round knob that rotates left or right. Has anyone done this already, and if so any hints / advice?
hmm…i’ve seen plenty of fligh sim’s made with Unity but I never heard about how they made the knobs…good question!
I’ll probably start roughing out some code for this later today, I’ve got a pretty good idea how I’ll do it (using a mouse down trigger and GUIUtility.RotateAroundPivot). I’m just hoping someone has already figured out a “better way”.
Sorted. Only problem I see is that you need a separate script for each rotating knob, unless someone can enlighten me on an easier way. Here’s a rough script that works:
var rotatingButton : Texture2D;
var rotationText : GUIText;
var rotatingGui : GUIStyle;
var theAngle : int = 0;
var thePoint : Vector2 = Vector2.zero;
var horizontalSpeed : float = 400.0;
function OnGUI () {
GUIUtility.RotateAroundPivot (theAngle, thePoint);
GUI.depth = 1;
if (GUI.RepeatButton (Rect(10,10,56,56), rotatingButton, rotatingGui)) {
if (Input.GetAxis ("Mouse X") > 0 theAngle < 130) {
theAngle += horizontalSpeed * Time.deltaTime;
print ("mouse moved right: " + theAngle);
}
if (Input.GetAxis ("Mouse X") < 0 theAngle > -130) {
theAngle -= horizontalSpeed * Time.deltaTime;
print ("mouse moved left: " + theAngle);
}
}
thePoint = Vector2(38, 38);
rotationText.text = theAngle.ToString();
}
You can put
GUI.matrix = Matrix4x4.identity;
after the first dial or you can save the matrix,
Matrix4x4 oldmatrix = GUI.matrix;
GUIUtility.RotateAroundPivot(rotSpeed, new Vector2(rect.x, rect.y));
GUI.DrawTexture(rect, imgFindingGames);
GUI.matrix = oldmatrix;
I’m not sure I’m following you Shaun. How does this allow me to add more dials in the same script?
The matrix operation at the top of your code will affect all gui code inside the OnGUI call. I thought you were asking how you can add more controls inside the same OnGUI function. Adding the GUI.Matrix=Matrix4x4.identity resets things, and allows you to use a new rotate or scale function.
The GUIUtility rotate and scale functions are just helpers for GUI.matrix.
function OnGUI () {
GUIUtility.RotateAroundPivot (theAngle, thePoint);
GUI.depth = 1;
if (GUI.RepeatButton (Rect(10,10,56,56), rotatingButton, rotatingGui)) {
if (Input.GetAxis ("Mouse X") > 0 theAngle < 130) {
theAngle += horizontalSpeed * Time.deltaTime;
print ("mouse moved right: " + theAngle);
}
if (Input.GetAxis ("Mouse X") < 0 theAngle > -130) {
theAngle -= horizontalSpeed * Time.deltaTime;
print ("mouse moved left: " + theAngle);
}
}
thePoint = Vector2(38, 38);
rotationText.text = theAngle.ToString();
//Reset the GUI.Matrix
GUI.Matrix=Matrix4x4.identity;
//Next control
GUIUtility.RotateAroundPivot (theAngle, thePoint);
if (GUI.RepeatButton (Rect(10,10,56,56), rotatingButton, rotatingGui)) {
if (Input.GetAxis ("Mouse X") > 0 theAngle < 130) {
theAngle += horizontalSpeed * Time.deltaTime;
print ("mouse moved right: " + theAngle);
}
if (Input.GetAxis ("Mouse X") < 0 theAngle > -130) {
theAngle -= horizontalSpeed * Time.deltaTime;
print ("mouse moved left: " + theAngle);
}
}
thePoint = Vector2(38, 38);
rotationText.text = theAngle.ToString();
}