ContextHolder contextType is a string reference instead of an asset reference

Issue #110 wontfix
John O'Connor created an issue

DataBind code uses a string to represent the Context class reference in the asset. It then uses reflection to search for the Type instance.

This makes it impossible to rename a context’s class, namespace, or assembly as the reflection will break. The assets will still have hard coded references to the old name.

Instead, the asset reference should use a standard Script reference. Since this leverages the GUID of the script file it would allow the rename while maintaining asset references

foo.prefab

  contextType: UI.Combat.PortraitContext, My.Special.Assembly, Version=0.0.0.0, Culture=neutral,

Comments (6)

  1. Christian Oeing repo owner

    Hi John!

    Thanks for your report.

    I will have a look at this issue. The reason for using a string is probably that the data context class is not always a Unity script, but may be a normal C# class in an assembly. In that case there is no GUID we can use.

    But I can see if I find a way to store a script reference if it is a script and only use a string reference for non-script classes.

  2. Christian Oeing repo owner

    Mmh, I had a look and found some libraries which serialize a type via its GUID if available. But there does not seem to be a built-in option to do so. So given that the only benefit is to not have to select the context types again when the namespace or name of the context type changed (which shouldn’t happen very often), it is not worth the effort to implement and test a solution there.

    Sorry about that! If you have some information about an easy way to serialize/deserialize a GUID for a given type, let me know and I might reopen this issue.

  3. John O'Connor reporter

    the data context class is not always a Unity script, but may be a normal C# class in an assembly. In that case there is no GUID we can use.

    Even if it’s just a C# class, but still directly in unity, the file will have a .meta associated with it and therefore have a GUID. Or are you saying the script may be from an external DLL?

    I had a look and found some libraries which serialize a type via its GUID if available

    Are you referring to using this API? https://docs.unity3d.com/ScriptReference/AssetDatabase.TryGetGUIDAndLocalFileIdentifier.html

    or maybe AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath?

  4. Christian Oeing repo owner

    Hi @John O'Connor ,

    I was referencing to such packages like this which can use the GUID of a script instead of the type name, but even they are not able to work with types e.g. from DLLs: https://github.com/mackysoft/Unity-SerializeReferenceExtensions#if-the-type-is-renamed-the-reference-is-lost

    Here you can see the logic which would be necessary: https://github.com/SolidAlloy/ClassTypeReference-for-Unity/blob/master/Runtime/TypeReference.cs Plus it would require a way to keep the current contextType field to be backwards compatible.

    So there might be a way, but as I said I don’t see enough value here for the work yet. This may change if Unity gets a built-in way to serialize types.

  5. Log in to comment