Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Custom Editor for generic base class?

Discussion in 'Scripting' started by Noblauch, Apr 27, 2021.

  1. Noblauch

    Noblauch

    Joined:
    May 23, 2017
    Posts:
    258
    Code (CSharp):
    1. [CustomEditor(typeof(BaseFeature<???>), true)]
    2. public class BaseFeatureEditor : UnityEditor.Editor
    3. {
    4.   public override void OnInspectorGUI()
    5.   {
    6.     BaseFeature<???> bf = target as BaseFeature<???>;
    7.     bf.SetDirty();
    8.     base.OnInspectorGUI();
    9.   }
    10. }
    This snippet says it all. How is it possible? I mean, I just want to call a SetDirty() function on those base classes.
     
    ipotonic likes this.
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,161
    I've never tried this but does this work? I doubt it TBH. Alternately you could create a concrete intermediary base type for all generic baseFeature<> and set the custom editor to that perhaps.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class BaseFeature<T> : Object
    5. {
    6. };
    7.  
    8. [CustomEditor(typeof(BaseFeature<>), true)]
    9. public class BaseFeatureEditor : UnityEditor.Editor
    10. {
    11.   public override void OnInspectorGUI()
    12.   {
    13.     EditorUtility.SetDirty(target);
    14.     base.OnInspectorGUI();
    15.   }
    16. }
    17.  
     
  3. Noblauch

    Noblauch

    Joined:
    May 23, 2017
    Posts:
    258
    This actually works:
    [CustomEditor(typeof(BaseFeature<>), true)]


    Problem is, that I can't call my method, since I can't get the target converted to a BaseFeature:
    BaseFeature<???> bf = target as BaseFeature<???>;


    SetDirty() is not a Unity function, but a implementation on our side :p
    Maybe I can figure something out via reflection. Good thing is, that at this point I don't really need the type in this case. If so this would even be worse x)
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,161
    So can you create an abstract base class to BaseFeature<> as I mentioned? If that's the case then you can target a custom editor to that. Obviously you'd need the abstract methods on that type so you can call them. Maybe even specify an interface instead.
     
  5. Noblauch

    Noblauch

    Joined:
    May 23, 2017
    Posts:
    258
    Okay so this setup worked out:
    Code (CSharp):
    1. public interface IFeature
    2. {
    3.   void SetDirty();
    4. }
    5.  
    6. public abstract class BaseFeature<T> : MonoBehaviour, IFeature where T : UnityEngine.Object
    7. {
    8.   private bool _isDirty;
    9.   public void SetDirty() => _isDirty = true;
    10. }
    11.  
    12. [CustomEditor(typeof(BaseFeature<>), true)]
    13. public class BaseFeatureEditor : UnityEditor.Editor
    14. {
    15.   public override void OnInspectorGUI()
    16.   {
    17.     var featureInterface = target as IFeature;
    18.     featureInterface.SetDirty();
    19.     base.OnInspectorGUI();
    20.   }
    21. }
     
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    This actually compiles?
    BaseFeature<>


    I had no idea.
    I'm assuming it just means "any generic type"?
     
  7. Noblauch

    Noblauch

    Joined:
    May 23, 2017
    Posts:
    258
    Yes, I was surprised too, but this really works like that.
     
    xogkdmltaha1 likes this.
  8. Denis-535

    Denis-535

    Joined:
    Jun 26, 2022
    Posts:
    25
    You can use dynamic.

    Code (CSharp):
    1.  
    2.         public override void OnInspectorGUI() {
    3.             OnInspectorGUI( (dynamic) target );
    4.         }
    5.         private void OnInspectorGUI<T>(MyScript<T> target) {
    6.             base.OnInspectorGUI();
    7.         }
    8.  
     
  9. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    741
    I can't seem to get the CreateInspectorGUI() to trigger with this approach. Does it just not work with the new UI Toolkit methods?
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,161
    I don't see what those methods have to do with the above question really which is how to define a custom editor for a generic base-type. If you want to define some kind of generic method in that custom editor then surely the above post answers that question?

    Beyond that, I wouldn't know, sorry.