Well basically the problem is that when a texture(in my case “needle”) is suposed to rotate around the gropus center (wich is the resolution of the background image (in my case “speedo”) will have an incorrect pivot position…
This would normally not happen, but since I have a scaling matrix modification, the groups are not working properly…
Heres the code:
using UnityEngine;
using System.Collections;
public class HUD : MonoBehaviour {
public Texture2D speedo;
public Texture2D needle;
// Internal
private Vector2 currentResolution;
private Vector2 nativeResolution;
private Vector3 nativeScale;
private float aspectRatio;
private float nativeRes = 720;
private float rotAngle = 0;
Rect speedoRect;
Vector2 speedoPivot;
void OnGUI ()
{
InitGUIMatrix();
if(speedo)
{
GUI.BeginGroup(new Rect(100,0,512,512)); // <---- Matrix gets messed up
GUI.DrawTexture(new Rect(0,0,speedo.width,speedo.height),speedo); // Background
Matrix4x4 prevMat = GUI.matrix;
pivotPoint = new Vector2(speedo.width/2, speedo.height/2); // Correct.
pivotPoint.x /= (screenRect.width)/(Screen.width); // Needs a modifyer after gropued?
pivotPoint.y /= (screenRect.height)/(Screen.height); // ...
GUIUtility.RotateAroundPivot(rotAngle, pivotPoint); // <---- Fix me...
rotAngle -= 100f*Time.deltaTime; // test rotation
if(needle)
{
GUI.DrawTexture(new Rect(0,0,speedo.width,speedo.height),needle); // Needle (rotating tex)
}
GUI.EndGroup();
GUI.matrix = prevMat;
}
}
/// Inits the GUI matrix.
void InitGUIMatrix()
{
aspectRatio = GetScreenAspectRatio();
currentResolution = new Vector2(Screen.width,Screen.height); // Just store
nativeResolution.x = (nativeRes*aspectRatio);
nativeResolution.y = nativeRes;
nativeScale.x = Screen.width / nativeResolution.x;
nativeScale.y = Screen.height / nativeResolution.y;
nativeScale.z = 1;
scaledMat = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, nativeScale);
GUI.matrix = scaledMat;
}
float GetScreenAspectRatio()
{
return (Screen.width*(1.0f/Screen.height));
}
}
And I included some sample textures to test with.
1616134–98528–$nismo.rar (334 KB)