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
1
Question by Mattias-Wargren · Mar 03, 2011 at 11:15 PM · guireferenceguitexturelistenerdispatch

GUI problem: Lack of Events and Listeners.

I have a screen (Empty GameObject) with three buttons (child GameObjects with GUITexture + Script) on my scene. What is an good approach when dealing with buttons? I want to know what button the user clicked without having to add a reference to the Screen in the button class. I am used to Actionscript 3 where you add listeners and dispatch events. Any tips?

- Screen
  |- Button1
  |- Button2
  |- Button3

Comment
Add comment · Show 4
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 yoyo · Mar 04, 2011 at 07:48 AM 1
Share

If you have a button script you can tell which game object it's attached to with the gameObject member variable. To access the parent game object (the screen), use gameObject.transform.parent.gameObject.

avatar image yoyo · Mar 04, 2011 at 07:49 AM 0
Share

You could also look at the use of scripted user interfaces where you implement an OnGUI method and call things like GUI.Button to create your interface.

avatar image yoyo · Mar 04, 2011 at 07:49 AM 0
Share

... and if those suggestions don't help, please share some of your code so we get a clearer idea of what you're trying to do and how.

avatar image amd2002a · Dec 29, 2011 at 11:06 PM 0
Share

read the docs...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Peter G · Dec 29, 2011 at 11:40 PM

I'm a little confused because I'm not sure why you want to reference the Screen class, but you can make an event based system.

You have a little bit of choice if you want a global button manager or events fired by individual buttons. Here's an individual button system:

 using System;
 using UnityEngine;
 
 public class Button : MonoBehaviour {
     private GUITexture thisTex;
     //Cache a reference to the button.
 
     public event Action onClick;
     //Action is a generic delegate in the System namespace.
 
     void Start () {
         thisTex = guiTexture;
         StartCoroutine( ListenForButton() );
     }
 
     //Here's a performance trick.  Checking for Input in an infinite coroutine will help performance.
     //Update() is called via reflection slowing it down some.
     //And we aren't allocating any additional memory in the loop.
     private IEnumerator ListenForButton() {
         for (;;) {
              if(thisTex.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0) ) {
                   if(onClick != null) 
                       onClick();
              }
              yield return null;
         }
     }
 }

Example:

    someButtonScript.onClick += buttonListen.SomeMethod();

Now if you want to get fancier, you can write an extension method that gets the event by referencing the guiTexture.

 using System;
 using UnityEngine;
 
 public static class ExtensionMethods {
       public static void AddListener (this GUITexture guiTex, Action listener) {
              var buttonScript = guiTex.GetComponent<Button>();
              if(buttonScript != null)
                     buttonScript.onClick += listener;
              else
                     Debug.Log("There is no Button script attached to the Game Object");
     }
 }

And then you can access it like such:

  var guiTex = (GUITexture)FindObjectOfType(GUITexture);
  guiTex.AddListener( someScript.Method() );
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 Peter G · Dec 29, 2011 at 11:42 PM 0
Share

P.S. I'm sure there's a typo somewhere in there.

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

2 People are following this question.

avatar image avatar image

Related Questions

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

Creating GUITexture with a reference 1 Answer

dynamic GUI Texture.. clipping and rotation HUD 1 Answer

script to create gui when detection collision between cube and first controler person ?? 1 Answer

WorldToScreenPoint returns the same value when facing the opposite direction. 0 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