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
0
Question by Lukas · Apr 28, 2010 at 09:12 PM · guitexturemouse-over

How to create GUITexture which does not react on over on transparent parts of it.

I have a GUITexture and it have transparent parts. If I use mouse over function it also reacts as over on those transparent parts. My question is, how to make GUITexture not to react on those transparent parts, so mouse over function is not called, when pixel under it have transparent color. Thanks for answers.

Comment
Add comment · Show 1
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 Cyclops · Apr 29, 2010 at 12:41 PM 0
Share

@Lukas, if you get a helpful answer, please be sure to upvote it (and checkmark the best answer, if any).

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Peter G · Apr 28, 2010 at 10:29 PM

There are a couple ways. I would do the second personally.

  1. Create multiple rects that encompass the GUITexture's opaque part and then, rather than check if the mouse clicked in the GUITexture's rect, check to see if any of these rects contain the point.

  2. When the user clicks the object, get the alpha value of the pixel they hit and if it is greater than one, treat the click as a hit, otherwise a miss. Here's a basic example.


var guiRect : Rect; //only used if using GUI 2.0. var useOnGUI : boolean = false;

private var texRect : Rect; var tex : Texture2D; //used for 2.0. Auto-assigned otherwise.

private var imageSheerX : float; private var imageSheerY : float;

private var pixelCoord : Vector2; private var guiPos : Vector2;

function Start () {

  if(useOnGUI) {

     guiPos = Vector2(guiRect.x, guiRect.y - guiRect.height);

     imageSheerX = guiRect.width / tex.width; //these two lines are important.
     imageSheerY = guiRect.height / tex.height; //they scale the screen position to match the same pixel on

  }

  else {

     texRect = guiTexture.pixelInset; //cache a link to the guiTexture and the coordinates.
     guiPos = Vector2(texRect.x, texRect.y);

     tex = guiTexture.texture as Texture2D; //cache a link to the texture;

     imageSheerX = texRect.width / tex.width; //these two lines are important.
     imageSheerY = texRect.height / tex.height; //they scale the screen position to match the same pixel on
         // on the texure.
  }

}

function OnMouseDown () { //account for shifts in the transform.

  var offset : Vector2 = Input.mousePosition - guiPos; //Find the mouse position
     //relative to the corner of the GUI.
 print (offset);

  pixelCoord = Vector2(offset.x / imageSheerX, offset.y / imageSheerY);
     //Scale the coordinates to match the Texture.

  if(tex.GetPixel(pixelCoord.x, pixelCoord.y).a > 0.1) {
     // check the alpha of the pixel.  Only run code if alpha > .1
     //Place your normal button commands in here.
     // anything else will automatically run no matter the transparency.
       print("Area is opaque");
  }

}

function OnGUI () { //If you are using 2.0 GUI.

 if(useOnGUI) {


     var offset : Vector2 = Input.mousePosition - guiPos; //Find the mouse position
     //relative to the corner of the GUI.

     pixelCoord = Vector2(offset.x / imageSheerX, offset.y / imageSheerY);
     //Scale the coordinates to match the Texture.


      if(GUI.Button(guiRect, tex)) {
         if(tex.GetPixel(pixelCoord.x, pixelCoord.y).a > 0) {
             print("I was clicked");
         }

     }
 }

}

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 Lukas · Apr 29, 2010 at 07:22 AM 0
Share

Thanks that really helps. And could be done sth. like this also with 2.0 GUI? Could I just get texture and access it as 2D field using GetPixel function? And last question, is there somewhere documentation in which I could find also functions like this, because I was searching all over Unity documentation and did not found it. Thanks for answer.

avatar image Lukas · Apr 29, 2010 at 08:37 AM 0
Share

There are two errors in you script.

  1. $$anonymous$$ouse positioning if down left oriented, texture positioning (pixelInset) is top left oriented, so Input.mousePosition - guiPos need to be changed. for example like this:
    var offset : Vector2 = Vector2(Input.mousePosition.x - guiPos.x,Input.mousePosition.y - (Screen.height + guiPos.y));

  2. Your script does not take into count that texture could be positioned using transform.

avatar image Peter G · Apr 29, 2010 at 10:26 AM 0
Share

Two comments. 1. GUITextures calculate from the bottom left, but 2.0 calculates the position from the top left. Now if you use 2.0 GUI then you will have to correct that, but with a GUItexture, you should be fine. 2. It did not occur to me that the tex. might be moved by the transform. So thank you for pointing that out. I'll correct it soon if I can, but for an easy workaround set the tranform position to (0, 0, 0) then move the object around using only the pixel inset. It's not perfect, but it will work until it is fixed.

avatar image Peter G · Apr 29, 2010 at 10:28 AM 0
Share

These are all classes listed in the script reference. Search Rect, Vector2, Input, and Texture2D and Unity will bring up the class reference, a guide to the class and its functions.

avatar image Peter G · Apr 29, 2010 at 07:44 PM 0
Share

I add 2.0 functionality to the script. Worked on adding position in as a calculation, but got some weird results. I'll try some more, but it's just as easy to use pixelInset and position the gui at (0, 0, 0).

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

No one has followed this question yet.

Related Questions

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

How to prevent guitexture from vertically shrinking? 0 Answers

Simple GUI In game 1 Answer

[CLOSED] Pixel Inset ---> transform scale convert 0 Answers

Door Problem 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