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 Reloaded23 · Feb 10, 2016 at 05:33 AM · eventssingletondelegate

Singleton class or delegate?

Which is the best approach to call a function. Lets take an example of a car game. Singleton class

GameManager is a singleton class and I have one more class called PlayerCar.

 public class GameManager : MonoBehaviour {
 
     private static GameManager _instance;
 
     public CameraManager camera;
     public SoundManager sound;
     public UIManager ui;
 
     public static GameManager instance{
         get{
             if(_instance == null){
                 _instance = GameObject.FindObjectOfType<GameManager>();
             }
             return _instance;
         }
     }
 
     public void CarDamage(){
         camera.Shake();
         sound.PlayDamage();
         ui.ShowDamageText();
     }
 }
 
 
 
 public class PlayerCar{
 
     private void OnDamageRecieved(){
         GameManager.instance.CarDamage();
     }
 
 }

Now whenever playercar recieves damage OnDamageRecieved is called which in turn calls Gamemanagers CarDamage function.

Now let's us take another method which is delegate and events

 public class PlayerCar{
 
     public delegate void Damage();
     public static event Damage ShakeCamera;
     public static event Damage PlayDamageSound;
     public static event Damage ShowDamageText;
 
     private void OnDamageRecieved(){
         ShakeCamera();
         PlayDamageSound();
         ShowDamageText();
     }
 }
 
 public class CameraManager : MonoBehaviour{
 
     void OnEnable(){
         PlayerCar.ShakeCamera += Shake;
     }
 
     void OnDisable(){
         PlayerCar.ShakeCamera -= Shake;
     }
 
     private void Shake(){}
 }
 
 
 public class SoundManager : MonoBehaviour{
 
     void OnEnable(){
         PlayerCar.PlayDamageSound += PlayDamage;
     }
 
     void OnDisable(){
         PlayerCar.PlayDamageSound -= PlayDamage;
     }
 
     private void PlayDamage(){}
 }
 
 public class UIManager : MonoBehaviour{
 
     void OnEnable(){
         PlayerCar.ShowDamageText += ShowDamageText;
     }
 
     void OnDisable(){
         PlayerCar.ShowDamageText -= ShowDamageText;
     }
 
 
     private void ShowDamageText(){}
 }

So which is the best approach of calling a function which is in some other class.

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
0

Answer by Paulius-Liekis · Feb 10, 2016 at 10:05 AM

First option is better, because you have less static stuff and there is just a single class that knows about a structure of these relationships.

What you want to build in the end is something that is easy to modify. Statics are not that. They couple your code. Imagine you need to add an extra car (without camera shake) or add an extra effect. Which way is it easier to do?

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 JoshuaMcKenzie · Feb 10, 2016 at 03:02 PM 0
Share

Statics aren't so bad with coupling as to always shun them. in fact the very idea of Singletons violate the idea of coupling, its just that they have benefits that make them worth to be used.

Yes, you generally want decoupled systems so that your projects can scale. however Statics have their own advantages that will sometimes outweigh the desire to go completely decoupled. Also there's a limit to how decoupled you should go. if you wanted to go full decoupled then you wouldn't even be allowed to use most of the Unity Engine namespaces

As for the OP's question I would semi lean to the second option, just not as static events. think of static events as car takes damage. which might make sense for the sound manager, but the camera should only shake for the car its locked to. perhaps he just has a single nonstatic event "OnDamaged" for the camera and Ui to listen for when the player's car takes damage, and then a static event "OnAnyCarDamaged" for the sound manager to attach to, with an internal handler for OnDamaged that will also invoke OnAnyCarDamaged.

$$anonymous$$y favorite way of setting up global events is using the an Event$$anonymous$$anager like what has been shown in the Event Live session tutorial. everything that wants to invoke or listen to a global unity event does so through the Event $$anonymous$$anager. This comes with a number of benefits: - Classes decouple themselves from the other classes (Event$$anonymous$$anager itself becomes tightly coupled, but all the other classes are completely decoupled which is a worthwhile trade). - Neither the Invokers nor the listeners have to be instantiated in the proper order. - Adding new types of events is extremely quick to do by simply assigning a unique string name

and if I want to apply events locked to a specific instance, I can simply wrap a UnityEvent inside an interface (usually as a property) to maintain decoupling

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

37 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to unsubscribe from a delegate that is inside of a singleton? 2 Answers

Singleton GameState broken delegate? 2 Answers

Can I use a delegate/event in Unity3D? 2 Answers

Cleanest way to decouple Manager class with events 1 Answer

C# and JavaScript Event system causing 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