Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Sun-Pengfei · Mar 15, 2016 at 10:08 AM · scripting problemsingletongenericsderived-class

How to make Unity singleton base class to support generics?

alt text

In my project, I have a class structure as shown in the image.

The green classes are old codes, that runs very well. The classes in red boxes are newly added codes. There're no compiler errors, however when click play in Unity and runs into the new code, the three classes can't be initialized correctly.

And unity console gives warning that says "The class named 'DataMgrBase`2' is generic. Generic MonoBehaviours are not supported! UnityEngine.GameObject:AddComponent()" at this line: "instance = obj.AddComponent ();"

How can I solve this problem?

Following are some code for your reference, thanks!

Implementation of singleton base class:

 using UnityEngine;
 using System.Collections;
 
 public class UnitySingletonPersistent<T> : MonoBehaviour where T : Component
 {
     private static T instance;
 
     public static T Instance {
         get {
             if (instance == null) {
                 instance = FindObjectOfType<T> ();
                 if (instance == null) {
                     GameObject obj = new GameObject ();
                     obj.name = typeof(T).Name;
                     obj.hideFlags = HideFlags.DontSave;
                     instance = obj.AddComponent<T> ();
                 }
             }
             return instance;
         }
     }
 
     public virtual void Awake ()
     {
         DontDestroyOnLoad (this.gameObject);
         if (instance == null) {
             instance = this as T;
         } else {
             Destroy (gameObject);
         }
     }
 }


Implementation of DataMgrBase:

 public class DataMgrBase<TKey, TValue>: UnitySingletonPersistent<DataMgrBase<TKey, TValue>> {
 
     protected Dictionary<TKey, TValue> dataDict;
 
     public override void Awake()
     {
         base.Awake();
 
         dataDict = new Dictionary<TKey, TValue>();
     }
 
     public TValue GetDataForKey(TKey key)
     {
         TValue data;
         if (dataDict.TryGetValue(key, out data))
         {
             return data;
         }
         else
         {
             data = LoadDataForKey(key);
             if (data != null)
             {
                 dataDict.Add(key, data);
             }
 
             return data;
         }
     }
 
     virtual protected TValue LoadDataForKey(TKey key)
     {
         if (dataDict.ContainsKey(key))
         {
             return GetDataForKey(key);
         }
         else
         {
             return default(TValue);
         }
     }    
 }
bugimage.png (62.6 kB)
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
2
Best Answer

Answer by Sun-Pengfei · Mar 15, 2016 at 01:48 PM

I've solved it by myself as following:

Change of the base class to get a new generic type(of the class that will derive from it, and pass this type to singleton base class)

 public class DataMgrBase<TKey, TValue, TClass>: UnitySingletonPersistent<TClass> where TClass: Component

For all the other three classes that want to derive from it, change them as following form:

 public class MobSettingDataMgr : DataMgrBase<int, MobSettingData, MobSettingDataMgr>
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

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

Related Questions

Singleton Problem 0 Answers

error CS0176: Static member `GameManagerSingleton.score' cannot be accessed with an instance reference, qualify it with a type name instead 1 Answer

Singleton HELP 1 Answer

How can I handle scripts correctly? What's a good way to apply scripts to GameObjects? 1 Answer

Get reference to dynamically added script 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