简体   繁体   中英

(UNITY / C#) Overridden function not getting called from a derived generic class

I know there's plenty of questions answered about virtual/override functions, how tos, and docs to read up on, but I haven't yet found something that matches what I'm trying to accomplish.

I'm working on a system to create data objects ( specifically for games with RPG elements ) via an Editor Window that I've created in Unity. I had it all working great, just as intended, but then I decided to try to condense all the code from each management panel into a single class using generics.

I'm still fairly new to generics, but from what I've experimented with so far has worked as I hoped. The only issue now is that for some reason when I try to call the Draw method from the derived class, it instead uses the parent classes virtual method. I've never seen this before, and maybe I'm blind and I'm missing something here, or maybe this is an issue specific to the use of generics.

public class DataManagementPanel<T> where T : DataObject {
   public static DataManagementPanel<T> instance;
   public static DataManagementPanel<T> GetInstance() {
      if (instance == null) {
         instance = new DataManagementPanel<T>();
      }
      return instance;
   }

   protected virtual void Draw(Rect window) {
      Debug.Log("Calling Base Class");
   }
}

public ClassManagementPanel() : DataManagementPanel<ClassData>() {
   protected override void Draw(Rect window) {
      Debug.Log("Calling Derived Class");
   }
}

Additionally, I have a DataObject class that holds the base information and a derived class of ClassData to hold data specific to the RPG Class/Job system, and a derived FactionData class that holds data specific to the games faction system.

public class DataObject {
   public int ID;
   public string Name;
}

public class ClassData : DataObject {
   public string IconPath;
}

public class FactionData : DataObject {
   public int Reputation;
}

Finally, I'm trying to draw the management panel on my editor window in the OnGUI() function found in another class. The console output returns "Calling Base Class", instead of the intended "Calling Derived Class".

public class ManagementPanelEditorWindow : EditorWindow {
   void OnGUI() {
      ClassManagementPanel.GetInstance().Draw(position);
   }
}

I found a fix that seems to work for me. Rather than trying to have a static instance within the DataManagementPanel class, made it an abstract class and moved the static instance to each individual panel class.

public abstract DataManagementPanel<T> where T : DataObject {

   public abstract Draw(Rect window);
}

public ClassManagementPanel() : DataManagementPanel<ClassData>() {
   
   private static ClassManagementPanel instance;
   public static ClassManagementPanel GetInstance() {
      if (instance == null) {
         instance = new ClassManagementPanel();
      }
      return instance;
   }       

   public override void Draw(Rect window) {
      Debug.Log("Calling Derived Class");
   }
}

This change results in the desired "Calling Derived Class" output, and more specifically draws my panel on the editor window as intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM