Search Unity

Automatically turning assets into addressables

Discussion in 'Addressables' started by joshcamas, Jan 30, 2019.

  1. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,277
    I would like to convert my game into addressables at some point, however I have hundreds of scenes, prefabs, and scriptable objects. All of these reference each other, so of course changing the references into AssetReferences would break all references, which clearly is not an option.

    Any ideas on how I could transfer to AssetReferences without breaking the original reference? (IE detect a prefab reference, so add that prefab to the addressables and then make that addressable attach to the new AssetReference)
     
  2. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    @joshcamas the easiest way is likely to use AssetDatabase.GetAssetPath and iterate over everything in a subfolder you want to make addressable and a) add it to an addressable group and b) use the AssetPath as key s.t. you can assign the AssetReference. Still would likely be a tedious process with many cases to consider.
     
  3. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,277
    Right! That's exactly what I'd do. However, that is only the first step.
    I didn't really make the title of this thread correct, since I'm wanting to replace all references in scripts to the assets to references to addressables.

    And of course, changing the type of a field in the code will break the serialized reference once everything deserializes after a code compilation

    So I'm wondering if there is some sort of *crazy* way to hook into after code compilation (this new code has fields that are AssetReferences, instead of GameObject), but before deserialization (aka unity detecting the new type doesn't fit the old value, and clearing it), and then going through the fields and fixing things up.

    Orrr if there is some other more reasonable solution, like adding an attribute sort of like "FormerlySerializedAsAttribute", but instead one that allows for a custom callback on deserialization.

    oof. I wish Unity's serialization wasn't a black box
     
  4. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    you can implement
    ISerializationCallbackReceiver
    and transfer the reference manually. you need to keep the original field intact though, otherwise it would be ignored completely and discarded.
    then after you are sure all assets are updated you can remove the old field.

    or you write a script that loops over all assets in the project, loads them with SerializedObject and transfer all references (you still need to have both in your script). you can create a convention on the field names to search for them (e.g. if you have a field named xxx, then you create an AssetReference field named xxx_something and you pair the names. or you put an attribute, e.g.
    [MoveToReference("destinationField")]
    to the source)
    ps: use SerializedObject over reflection to get around corner cases, e.g. arrays/lists or nested structs