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 kebrus · Feb 08, 2013 at 04:34 PM · methoddelegate

Multiple classes delegates combining methods?

I'm gonna be honest, i'm not sure if i understand delegates yet.

The thing, i thought that after registering a method belonging to a specific to a delegate of another class that ONLY the code from that method would run on the assigned gameobject. But that doesn't seems to be the case.

I have this setup with two different kinds of buttons and i have a handler that detects the input and calls the delegate. But every button runs the code of the TWO different kinds of buttons.

Why does this happens and how you do this correctly?

Comment
Add comment · Show 6
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 hoy_smallfry · Feb 08, 2013 at 06:27 PM 0
Share

Can you show some code of how you are adding the delegates, and show the definition of what you are adding it to?

avatar image dubbreak · Feb 16, 2013 at 01:22 AM 0
Share

Yeah we need a code snippet to see what you're doing. Your declarations, how you're calling it etc.

avatar image Chronos-L · Feb 16, 2013 at 01:27 PM 1
Share

Either he fix it, or he just gave up. Sometimes I wonder why we care so much while the one asking the question doesn't seem to care at all.

avatar image kebrus · Feb 16, 2013 at 10:33 PM 0
Share

Sorry, i completely missed the first reply email. From my understanding delegates always run on every object that adds the same delegate method, so i ended up doing a check to see if the object calling it is the same of the running method... it works for now but is this really the intended way of using delegates?

I'll post a code snippet tomorrow at work.

avatar image kebrus · Feb 18, 2013 at 10:09 AM 0
Share

this is my declaration on a touch handler:

 public delegate void handleTouchBegan(RaycastHit rh, int fingerid);
 public handleTouchBegan BeganTouch;

then i use it in mul$$anonymous$$ch loop like this

 if (touch.phase == TouchPhase.Began){
     if (Physics.Raycast (ray, out hitInfo, 1.0e8f)){
     BeganTouch(hitInfo, touch.fingerId);
     }
 }

finally, on each button handler i have a code like this:

 void Start () {
 TouchHandler.instance.BeganTouch += handleTouchBegan;
 }

 void handleTouchBegan(RaycastHit hit, int id){
         fid = id;
         if(hit.collider.gameObject == this.gameObject){
             //Do Something
         }
 }
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dubbreak · Feb 19, 2013 at 12:15 AM

This might be of help? For my buttons (I use ngui) I have this script added on to them:

 using UnityEngine;
 using System.Collections;
 using System;
 public class EventButton : MonoBehaviour {
     
     public event Action Clicked;
     public bool buttonEnabled { get; set; }
     
     void Start()
     {
         buttonEnabled = true;
     }
     void OnClick()
     {
         if (Clicked != null && buttonEnabled) Clicked();
     }
 }

OnClick gets called by ngui (since it's attached to an ngui button) by the normal unity messaging. The gist is OnClick gets called when it is clicked which then calls my own event. Since you're not using ngui you use your touch logic to determine it has been clicked. I'm using the built in Action delegate since I'm not sending info with it, could be a different custom delegate or

 public delegate void ClickDelegate;
 public event ClickDelegate Clicked;

Action is just easier and cleaner if you don't have any arguments.

I then reference my buttons from my main script:

 public class GameManager : MonoBehaviour {        
     
     public EventButton playButton;
     public EventButton leaveButton;    
     
     void Start()
     {
         playButton.Clicked += HandlePlay;
 
     }
 
     private void HandlePlay()
     {
         DoPlayStuff(true);    
     }    
 
 }

That's simplified a bit since I don't subscribe to the event until network stuff happens and some buttons get subscribed and unsubcribed depending on state.. but it gives you and idea of how I'm using events to deal with buttons.

In case anyone that's using ngui reads this, I think the latest ngui version has events you can subscribe to now (rather than just the unity messaging). So you shouldn't have to roll your own like this.

Comment
Add comment · Show 2 · 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 kebrus · Feb 19, 2013 at 12:51 AM 0
Share

whats the advantage of using events?

and one clear difference from my approach is that you "subscribe" your buttons on the manager while i do it on the button itself, i did it like this because that way i could add any kind of button to the scene without having to change the manager

avatar image dubbreak · Feb 19, 2013 at 01:02 AM 0
Share

If it's within the button itself then why have an event? You can just run whatever method you need (using a coroutine if it's going to take a while).

If you don't have something external subscribing to the event I don't see the point of having an event.

Also you don't have to hardcode the references to the buttons like I did. Simple example. Ins$$anonymous$$d you can get all the objects with "eventbutton" components and subscribe to the click. Then you can deter$$anonymous$$e what button is being clicked in the handler. The point is so that you don't have to hardcode the button to references of other external things that should happen. It just says ,"Hey someone clicked me, if anyone cares here's the info and you can go do your own thing."

If you look how buttons are handled in winforms (for example) the form subscribes to the event in the form.designer.cs (adding whatever your handler method is). To do specific things you have to subscribe from somewhere. And to do something interesting it's external to the object raising the event.

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Action delegate doesn't show in inspector. 1 Answer

Delegate bug, or intended behaviour? 0 Answers

Interaction script 2 Answers

Singleton class or delegate? 1 Answer

Dynamic Anonymous Delegates/Lambda On UntyEvent 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