Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Addressables.Instantiate feedback

Discussion in 'Addressables' started by AlkisFortuneFish, Sep 26, 2018.

  1. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    970
    Run into what I can best describe as an API design issue in the system, when migrating to it from an existing project.

    Basically, directly ported instantiation of several prefabs to use addressables, using Addressables.Instantiate<T>(). The majority of call sites were instantiating GameObject references, and they worked fine. There was one that was instantiating via a prefab component reference and was ported to do Addressables.Instantiate<SomeComponent>().

    Of course, AssetBundle.LoadAsset<T>() does not support this pattern since Unity 5.0 and the error that the system was returning was rather unhelpful.

    The fact that Addressables.Instantiate<T>() allows a type argument at all in this case is a bit confusing in itself. Object.Instantiate in general has two main uses off the top of my head:
    1. Cloning of arbitrary UnityEngine.Object instances.
    2. Instantiation of prefabs into the currently active scene.

    The way the API is structured implies to me that it is tailored to the latter use, in which case can't the ambiguity of the original API be removed?
    It could be done by having an explicit non-generic method for prefabs that returns GameObject, a generic method that is constrained to Component and internally does a GetComponent<T>() and a separate method for instantiating ScriptableObject clones and still maintain reference counts.

    Of course, it could all be done by one suite of method with type checks at increased overhead, if that is desired.

    Any thoughts?