- Home /
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
Answer by Eric5h5 · Feb 03, 2010 at 12:50 PM
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.
thanks, this is what i thought. yes it seems simpler, but then the crosshair is also influenced by light and this is what i try to avoid
Just use a shader that has no lighting for the crosshair.
@Fattie: look up GUITexture in the Unity docs. There are no rotation functions.
Documentation is good like that! In any case, for this specific kind of problem, I usually use a mesh anyway- much more straightforward to handle transformations.
Answer by duck · May 12, 2010 at 09:37 AM
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;
}
}
Awsome man, this was exactly what I was looking for. $$anonymous$$y compass is working beautifully :) The OP should select this answer ins$$anonymous$$d.
Would this be of any use for drawing a player indicator (texture) as a GUI which correspondes to the actual player? Rather than using a secondary camera?
I know this is over a year old but I came across this and found it really useful. I only have one problem with it, I cannot update the position of the Item when the code is compiled using "build and run", even though it does rotate. It works fine when running in the Unity editor. Is there any solution to this?
Answer by swisscoder · Nov 08, 2011 at 11:24 AM
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
}
}
Great! It is possible to have javascript version? I need to create script that make change (move, rotation, scale in real-time) but I don't able to use C#... Thanks!
I apologize if I have offended with my request. However, you are not obliged to respond. You're right, for the future I will try to be more careful with the voting system. Unfortunately, I am always in a hurry. The fact that I do not answer the questions of others is easily explained: I do not have the skills. $$anonymous$$y knowledge of java or other programs is very limited and this is why I do a lot of questions. As you also noted in my posts, I always try to place the code of my script in case it was useful to someone. Stefano.
yes I saw that. I mean i am by no way hunting for those points.(I really don't care about them.) But marking questions as answered at least would help others. They will quite likely not look at an unanswered question, if they search for results ;) Anyway, your questions sure will help others too. Unfortunately I never used unityScript since I started with Unity. I also only know the link to the converter, that converts the other way round.. I even tried with changing the site's name "js_to_c" (http://www.m2h.nl/files/c_to_js.php) I hope for you, someone who is more used to unityScript can translate it for you :)
Original answer: Awesome, thank you for this. Exactly what I needed to get me started.
Answer by Lo0NuhtiK · Dec 19, 2011 at 12:19 PM
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 ; //<--?
}
Answer by Simar1992 · Sep 26, 2015 at 05:58 PM
@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 http://armedunity.com/files/file/41-loading-effect/
Your answer
Follow this Question
Related Questions
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
OnGUI vs GUITexture for Tablets and Phones 2 Answers
Make my HUD full screen 2 Answers
Resolution for assets on Android 1 Answer