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 MaT227 · Aug 01, 2013 at 01:34 PM · c#performanceoptimization

Optimization Question about Singleton

Hi,

I have a GameObject with a script with a Singleton pattern. And I need to call a function in an other object.

But what's the best thing to do :

  • Call for :

MyScript.Instance.MyFunc();

  • Store my Singleton's GameObject in my calling object and call it like this :

myScript.MyFunc();

What is the best thing to do in term of performance and optimizations.

Thanks a lot.

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

2 Replies

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

Answer by FuzzyLogic · Aug 01, 2013 at 07:33 PM

There is no need to store the instance variable locally. It's already stored in the singleton, so creating a local reference to the instance is just redundant, unless you intend to be able to dynamically swap to a different singleton that implements the same interface. (See abstract factory pattern)

There is two main ways:

1. use an Instance method or readonly property, like you described.

eg. MyScript.Instance.MyFunc()

2. create static wrapper fuctions in your singleton class, which call your instanced functions.

eg. MyScript.MyFunc() (note 'MyScript', not 'myScript')

Here is a pseudo example of a wrapper function in case I'm not explaining clearly:

 public static void MyScript.MyFunc() {
     MyScript.Instance.MyFunc();
 }

I prefer #2 but using Instance makes your code self-documenting, which is never a bad thing. Using Instance makes it clear to anyone reading your code that you are making a call to a singleton. Using #2, its unclear of it's a static class or a singleton.

Depending on how your singleton is used, you may prefer one method over the other. If you create your singleton once and never destroy the instance until the application terminates, then #2 is a good choice. This is normally how singletons are used in Unity, as some sort of object manager. It's not important if it's static or singleton in this case.

Hope that helps.

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 Jamora · Aug 01, 2013 at 08:10 PM 0
Share

Edited to add the link to abstract factory pattern from wikipedia.

avatar image MaT227 · Aug 02, 2013 at 07:31 AM 0
Share

Thanks for your answer !

avatar image
1

Answer by Fattie · Aug 01, 2013 at 01:42 PM

In fact here's the "really long and final" discussion on the simple, reliable and now inevitable "grid" system...

http://answers.unity3d.com/questions/551297/how-can-i-instantiate-a-prefab-from-a-static-funct.html

cheers!


An endless question in Unity development is how to handle singletons and singleton-like structures, the efficiency thereof, and the fact that coroutines and arguably the whole general nature of Unity just, in many ways, hates singletons. It's just so unportable, so many assumptions, etc - we just got in to a big mixup with a code base working with another dev where singletons didn't fit together etc.

I only like things that are extremely simple, over on this question,

http://answers.unity3d.com/questions/17916/singletons-with-coroutines.html

I outline something we do in every project now which is little more than a linguistic trick. But it solves ALL problems in a lovely way. The only downside is, essentially, you have to add one extra "." in those calls. But it's just so tidy. I like it more and more.

Here is the whole answer:


Purely for convenience:

In almost all projects: it will be the case that you will have an empty game object attached on your opening scene, for clarity let's call it "holdAll".

"holdAll" is persistent throughout the game.

Attached to that, you will have many files such as Shopping.js, Analytics.js, Calculations.js .. etc etc

Each of those uses coroutines. So they really are best as "normal" scripts attached to an game object.

Again, it's inevitable that almost every Unity game has an empty game object ("holdAll") which is persistent throughout the whole game, and you use those various

I've never seen a Unity project that does not have that.

So, this is all great and normal. But, each time you want to use a routine from shopping.js, you need to make a local variable which, annoyingly, you have to hook to shopping.js on holdAll.

Of course, it's much easier to just call ordinary static libraries, like when you use Mathf or the like.

Is there a solution?

Yes, here's what I do. Make a tiny little class called say "grid":

 class grid
     {
     static var shop:Shopping;
     static var etc:Etc;
     static var etc:Etc;
  
     private static function grid()
        {
        shop = GameObject.Find("holdAll").GetComponent(Shopping);
        etc = GameObject.Find("holdAll").GetComponent(Etc);
        etc = GameObject.Find("holdAll").GetComponent(Etc);
        }
         // nb, grid() is indeed magically called (once only)
         // when one of these vars is first accessed - 
         // you need do nothing, just use the vars at will.
     }


then from anywhere at all in your project,

with no further effort,

you can just write

 grid.shop.buy(..);
 grid.shop.sell(..);
 yield grid.shop.showDialog(...);
 yield grid.shop.interactionUser(...);
 x = grid.shop.count;

and so on. Same for all the others (Shopping.js, Analytics.js, Calculations.js etc etc)

I want the convenience of just being able to say

yield shop.whatever();

anywhere in the project WITHOUT needing an annoying hooked-up local variable "shopit" which leads to holdAll-Shopping.js.

But in my opinion there's just no really good, bulletproof, way to make a static/singleton/etc, where it's a "normal" coroutine-based Unity object.

So ... for me, this seems to answer all those needs. The only downside is -- effectively -- one extra "." when you type grid.shopping.

Seems to work ok ?

Comment
Add comment · 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

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

18 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

Related Questions

How do I efficiently maintain a constantly-changing list without triggering GC? 2 Answers

Is there a more efficient way to write a "Find" script? 2 Answers

need help getting more performance out of this thing 1 Answer

Mesh.CombineMeshes does not work when mergeSubMeshes=false 2 Answers

Speed and efficiency of enabling/disabling component? 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