Beginner B25 – GUI Texture & Mouse Events
Using simple GUI Textures and Mouse Events to create interaction in Unity.
Code Used (Javascript)
var normalTex : Texture2D;
-
var hoverTex : Texture2D;
-
-
function OnMouseEnter () {
-
guiTexture.texture = hoverTex;
-
}
-
-
function OnMouseExit(){
-
guiTexture.texture = normalTex;
-
}
-
-
function OnMouseDown(){
-
Debug.Log("clicked");
-
}
Further Reading
21 Comments
Leave a Comment









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