I’m trying to make an analog clock for my project’s GUI, this clock needs to display the current time with its hands so I need to perform rotations on the Z axis of the hands in order to do this. I tried to rotate the GUI texture along the Z axis and nothing happens so I’m assuming rotations aren’t allowed on GUI textures, any ideas?
you can do this with the new GUI system in Unity2 - assign a rotation matrix to GUI.Matrix and draw your clock hands with GUI.Box
cool, I’ll give that I try thanks!!!
ok, I tried it, it does allow me to rotate GUI textures, but is there a way to set the pivit point? it’s hard to control the rotation for thicker objects when the rotation occurs on the top left exactly, or is there a trick to it?
You need to move your texture to its new center, rotate, then move back to its original position.
Here I post a complete example taking into account the grouping system:
var orgX : int = Screen.width - SpeedometerBackground.width - 50;
var orgY : int = Screen.height - SpeedometerBackground.height - 50;
GUI.BeginGroup (Rect (orgX, orgY, SpeedometerBackground.width, SpeedometerBackground.height));
GUI.Label (Rect (0, 0, SpeedometerBackground.width,SpeedometerBackground.height), SpeedometerBackground);
var tOffset:Matrix4x4 = Matrix4x4.identity;
tOffset.SetColumn(3, Vector4(-Needle.width/2.0 - orgX, -Needle.height/2.0 - orgY, 0.0, 1.0));
var tRotation:Matrix4x4 = Matrix4x4.TRS(Vector3(0,0,0), Quaternion.AngleAxis(needleAngle, new Vector3(0, 0, 1)), Vector3(1,1,1));
var tPostOffset:Matrix4x4 = Matrix4x4.identity;
tPostOffset.SetColumn(3, Vector4(Needle.width/2.0 + orgX, Needle.height/2.0 + orgY, 0.0, 1.0));
var tempMatrix:Matrix4x4 = GUI.matrix;
GUI.matrix = tPostOffset * tRotation * tOffset;
GUI.Label (Rect (0,0, Needle.width, Needle.height), Needle);
GUI.matrix = tempMatrix;
GUI.EndGroup();
A suggestion to whoever may be reading: It would be much more intuitive if GUI.matrix was expressed in group coordinates, that’s it, relative to the current group. Right now it’s referred to screen space, and you lose the power of groups.
I know. I would love it to work within groups, but GPUs have problems clipping to arbitrary shapes. If GUI.matrix worked within groups, you could do such things as:
- begin a group
- rotate
- begin another group
Now you could have an 8-sided clipping “rectangle”. Since that couldn’t be supported across-the-board, it ended up with this solution
aaah, I see… maybe rendering the groups to intermediate render targets that do the clipping for u?
I suposse that generating the coords in the CPU is not an option…
anyway… I don’t see a big deal as is, maybe it’s not as elegant or as intuitive as it could, but I don’t see it as very important, as least for me
That was sorta my opinion as well - when you know this will take many days to get right (Not all cards actually support rendertextures) - and have been working like mad for several months, it’s like… oh well… I can live without it