0

The following is a wrapper class that will be passed an instance of HEventProperty in order to sync it with HDropdown.

In this case the T in HEventProperty will always be an enum, but we don't know which.

For other wrappers I use derived classes of HEventProperty<> that hard code a specific datatype, and then pass that to the wrapper. But I can't do that in this case because I don't want to hard a separate wrapper for every possible enum definition.

    public class HDropdownWrapper

    {
        private HEventProperty<some_enum> _ep;    
        private HDropdown _hDropdown; //dropdown GI element

        public HDropdownWrapper(HDropdown hDropdown, HEventProperty<some_enum> ep)
        {
            _ep = ep;
            _hDropdown = hDropdown;
        }
    }

Thanks in advance

////////////////////////////////////////////////////

Update

I got it partly working, but I'm still not quite there.

I define the generic EventProperty:

[Serializable]
public class GenericEventProperty<T>
{
    [SerializeField] 
    private T value;

    public GenericEventProperty(T value)
    {
        this.value = value;
    }

    public T Value
    {
        get => value;
        set => this.value = value;
    }
}

I can instantiate these in a monobehaviour and they will show up in the inspector as desired, with my custom enum displaying appropriately:

public enum myEnum
{
    a,
    b,
    c
}
public class InspectorParams : MonoBehaviour
{
    public GenericEventProperty<float> FloatParam;
    public GenericEventProperty<myEnum> EnumParam;
}

However when I come to my wrapper which must be passed an event property, I have trouble:

public class FloatWrapper
{
    private GenericEventProperty<float> ep;
    public FloatWrapper(GenericEventProperty<float> incomiungProperty)
    {
        ep = incomiungProperty;
        Debug.Log(ep.Value);
    }
}
public class EnumWrapper
{
    private GenericEventProperty<Enum> ep;
    public EnumWrapper(GenericEventProperty<Enum> incomingProperty)
    {
        ep = incomingProperty;
        Debug.Log(ep.Value);
    }
}

public enum myEnum
{
    a,
    b,
    c
}
public class InspectorParams : MonoBehaviour
{
    public GenericEventProperty<float> FloatParam;
    public GenericEventProperty<myEnum> EnumParam;
    private void Awake()
    {
        FloatWrapper floatWrapper = new FloatWrapper(FloatParam);
        EnumWrapper enumWrapper = new EnumWrapper(EnumParam); // this line fails
    }
}

FloatWrapper works. However,

EnumWrapper wrapper = new EnumWrapper(EnumParam); yields the error "Argument type 'GenericEventProperty' is not assignable to parameter type 'GenericEventProperty<System.Enum>'"

The error goes away if I create EnumParam with type Enum instead of myEnum, but then I am obviously not using my enum definition and it isn't very useful.

////////////////////////////////////////////////////

Update Update

Got there in the end.

For any future searchers, just needed the generic type on the wrapper class:

public class EnumWrapper<T>
{
    private GenericEventProperty<T> ep;
    public EnumWrapper(GenericEventProperty<T> incomingProperty)
    {
        ep = incomingProperty;
        Debug.Log(ep.Value);
    }
}

and to use:

public class InspectorParams : MonoBehaviour
{
    public GenericEventProperty<myEnum> EnumParam;
    private void Awake()
    {
        EnumWrapper<myEnum> enumWrapper = new EnumWrapper<myEnum>(EnumParam);
    }
}
arcadeperfect
  • 137
  • 10
  • 1
    How about using a generic and or with constraint? Could you show the definition of `HEventProperty`? – Trevor Apr 19 '22 at 13:44
  • 3
    Per @Trevor's suggestion; see: https://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum – JohnLBevan Apr 19 '22 at 13:46
  • If you can't (or don't want to) make `HDropdownWrapper` generic, then you could also use `Enum` directly, like `HEventProperty ep` https://dotnetfiddle.net/afDdVd – MindSwipe Apr 19 '22 at 14:25
  • Sorry for the limited understanding - still learning how generics work. HEventProperty is already generic, constrained to struct. It passed me by that that was preventing me from using Enum as a type – arcadeperfect Apr 19 '22 at 14:30

0 Answers0