Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

LocalizedString.GetLocalizedString throws an error if no ChangeHandler is registered

Discussion in 'Localisation Tools Previews' started by james7132, Nov 7, 2019.

  1. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    131
    Using
    LocalizedString.GetLocalizedString
    without setting a ChangeHandler results in the following error. It's not clear if this is intended behavior.

    Code (csharp):
    1. Exception: RefreshString should be used with RegisterChangeHandler however no change handler has been registered.
    2. UnityEngine.Localization.LocalizedString.RefreshString () (at Library/PackageCache/com.unity.localization@0.5.0-preview/Runtime/Localized Reference/LocalizedString.cs:81)
    3. UnityEngine.Localization.LocalizedString.AutomaticLoadingCompleted (UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle`1[TObject] loadOperation) (at Library/PackageCache/com.unity.localization@0.5.0-preview/Runtime/Localized Reference/LocalizedString.cs:162)
    4. DelegateList`1[T].Invoke (T res) (at Library/PackageCache/com.unity.addressables@1.3.3/Runtime/ResourceManager/Util/DelegateList.cs:69)
    5. UnityEngine.Debug:LogException(Exception)
    6. DelegateList`1:Invoke(AsyncOperationHandle`1) (at Library/PackageCache/com.unity.addressables@1.3.3/Runtime/ResourceManager/Util/DelegateList.cs:73)
    7. UnityEngine.ResourceManagement.ChainOperation`2:OnWrappedCompleted(AsyncOperationHandle`1)
    8. DelegateList`1:Invoke(AsyncOperationHandle`1) (at Library/PackageCache/com.unity.addressables@1.3.3/Runtime/ResourceManager/Util/DelegateList.cs:69)
    9. UnityEngine.ResourceManagement.ResourceManager:Update(Single)
    10. MonoBehaviourCallbackHooks:Update() (at Library/PackageCache/com.unity.addressables@1.3.3/Runtime/ResourceManager/Util/MonoBehaviourCallbackHooks.cs:19)
    11.  
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    3,550
    Yes that's intentional. Refresh string is not needed if you call get localized string as it generates the string each time,(if using formatting).
    I'll try and make the error message clearer.
     
  3. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    131
    It seems like GetLocalizedString is calling in it's RefreshString in it's callback, and calling GetLocalizedString without first initializing it with a ChangeHandler causes it to fail.

    So the expected flow is to always provide a ChangHandle before calling GetLocalizedString?
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    3,550
    Thats really strange. Are you registering a null change handler?
    If you just want to use GetLocalizedString then you do not need to use anything to do with ChangeHandlers or RefreshString.

    I have added a null check in RegisterChangeHandler.
     
    Last edited: Nov 7, 2019
  5. james7132

    james7132

    Joined:
    Mar 6, 2015
    Posts:
    131
    Discovered the issue, had nothing to do with GetLocalizedString at all. I am using LocalizeStringBehaviour on my main menu. By default it loads the entire scene, which has all of the menu GameObjects active, then deactivates all of them but the active menu. What this functionally results in is a OnEnable call, registering the handler (including the AutomaticLoadingCompleted callback), then instantly disables them, clearing the change handler. This launches the loading operation, but then removes the change handler for it, and AutomaticLoadingCompleted is still subscribed, which results in it calling a RefreshString without that handler.
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    3,550
    Makes sense. We just need to cancel any pending loading operations in ClearChangeHandler. Thanks for spotting this.