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
3
Question by nuverian · Apr 03, 2013 at 06:39 AM · javascriptinheritancesingletongenerics

Extending a Singleton base class (UJS) (?)

So I have a lot of singletons in my game as it seems and want to make a base class for all of them to derive from. Now, is that even possible in unity javascript since it doesn't support generics(?). The C# way should be:

 class MySingleton : MonoSingleton<MySingleton>{
 
 }

is something like this possible or some other alternative?

Thanks

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 MorphingDragon · Apr 03, 2013 at 07:00 AM 0
Share

singletons should be used sparingly. Is there a reason why you need so many.

avatar image nuverian · Apr 03, 2013 at 07:46 AM 0
Share

many = 5 btw. I prefer to have a singleton for a unique persistant object that performs a job, rather than:

A) having a singleton tha does all jobs B) having a singleton than has reference to the other objects that perform a single job

Even with 5 or 6, I would like to go the good way and use inheritance rather than copy pasting code

avatar image MorphingDragon · Apr 03, 2013 at 07:55 AM 0
Share

Wouldn't it be better then to use Unity's builtin messaging dispatch? Or have a game object than can be referenced by a tag?

avatar image nuverian · Apr 03, 2013 at 08:08 AM 0
Share

I've build my own messaging/event system. Unity's didn't cut it. I dont want to use tags because then that will either mean that I have a reference of the "manager" into the class that uses it, or have to search for it every time the class needs it which is not good. heh it became a discussion about if singletons are good or not, rather than a Q&A :P

avatar image MorphingDragon · Apr 03, 2013 at 09:07 AM 0
Share

Well my questions are related to your problem, I just don't think you're using Inheritance or Singletons properly.

I guess what I really should've asked is: Do these objects really need to be shared across the entire program or are they specific to a Scene? If they do need to be shared across the entire program then the Singleton pattern is a good option. If they're scene specific then consider using a game object. Dragging relationships in the editor may be annoying, but it really does help with larger projects by making bugs easier to trace.

Do all these singletons need to inherit from a single parent? Do they share state or behavior in any way, or do you really want to define some common interface to all of these singletons? If the latter you should look up something called "Duck Typing", its much better suited to Javascript. In both cases actually, Duck Typing may be better.

Show more comments

2 Replies

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

Answer by whydoidoit · Apr 04, 2013 at 10:58 AM

UnityScript (Javascript in Unity) is not Javascript and absolutely does have classes!

However UnityScript cannot define a generic class - it can however inherit from one...

If you defined your base MonoSingleton class in C# in a Plugins folder then you could use:

   class TestInherit extends MonoSingleton.<TestInherit>
   {
   }


The base class would look like this:

 using System.Collections;
 
 public class MonoSingleton<T> : MonoBehaviour where T: MonoSingleton<T> { 
     
     public static T instance;
     
     void Awake()
     {
         instance = this as T;
         AwakeEx();    
     }
     
     protected virtual void AwakeEx()
     {
     }
     
     protected virtual void OnDestroyEx()
     {
     }
     
     void OnDestroy()
     {
         OnDestroyEx();
         if(instance == this) instance = null;
     }
 }

Note to avoid problems you should use AwakeEx and OnDestroyEx in the classes derived from MonoSingleton - they work just the same way but ensure the singleton is handled properly

While I'm at it, here's a base class that creates a static collection of all the enabled derivatives:

 using System.Collections.Generic;
 using Unity.Engine;
 
 public class MonoCollection<T> : MonoBehaviour where T : MonoCollection<T>
 {
     public static List<T> all = new List<T>();
     
     protected virtual void OnEnableEx()
     {
     }
     
     protected virtual void OnDisableEx()
     {
     }
     
     void OnEnable()
     {
         all.Add(this as T);
         OnEnableEx();
     }
     
     void OnDisable()
     {
         all.Remove(this as T);
         OnDisableEx();
     }
 }
Comment
Add comment · Show 4 · 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 Fattie · Apr 04, 2013 at 11:00 AM 0
Share

i elevated that to an answer because (of course) it rocks. it's so obvious once you say it - put the base in c#

avatar image Fattie · Apr 04, 2013 at 11:04 AM 0
Share

Since you have to do everything else around here I was thinking perhaps you could paste in an awesome example of the c# sgtn base, for programmers who are more JS oriented.

