Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
21
Question by Tom de Jank · Feb 03, 2010 at 12:15 PM · guiguitexturehud

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
14
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tom de Jank · Feb 03, 2010 at 03:09 PM 0
Share

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

avatar image Gunder · Feb 03, 2010 at 08:43 PM 2
Share

Use a layers to avoid the lighting.

avatar image Eric5h5 · Feb 03, 2010 at 11:35 PM 2
Share

Just use a shader that has no lighting for the crosshair.

avatar image Eric5h5 · Oct 07, 2011 at 09:14 AM 1
Share

@Fattie: look up GUITexture in the Unity docs. There are no rotation functions.

avatar image syclamoth · Oct 07, 2011 at 09:19 AM 1
Share

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.

Show more comments
avatar image
51

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;
 }

}

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PatHightree · Jul 29, 2010 at 10:16 AM 4
Share

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.

avatar image lampshade · Aug 23, 2010 at 07:54 PM 0
Share

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?

avatar image centaurianmudpig · Dec 17, 2011 at 02:09 PM 0
Share

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?

avatar image programad · Nov 16, 2013 at 11:18 PM 0
Share

This script misses the angle increment.

avatar image
7

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
 }

}

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image benfattino · Nov 28, 2011 at 11:06 PM 0
Share

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!

avatar image benfattino · Dec 18, 2011 at 03:49 PM 1
Share

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.

avatar image swisscoder · Dec 18, 2011 at 04:37 PM 1
Share

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 :)

avatar image jwinn · Oct 12, 2013 at 11:50 PM 0
Share

Original answer: Awesome, thank you for this. Exactly what I needed to get me started.

avatar image programad · Nov 16, 2013 at 11:18 PM 0
Share

This script misses the angle increment.

avatar image
4

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 ; //<--?
}
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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/

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jesusluvsyooh · Mar 12, 2017 at 10:32 PM 0
Share

This did it for me! Simple and effective. :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How do you get a minimap to stay rooted to the top left corner of the screen despite resolution changes? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges