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
1
Question by jackalkane · Jan 18, 2017 at 08:42 AM · eventsingletondelegatemodularbroadcast

Cleanest way to decouple Manager class with events

So here's my dilemma. I've been kicking around online many hours the last few weeks trying to figure out a solution to my problem.

I have an animation system and sound system in use throughout multiple scenes in my game which I instantiate when I need them. I'm naming the sound system class AudioManager in this instance. The AudioManager is one of the few singletons I have in my game.

I have scripts attached to different prefabbed game objects that I instantiate that make noise. These objects come and go so it's not like I can just click and drag these onto the AudioManager. I want to be able to just essentially broadcast an audio play event like PlayNoise("coinDrop") in an ignorant way from anywhere in these scripts and have AudioManager listen for it and handle the fadein/fadeout and intricacies. I could do that easily by just referencing the audiomanager as a singleton and calling AudioManager.Instance.PlayNoise("blah") but the one improvement I'd like to make is that I'd like to be able to totally remove the AudioManager from the entire project and still have the game work without having to change any code or recompile anything. I don't really want to have a bunch of delegate subscribers and list a bunch of functions either. Just a simple event broadcast out.

The purpose for this is that I'm looking for more modularity. I've done the drag and link with OnClick and unity events and it works great for in game scenes but works like garbage when you have an intricate UI system and you need to be able to prefab everything.

TL:DR; I want to announce an event from multiple classes/gameobjs without having to know anything at all about the listener (e.g. no delegate subscribers in every class I need to make a sound from). The listener knows nothing about the announcers; it just knows to do something when it hears an event. I've heard of SendMessage but I've heard negative things about it with regard to efficiency and it sounds like some protocode from when Unity was new.

Thanks!!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by juicyz · Jan 19, 2017 at 12:52 AM

Something like https://unity3d.com/learn/tutorials/topics/scripting/events-creating-simple-messaging-system?playlist=17117 ?

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 jackalkane · Jan 19, 2017 at 06:50 AM 0
Share

So this isn't exactly what I was wanting but I pretty much realized the only way without listeners and subscribers in the manager class was to have it search the entire scene like Send$$anonymous$$essage does (inefficient) so I went ahead and implemented what the tutorial described. I made the Event$$anonymous$$anager a true singleton and wrote a generic function that accepts multiple things and turns them into strings. IT seems to work really well.

I saw that tutorial before but it wasn't exactly what I was looking for so I kind of skimmed the code and never took the time to understand it and watch the vid. The Unity3d tut vids are actually quite good these days.

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

4.6 adding onClickListener to instatiated buttons on a scrollview and click first button then it contain last index value of delegate method 0 Answers

is there any easier way to do this ? 2 Answers

Delegate an event 2 Answers

What part of the delegate gets passed back?The return type? 1 Answer

the script runs when it was first created, but if I close my unity and re-run the game the script says nul reference ?! Why is it like that? please help 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