(Err not me of course, I'm thinking of a friend who it could help...)

avatar image whydoidoit · Apr 04, 2013 at 11:18 AM 0
Share

Sure that would look something like this:

 using System.Collections;
 
 public class $$anonymous$$onoSingleton<T> : $$anonymous$$onoBehaviour where T: $$anonymous$$onoSingleton<T> { 
     
     public static T instance;
     
     void Awake()
     {
         instance = this as T;
         AwakeEx();    
     }
     
     protected virtual void AwakeEx()
     {
     }
     
     protected virtual void OnDestroyEx()
     {
     }
     
     void OnDestroy()
     {
         OnDestroyEx();
         if(instance == this) instance = null;
     }
 }

Note to avoid problems you should usine AwakeEx and OnDestroyEx in the classes derived from $$anonymous$$onoSingleton - they work just the same way but ensure the singleton is handled properly

While I'm at it, here's a base class that creates a static collection of all the enabled derivatives:

 using System.Collections.Generic;
 using Unity.Engine;
 
 public class $$anonymous$$onoCollection<T> : $$anonymous$$onoBehaviour where T : $$anonymous$$onoCollection<T>
 {
     public static List<T> all = new List<T>();
     
     protected virtual void OnEnableEx()
     {
     }
     
     protected virtual void OnDisableEx()
     {
     }
     
     void OnEnable()
     {
         all.Add(this as T);
         OnEnableEx();
     }
     
     void OnDisable()
     {
         all.Remove(this as T);
         OnDisableEx();
     }
 }
avatar image nuverian · Apr 04, 2013 at 04:34 PM 0
Share

So in a nutshell, UJS doesnt support declaring generics, does support inheriting generics. Thanks. I will use my own singleton though :P but thanks for the code to make clear on the generics

avatar image
0

Answer by MorphingDragon · Apr 03, 2013 at 09:11 AM

See my comments above, but this website should have explanations on the type of inheritance and interfaces in Javascript. But the simple answer is Javascript doesn't have classes at all technically, but you can program it like it does. A more Javascript friendly way would be to use something called Duck Typing.

[Edit: whydoidoit the following link is relevant to real Javascript and not the Javascript which is present in Unity - Javascript in Unity is a .NET language more like ActionScript than it is like real Javascript. Many real Javascript techniques do not apply or are flawed in their by their implementation in Unity. Not to say UnityScript isn't good - it just isn't Javascript]

http://www.crockford.com/javascript/inheritance.html http://stackoverflow.com/questions/12762550/example-of-javascript-duck-typing/12763070#12763070

Comment
Add comment · Show 9 · 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 nuverian · Apr 03, 2013 at 09:30 AM 0
Share

Yes, as stated in my second comment ("..unique persistant object..") they need to be along the whole program. They do share a behaviour and that is of being a singleton and I hate copy pasting code. By the way UNITYscript, does have inheritance all fine, interfaces and all. Thanks for answer and your time :)

avatar image nuverian · Apr 03, 2013 at 09:31 AM 0
Share

Oh by the way I will look into Duck Typing, although I think that doesnt answer if UnityScript support Generics :)

avatar image whydoidoit · Apr 04, 2013 at 10:59 AM 0
Share

Also Duck typing is a very bad idea in UnityScript/Javascript (in Unity).

avatar image MorphingDragon · Apr 04, 2013 at 11:26 AM 0
Share

Generally speaking it's actually a bad solution in most cases in a lot of languages; A result of trying to get the language to do something it wasn't designed for. It is there if needed, it's a tool like any other program$$anonymous$$g technique or design pattern.

avatar image whydoidoit · Apr 04, 2013 at 11:28 AM 0
Share

Yeah agreed - I just wish they hadn't called UnityScript damn Javascript - screwed me up as a JS programmer when I first started and I know @Fattie and I have discussed the differences at length.

Show more comments

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

13 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

Related Questions

MonoBehaviour Inheritance, Singletons and Generics 1 Answer

C# Generics not constraining correctly? 1 Answer

Calling coroutines through pointers/"Function" class? 2 Answers

Using functions of Inherited classes 3 Answers

C# Inheriting from MonoBehaviour but still be able to instantiate with new()? How should I do this? 2 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