unity 3d student

Beginner B25 – GUI Texture & Mouse Events

Using simple GUI Textures and Mouse Events to create interaction in Unity.

Code Used (Javascript)

var normalTex : Texture2D;
  1. var hoverTex : Texture2D;
  2.  
  3. function OnMouseEnter () {
  4.  guiTexture.texture = hoverTex;
  5. }
  6.  
  7. function OnMouseExit(){
  8.  guiTexture.texture = normalTex;
  9. }
  10.  
  11. function OnMouseDown(){
  12.  Debug.Log("clicked");
  13. }

Further Reading

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites

34 Comments

    At the end (at time of writing) of the beginner modules seems as good a place as any to say how staggeringly impressed I am with this incredibly helpful series.

    My method working through these was to watch the video and then try to replicate what I’d seen. With only a handful under my belt I found myself beginning to combine bits of lessons together and working out new tweaks based upon the foundation you’ve provided – “hey what if that spotlight varies in intensity in relation to the distance to the wrecking ball, ooh and add forces to the wrecking ball based on key input” etc.

    The best bit is that I’ve now got a handful of scenes chock full of bouncing swinging spinning things which I refer back to regularly for reminders or ideas like some 3D visualised search engine…. hmmm…. 3D search engine…. I’ll be right back, just need to prototype something :o)

    • Matt – thanks very much for your comment I really enjoy hearing people’s stories like this, it makes it worthwhile – so thank you very much. Please post back with anything else you do I’m sure everyone would like to see what you’re up to! cheers 🙂

    Oh a long way from exciting projects to be honest 🙂 Got a smattering of Delphi & Pascal and a smidgen of Blitz (and of course succesfully conned my parents that a ZX Spectrum would help my education back in the day) but still complete beginner.

    Anyway – temporary holding site but my first “hello world” is now up at http://www.mattandem.plus.com/unity/WebPlayer.html (yes, I loved lesson 11)- pretty much utilising each of your beginner lessons and little else besides (though I’m certainly beginning to see the need to investigate the “proper” GUI system).

  • AWESOME!

    Matt that rocks, and exemplifies everything I made this website for, so people can take the building blocks and create something original and awesome like that! thanks for posting it man, I may make a dedicated area on the site for stuff like this, cheers, have a good weekend.

    Will

  • I love it how you pronounce GUI as “G-U-I”. I’ve always pronounced it “gooey”, like fudge cake. Mmmm…

    Oh, and I’m a Brit too. I’m not an American loving your “quaint accent”. 😉

    • Ha! wicked! bout time I heard from some ‘brits’, where are you based man?

    I pronounce it however seems best at the time. Have you ever tried to say gooey with a French-Zummerzet accent? I did that in one of my classes and people thought I was choking. Seriously.

  • Hey wgstone your video’s are great. I could watch for hours of your videos 🙂 a lot of videos online by other people makes me fall asleep or get p-off.

    You sound very professional and always apply the right information each time.

    I’ve just started learning unity the last few days, so I’m still super noob. I’m probably going to buy the pro unity in the next day or two.

    I’m from the UK yey I thnink in the future we should create a site for UK unity fans to chat and brainstorm, maybe in time we could all combine to create team of game devs.. hheehe well I have a lot to learn still.

    Keep up the good work mate 🙂

    • Cheers Santino, let us know when you have something created and uploaded, be good to see what you create!

    hi wgstone.. thanks for your wonderful tutorials.. it helps a lot. you gave me strength and courage to continue my studies with your tutorials. you are a very reliable tutor. continue doing this, you dont know how much you help students like me., i tell my friends to visit your site. =)

    -Rona of Philippines.

    • Hi Rona , thanks so much for your comments, I will continue to do this soon as I can take time off other work to carry on – currently tied up with the 2nd edition of my Unity book! have a few new modules prepared though, just not had time to record them.

    Another good one yo!

  • Hello Will. Doubt you will reply but on the off chance. I am using ‘function OnGui’ and need when an object is click for it to change the image in a texture. much like the tutorial above but a bit more advanced.

    var tex1 : Texture2D;
    var tex2 : Texture2D;

    function OnGui{

    }

  • sorry pressed enter by accident.

    var tex1 : Texture2D;
    var tex2 : Texture2D;

    function OnGui{

    if(GUILayout.Buttin((tex1), GUILayout.width(119),GUILayout.Height(119))){
    Destroy(GameObject.FindWithTag(“bike”));
    Instantiate(bigWheel2, Vector3(13,13,-507),Quaternion.identity);
    guiTexture.texture = tex2;

    }

    }

    However i get the following error message: MissingComponentException: There is no ‘GUITexture’ attatched to the “ramp” game object, but a script is trying to access it. You probably need to add the GUITexture to the game object “ramp” or your script needs to check if the component is attatched before using it.

    It is definatly attatched to ramp so i need to check if it’s attatched before using it. How do i do this? i have tried if statements like:

    if(tex1){
    guiTexture.texture = tex2;
    }

    but that doesn’t work. any ideas?

  • If you want to ensure a guitexture component is present you could just say if(guiTexture){} otherwise you’re simply checking if that tex1 variable exists. Sorry I haven’t replies to your other email, no computer setup at the moment, all phone based.

    • Cheers Will, i don’t really follow but i will have a fiddle. Don’t worry if it’s too much hassle with the other email, it’s not exactly your job anymore haha.

    Hey! loved ur tutorial! helped me a lot!… I have a question though: How does it work when im using more than one button in the same scene, because I´ve been trying to do that and all the buttons change when rolling over, and also the links dont work propelly!… any ideas why? 😉

    • Hi Laura, this is a fairly basic example to get you started. Not sure why your script is changing everything, are your objects independent?

    Hey Will, another great tutorial! This my second day going through this site and completing all the module tutorials, and I have to say that this is the first time I’m really *getting* Unity, which is exhilarating!

    One quick question about texture importing. Right now any non-square file I create in Photoshop get’s distorted once I import it as a texture into Unity. For example, I created a button image in PS that was 64px in height, and 320px in width. Once I imported this into Unity however, it imported it with 64px in height and 256px in width.

    I’m guessing textures have to meet a certain height and width criteria to be properly imported, I just don’t know what the secret is. Any help would be great!

    • Hi Tiff, its just doing powers of 2, if you select the file in the project panel and in the Importer component in the inspector, set texture type to GUI if you’re using it as a GUI Texture or similar. Otherwise yes it’ll scale to power of 2 sizes. You can also always alter width and height of things manually in GUI Texture component pixel inset settings or in your GUI code if you do it that way.

      Glad you are enjoying the tutorials, will check out your site, have fun!

      • Thanks for your reply! Helpful stuff.

    if i was to make a button like this

    GUILayout.Button(“Cube”)

    and i wanna check if the button is clicked and each time its clicked to create a PrimativeTpye.Cube

    can i do if(GUILayout.Button(“Cube”))
    {
    var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

    }

    if not how would i go about making something like this work

  • Hey W! I just found your site, and I have to tell you, is amazing.
    I just started to learn Unity, and your tutorials and tips are help´ng me a lot to understand the whole thing. So, here you have a fan from Argentina!
    I hope soon can share with you mi first proyect. Thank you really much for your time and dedication.
    I have just a little question about this tutorial. I create my scene “Main Menu” and inside that, 3 buttons, and asign diferents variations in the scripts for each one, based on ehat you show here (couse I have “Options” “Start” and “Quit”)
    Averything is work just perfect, just one thing that I can´t fix:
    If I play de preview in my second monitor, (1920×1080) wheres is my preview window of Unity always, or if build and run the game in full resolution, the buttons are exactly like I created. BUT, when build the scene, for example in stand Alone 1024×768, or somehow the window are not like my preview window, the buttons dont change they size! So, if the window is small, I have the 3 buttons all together and ruining the design.
    Is there a posibility to say to the buttons, “reescale in order to maintain de design” ?
    Thanks you A LOT for your time! And please sorry if my inglish is no perfect, I´m still learning!

  • hello. but iOS(iPhone, iPad) Touch No.
    only PC Mouse?? how iOS Image Click??
    Help Please….

  • Hey James thanks for the tutorial on the GuiTexture. I have a question and i have been asking around but seems no one help me with. My question is for the GuiTexture i notice you can create start menu,life bars and other type of things using the GuiTexture. But my question is can you use the GuiTexture that you import from photoshop say for instances you imported four text from photoshop as a guiTexture in unity. Can you script the four GuiTextures as questions and answers to function together as correct and incorrect answer?

    • Yes, entirely possible. ps. who is James? I’m the only one running this site! who let James in?

    hi, I’m trying to make similar to this but box collider. I put this code:
    function Update () {
    Start();

    }

    var image:GUITexture;
    var canShow = false;

    function Start() {
    image.enabled = false;

    }

    function OnTriggerEnter(other : Collider) {
    var g_infotext = gameObject.Find(“g_infotext”);
    canShow = true;
    image.enable = true;

    if (other.gameObject.CompareTag (“Player”) && !canShow) {
    image.enable = true;
    }
    }

    function OnTriggerExit(otherCollider : Collider) {
    image.enable = false;
    canShow = false;
    }

    into it, and when I pass it can check the canShow, but no GUITexture appears… Waht’s wrong with this? Can anyone help?

    • Many things wrong here – you’re calling Start(); in update – Start() is inbuilt and runs when the scene loads, and you should not call it again, definitely not EVERY frame! You also delcare ‘image’ as a GUITexture variable but don’t tell unity which GUITexture component you’re referring to and on which object. Try saying image = GetComponent(GUITexture); in your Start() function to set the variable – this is IF the GUITexture component happens to be attached to the object this script is on.

    Hello I’m trying this in C#, but it doesn’t work. My code is:

    public class MouseClick : MonoBehaviour {
    public Texture2D normalTex;
    public Texture2D hoverTex;

    void OnMouseEnter() {
    guiTexture.texture = hoverTex;
    }

    void OnMouseExit(){
    guiTexture.texture = normalTex;
    }

    void OnMouseDown(){
    Debug.Log(“clicked”);
    }
    }

    I don’t get any error, just nothing happens when I put the mouse over the button or I click in it.

    Thank you for your help

  • Thanxxx A Ton Thats What I Was Lookin For….
    I am just an amatuer in Unity…so i was not knowing how to implement Mouse hover effect on GUI Texture… Thanksss

  • Hi,

    While we’re on the subject of GUIs, I wonder if you could explain when you would use the OnGUI() function. I can see that you can display textures on the screen by simply using the GUITexture component which holds the Texture2D (as in this example), but when would you want/need to use OnGUI()?

    Also, is this function executing elsewhere in the code, regardless of whether you create your own version?

    Thanks

  • hey how about on hover play a sound pls add the code line !!!

  • Hello, Thanks for the videos, I feel I’ve learnt more from the few videos Ive watched today that the weeks of videos I’ve watched before. I’ve trying to get an object to move from one side of the screen to the other when I press a button. I cant seem to find a solution. So far all I’ve managed to do is get it to wiggle a little bit. Many thanks.

Leave a Comment