Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Twister Gaming · Jun 27, 2014 at 03:40 PM · guidevelopment

How do I make that when I hover the mouse over GUI Texture it changes color?

Can someone please help me? I'm using function OnMouseOver() #pragma strict

 function Start () {
 
 }
 
 function Update () {
 
 }
 function OnMouseEnter() {
 renderer.material.color = Color.red;
 }

However I am supposed to attach some renderer thing? Please help and also please don't answer in the comments

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by YoungDeveloper · Jun 27, 2014 at 03:46 PM

Here's a simple C# example which will start you going.

 private Rect rect;
 private Event EVENT;
 
 private void Start(){
     rect = new Rect (0,0,100,100);
 }
 
 private void OnGUI(){
     EVENT = Event.current;
 
     if(rect.Contains(EVENT.mousePosition)) GUI.backgroundColor = Color.green;
     else GUI.backgroundColor = Color.red;
 
     GUI.Button (rect, "");
     //resets color to default
     GUI.backgroundColor = Color.white;
 }

Theoretically, you can check does the rect contains mouse in update, but for that you will have to use Input.mousePosition and fix y, as Input (0,0) starts from other corner. Like this

 //This will return mouse position in GUI "normal" coordinate system
 private Vector2 FixedInputMousePosition(){
     Vector2 pos = Input.mousePosition;
     pos.y = Screen.height - pos.y;
 
     return pos;
 }
 
 //And check it like this   
 private void Update(){
     if(rect.Contains(FixedInputMousePosition())){
         //Mouse is over rect
     }
 }




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
-2

Answer by Macmini1 · Jun 27, 2014 at 03:54 PM

You might have to put a Box Collider on the GUI Texture.

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
0

Answer by koray1396 · Jun 27, 2014 at 04:16 PM

you can simply use the following and you can change mouse events as you wish;

http://docs.unity3d.com/ScriptReference/Input.html

 GUITexture exampleGui;

 if(exampleGui.HitTest(Input.mousePosition)){ 
     if(!Input.GetMouseButton(0)){
         exampleGui.color = Color.red;
     }
 }
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 Twister Gaming · Jun 27, 2014 at 05:49 PM 0
Share

Cool! But it just stays there... How can I do it so that when the mouse gets "off" it turns back into the normal color?

avatar image
0

Answer by hav_ngs_ru · Jun 27, 2014 at 06:05 PM

You may draw GUI Button instead of GUI Texture, and just customize "normal" and "hover" styles as you wish. it will change it's view automatically.

And make you "active" style the same as "normal" style to make is looks not like button when user press it.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Android Button Screen 1 Answer

Developing a Dynamic GUI 1 Answer

Which are the computer requisites to develop in Unity3D? 1 Answer

GUI.DrawTexture isn't centred. Help? 1 Answer

GUIStyle returning error (error inside) 1 Answer


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