Fix for lazy loding character models

Fenda

Member
Hey @Justin

Another fix we wanted to contribute. We've been implementing lazy loading of character models using Addressables (we're on UCC v3). At first we tried to use the CharacterInitializer which did work but unfortunately in combination with the PUN add-on it lead to some pretty sticky bugs that were hard to fix.

The easiest implementation seems to be that we start with a Dummy model child object in the player prefab (that simply has an AnimatorMonitor and a disabled Animator component) and then when the real character model is loaded via Addressables, it switches the model and fires the OnCharacterSwitchModels event (not using ModelManager).

However, there is an edge case where its possible that the parent player prefab could have been destroyed just before Addressables finishes loading the character model. There isn't any way to prevent these Awake() calls from firing before we can delete the loaded model so we added in these null checks.

Just sharing the diff in case you want to add it too? If you think our approach is relatively clean, I'd be happy to share a short step-by-step on how we achieved lazy loaded character models in case you want to add it to the docs for others to benefit from?

1704758443419.png
 
I haven't tested the Character Initializer with the PUN add-on but I'll do so. I'd like to not have to return early with a null reference check and instead have things initialized in the correct order. If that doesn't work for some reason then a null check makes sense but I'll let you know after I test it.
 
I haven't tested the Character Initializer with the PUN add-on but I'll do so. I'd like to not have to return early with a null reference check and instead have things initialized in the correct order. If that doesn't work for some reason then a null check makes sense but I'll let you know after I test it.
The issue arises specifically when using Addressables (or similar) to lazy load a character model. This is because the player can be destroyed (e.g. disconnect) before the model finishes loading and instantiates. As far as I am aware there is no way to prevent the Awake() calls from happening on the scripts attached to the model in this case.
 
Lazy init maybe? in short ...
C#:
 UCL m_UCL = null;
 UCL GETUCL
 {
     get
     {
         if (!m_UCL)
             m_UCL = get....;
         if (m_UCL)
         {
             if (!init)
                 //in place of Awake()..
                 Init();
             return m_UCL;
         }
         return null;
     }
 }

... something like that just roughly..

Always using GETUCL will then initialize when found, if need be, else do nothing and could be null checked against still.
 
Top