ScriptableObjectCollection icon indicating copy to clipboard operation
ScriptableObjectCollection copied to clipboard

CollectionItem with Generic Inheritance (Unity Atoms)

Open mgmhunt opened this issue 1 year ago • 2 comments

Hi thanks for this library - trying to use it organise Unity Atoms - starting with Events but probably move on to Variables, Actions, Conditions etc.

I have classes derived from your library with Generics

public class AtomEventCollection<T> : ScriptableObjectCollection<AtomEventItem<T>>    
{    }
public abstract class AtomEventItem<T> : ScriptableObjectCollectionItem
    {
        [SerializeField]
        private AtomEvent<T> _event;
        public AtomEvent<T> Event
        {
            get { return _event; }
            set { _event = value; }
        }

.... this also automatically generates the AtomEvent<T> on creation and nests it under the CollectionItem
public class AtomEventIndirectReference<T> : CollectionItemIndirectReference<AtomEventItem<T>>    {
        public AtomEventIndirectReference() {}
        public AtomEventIndirectReference(AtomEventItem<T> collectionItemScriptableObject) : base(collectionItemScriptableObject) {}
    }

which then get made concrete like

public partial class WorldGameEvent : AtomEventItem<World> { }

public class WorldGameEventCollection : AtomEventCollection<WorldGameEvent> { }

As far as I can tell at this stage this is working.

In order to get this working I've had to make the following edit on line 226 of ScriptableObjectCollection.cs ` // DOESN'T WORK -> if (baseType.IsGenericType && baseType.GetGenericTypeDefinition() == typeof(ScriptableObjectCollection<>))

if (baseType.IsGenericType) // edit mgmhunt`

as it's presuming the Generics are only the ScriptableObjectCollection<>. I couldn't get IsSubClass of (probably to do with Generics again - maybe you've got a cleaner way of fixing this?)

My question is do you foresee issues down the road with this as I'm hoping this library will be broadly useful and not sure what this assumption my cause with other classes?

----- Working so far ----

WorldGameEvent testWorldEvent;
testWorldEvent.Event.Observe().TakeUntilDisable(this).Subscribe(_ => { LoadWorld(defaultWorld); }).AddTo(_subscriptions);
testWorldEvent.Event.Raise();

Screen Shot 2024-05-18 at 09 43 49 Screen Shot 2024-05-18 at 09 46 58

mgmhunt avatar May 17 '24 21:05 mgmhunt

Yeah I think this should be fine, I will update to match your changes and follow close to the next release to see if cause any issues

brunomikoski avatar May 18 '24 15:05 brunomikoski

Cool yea haven't come across anything so far but it'll be a ways down the track before the number and type of Collections builds up so needed to test the waters a bit.

mgmhunt avatar May 20 '24 20:05 mgmhunt