3
\$\begingroup\$

For example, if we have a class that is for handling GooglePlay. And instead of creating an instance of it in every class where you want to use it, you have:

public class GooglePlayHandler {

    public static GooglePlayHandler instance;

    private void Awake(){
        instance = this;
    }
}

If I then access this in say 5 of my scripts in Unity. Would it be the same, better or worse than having a separate GooglePlayHandler inside each class?

And furthermore if I continue using this style in my game with lots more classes and features would that badly affect the games frame rate. (For example if I did this with my inventory code, cash/points, total time taken, etc etc.

\$\endgroup\$
  • 1
    \$\begingroup\$ Almost certainly better to use a single instance like this. \$\endgroup\$ – Draco18s Jul 28 '17 at 18:02
  • \$\begingroup\$ wow nice! I've only just learned how to do this properly, but it seems like it will make my work much easier too :) double-bonus !! \$\endgroup\$ – Big T Larrity Jul 28 '17 at 18:04
  • 1
    \$\begingroup\$ You have bad framerate because you don't know how to optimize your draw calls yet. \$\endgroup\$ – Sidar Jul 28 '17 at 18:18
  • \$\begingroup\$ @Sidar you are almost certainly correct. It just stutters very slightly, for about 1-2 secs at start of the race. Where i have lots of 3d models and also the starting code is running. It of course only slows down on my Android, not via PC. I reduced the shadows down massively and now it is smooth. But I was sure there was some way to make it run with shadows, since I have seen GTA san andreas running smoothly on Android so im sure this little race game can too \$\endgroup\$ – Big T Larrity Jul 28 '17 at 22:49
  • 1
    \$\begingroup\$ You should async your levels then. The problem with that is that you're loading too much at once and android devices are nowhere near the speed pc's are. You can have a loading screen present in every scene (with DontDestroyOnload()) that's disabled and only shows when you're async loading and turn it off when everything is loaded. \$\endgroup\$ – Sidar Jul 29 '17 at 5:21
6
\$\begingroup\$

No. This is a standard practice called a singleton (you can see an example in Unity's tutorials with a game manager).

You likely won't see a performance difference between this and 5 instances. But you will have a shared instance instead of multiple, meaning any data created in one would be available to the others.

In the long run, worry about completing the game first, and performance second. Minor performance alterations like this can be performed after the game is complete as a refactoring effort with much better knowledge of the implications and the ability to test changes to see their effects on overall performance.

\$\endgroup\$
  • \$\begingroup\$ thanks that's just the helpful info i needed. Now I understand better when to and not to use this. I have almost finished the game tbh and there are some slight frame drops , mainly at the very start where lots is happening \$\endgroup\$ – Big T Larrity Jul 28 '17 at 18:05
  • 2
    \$\begingroup\$ Note: the performance between 1 and 5 instances won't be noticeable because it is so small. If you had hundreds or thousands then you would start to see the impact. \$\endgroup\$ – Draco18s Jul 28 '17 at 18:07
2
\$\begingroup\$

Ah Singletons - the best pattern. The worst pattern.

So, there should be no performance difference in either case. However, it's likely that with a Handler class (or a Manager class or similar) you'd want a singleton so there's only a single instance managing those data, especially if it's communication with a backend or the like.

http://www.labyrintheer.com/2017/07/22/random-class-singletons-and-the-big-debate-does-this-sound-familiar/

I recently wrote on how I'm using some singletons. There's also a link to a podcast that has some good info on singletons. They are often frowned upon because people tend to misuse them and because it's easy for them to get out of hand. Used correctly, though, the singleton can be a very simple and elegant solution to many issues.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can always go the service locator pattern way if you want to tuck away the singleton pattern. \$\endgroup\$ – Sidar Jul 28 '17 at 18:16
  • 2
    \$\begingroup\$ Singletons are just another tool. Don't treat everything like a nail. ;) \$\endgroup\$ – Draco18s Jul 28 '17 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.