how to rotate GUI Textures

hi out there, is there a way to rotate a GUI Texture? because I want to make a spinning crosshair for my gun. is this possible with a PNG picture?

greetings tom from berlin

While you can't rotate a "GUITexture" - from the old-style GUI system, you can rotate a texture drawn by the new GUI system. Using this, you can create a script which behaves very similarly to the GUITexture component, but allows rotation.

Here's one that I've made, which you can place on an empty gameobject, which behaves similarly to a GUITexture, but allows you to rotate the texture. The object's world X & Y position affects where the texture is drawn on-screen.

using UnityEngine;
[ExecuteInEditMode()] 
public class RotatableGuiItem : MonoBehaviour {

    public Texture2D texture = null;
    public float angle = 0;
    public Vector2 size = new Vector2(128, 128);
    Vector2 pos = new Vector2(0, 0);
    Rect rect;
    Vector2 pivot;

    void Start() {
        UpdateSettings();
    }

    void UpdateSettings() {
        pos = new Vector2(transform.localPosition.x, transform.localPosition.y);
        rect = new Rect(pos.x - size.x * 0.5f, pos.y - size.y * 0.5f, size.x, size.y);
        pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
    }

    void OnGUI() {
        if (Application.isEditor) { UpdateSettings(); }
        Matrix4x4 matrixBackup = GUI.matrix;
        GUIUtility.RotateAroundPivot(angle, pivot);
        GUI.DrawTexture(rect, texture);
        GUI.matrix = matrixBackup;
    }
}

No, it's not possible to rotate a GUITexture. You could animate it with multiple textures to get the effect, but it would be simpler, faster and use fewer resources to put a plane in front of the camera and rotate that.

Hi there, I quickly edited Ducks example, in order to be able to select to which point of the screen the texture can be placed relative. (In Ducks example it will always be top left). Attention: In case the resolution changes, you will have to call UpdateSettings(), I don’t know if there is an event for screen resolution change, so please feel free to correct that.

using UnityEngine;

[ExecuteInEditMode()] 
public class RotatableGuiItem : MonoBehaviour
{
public Texture2D texture = null;
public float angle = 0;
public Vector2 size = new Vector2(128, 128);

//this will overwrite the items position
public AlignmentScreenpoint ScreenpointToAlign = AlignmentScreenpoint.TopLeft;
public Vector2 relativePosition = new Vector2(0, 0);

Vector2 pos = new Vector2(0, 0);

Rect rect;
Vector2 pivot;

void Start() 
{
	UpdateSettings();
}

void UpdateSettings()
{
	Vector2 cornerPos = new Vector2(0, 0);

	//overwrite the items position
	switch (ScreenpointToAlign)
	{
		case AlignmentScreenpoint.TopLeft:
			cornerPos =new Vector2(0, 0);
			break;
		case AlignmentScreenpoint.TopMiddle:
			cornerPos =new Vector2(Screen.width/2, 0);
			break;
		case AlignmentScreenpoint.TopRight:
			cornerPos = new Vector2(Screen.width, 0);
			break;
		case AlignmentScreenpoint.LeftMiddle:
			cornerPos = new Vector2(0, Screen.height / 2);
			break;
		case AlignmentScreenpoint.RightMiddle:
			cornerPos = new Vector2(Screen.width, Screen.height / 2);
			break;
		case AlignmentScreenpoint.BottomLeft:
			cornerPos = new Vector2(0, Screen.height);
			break;
		case AlignmentScreenpoint.BottomMiddle:
			cornerPos = new Vector2(Screen.width/2, Screen.height);
			break;
		case AlignmentScreenpoint.BottomRight:
			cornerPos = new Vector2(Screen.width, Screen.height);
			break;
		default:
			break;
	}

	pos = cornerPos + relativePosition;
	rect = new Rect(pos.x - size.x * 0.5f, pos.y - size.y * 0.5f, size.x, size.y);
	pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
}

void OnGUI()
{
	if (Application.isEditor)
	{
		UpdateSettings();
	}
	Matrix4x4 matrixBackup = GUI.matrix;
	GUIUtility.RotateAroundPivot(angle, pivot);
	GUI.DrawTexture(rect, texture);
	GUI.matrix = matrixBackup;
}

public enum AlignmentScreenpoint
{
	TopLeft, TopMiddle, TopRight,
	LeftMiddle, RightMiddle,
	BottomLeft, BottomMiddle, BottomRight
}

}

Did what I could to convert @swisscoder 's script to UniJava, if it doesn't work.... fix it lol

@script ExecuteInEditMode()
public var texture : Texture2D = null ;
public var angle = 0 ;
public var size : Vector2 = Vector2(128,128) ;
public var relativePosition : Vector2 = Vector2(0,0) ;
var pos : Vector2 = Vector2(0,0) ;
var rect : Rect ;
var pivot : Vector2 ;
public enum AlignmentScreenpoint{
   TopLeft, TopMiddle, TopRight,
   LeftMiddle, JustSeeing, RightMiddle,
   IfAnyones, BottomLeft, PayingAttention,
   BottomMiddle, OrOnlyCopyPasting, BottomRight
   }
var ScreenpointToAlign : AlignmentScreenpoint = AlignmentScreenpoint.TopLeft ;
 
function Start(){
   UpdateSettings() ;
}
 
function UpdateSettings(){
   var cornerPos : Vector2 = Vector2(0,0) ;
      switch(ScreenpointToAlign){
         case AlignmentScreenpoint.TopLeft:
           cornerPos = Vector2(0,0) ;
           break ;
         case AlignmentScreenpoint.TopMiddle:
           cornFlakes = Vector2(Screen.width / 2, 0) ; //<--?
           break ;
/**!!!------------------------->
  whoever's using this can type all the rest of the case switchers from swisscoders script, I don't feel like doing it...
*********************************************************/
         default:
           broken ; //<--?
      }
  pos = cornerPos + relativePosition ;
  rect = Rect(pos.x - size.x * 0.5, pos.y - size.y * 0.5, size.x, size.y) ;
  pivot = Vector2(rect.xMin + rect.width * 0.5, rect.yMin + rect.height * 0.5) ;
}
 
function OnGUI(){
   if(Application.isEditor){
     UpdateSettings() ;
   }
var matrixBackup : Matrix4x4 = GUI.matrixRedPill ; //<--?
   GUIUtility.RotateAroundPivot(angle, pivot) ;
   GUI.DrawTexture(rect, texture) ;
   GUI.matrixBluePill = matrixBackup ; //<--?
}

@Tom de Jank
yes its possible, you can rotate a GUITexture. just do this

void Update () {
		rotAngle += rotSpeed * Time.deltaTime;
		}

void OnGUI() {
		Vector2 pivot = new Vector2(Screen.width/2, Screen.height/2);
		GUIUtility.RotateAroundPivot(rotAngle%360,pivot);
		GUI.DrawTexture( new Rect ((Screen.width - size)/2 , (Screen.height - size)/2, size, size), loadingTexture); 	
}

or refer HugeDomains.com