- Home /
GUI Scale and Rotate
Hell everyone i Have 2 codes 1 to rotate my guis and the other to scale them. Both work great alone but when i put them together the GUI's just rotate and dont scale here are my Codes
The Scale
function OnGUI()
{
if(GUI.Button(ResizeGUI(Rect(470, 225, 100, 50)), "Settings"))
{
}
}
function ResizeGUI(_rect : Rect) : Rect
{
var FilScreenWidth = _rect.width / 800;
var rectWidth = FilScreenWidth * Screen.width;
var FilScreenHeight = _rect.height / 600;
var rectHeight = FilScreenHeight * Screen.height;
var rectX = (_rect.x / 800) * Screen.width;
var rectY = (_rect.y / 600) * Screen.height;
return Rect(rectX,rectY,rectWidth,rectHeight);
}
The rotation
private var rotAngle : float = 90;
private var pivotPoint : Vector2;
pivotPoint = Vector2(Screen.width/2,Screen.height/2);
GUIUtility.RotateAroundPivot (rotAngle, pivotPoint);
if(GUI.Button(ResizeGUI(Rect(470, 225, 100, 50)), "Settings"))
{
}
If anyone one knows how to make them both work please answer me im so frsutrated D: Thank You
skullbeats1
Code formatting does not work within or just after a list. I fixed the code formatting for you.
Answer by unimechanic · Oct 01, 2014 at 02:27 PM
You can use the GUI.matrix instead to do the scaling and rotation at the same time:
http://docs.unity3d.com/ScriptReference/GUI-matrix.html
You basically assign to it a matrix created with this static method:
http://docs.unity3d.com/ScriptReference/Matrix4x4.TRS.html
Here is an example:
Matrix4x4 transformation = Matrix4x4.TRS(
Vector3.zero,
Quaternion.identity,
Vector3.one);
void OnGUI() {
// We need to assign the GUI matrix before GUI controls are shown.
GUI.matrix = transformation;
if (GUI.Button(new Rect((Screen.width - 250) / 2, (Screen.height - 100) / 2 - 55, 250, 100), "TRANSFORM GUI")) {
transformation = Matrix4x4.TRS(
new Vector3(-Screen.width * 0.2f, -Screen.height * 0.7f, 0), // move the GUI a little to fit the transformed controls in the screen
Quaternion.Euler(0, 0, 15), // rotate 15 degrees clockwise around Z, because controls are in XY plane
new Vector3(2, 2, 1)); // scale GUI x2 times in X and Y
}
if (GUI.Button(new Rect((Screen.width - 250) / 2, (Screen.height - 100) / 2 + 55, 250, 100), "NORMALIZE GUI")) {
transformation = Matrix4x4.TRS(
Vector3.zero,
Quaternion.identity,
Vector3.one);
}
}
You are th GOD you saved my project and improved it this is so awesome :D
1 question do you know how to change this to javascript?
Pleasee i need help i used this script and it worked perfectly but the problem is that i used javascript for my whole game and this doesnt work in java i tried everything but it doesnt work please help
Your answer
Follow this Question
Related Questions
GUI Rotate and Scale dont work together 0 Answers
Create 3D GUI Button? 2 Answers
Scaling a GUI element to fit it's contents 0 Answers
Problem With GUI Circle Thickness 1 Answer
Animate GUI Texture 2 Answers