LocalizeTarget_UnityUI_Image.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using UnityEditor;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace I2.Loc
  5. {
  6. #if UNITY_EDITOR
  7. [InitializeOnLoad]
  8. #endif
  9. public class LocalizeTarget_UnityUI_Image : LocalizeTarget<Image>
  10. {
  11. static LocalizeTarget_UnityUI_Image() { AutoRegister(); }
  12. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] static void AutoRegister() { LocalizationManager.RegisterTarget(new LocalizeTargetDesc_Type<Image, LocalizeTarget_UnityUI_Image> { Name = "Image", Priority = 100 }); }
  13. public override bool CanUseSecondaryTerm () { return false; }
  14. public override bool AllowMainTermToBeRTL () { return false; }
  15. public override bool AllowSecondTermToBeRTL () { return false; }
  16. public override eTermType GetPrimaryTermType(Localize cmp)
  17. {
  18. return mTarget.sprite == null ? eTermType.Texture : eTermType.Sprite;
  19. }
  20. public override eTermType GetSecondaryTermType(Localize cmp) { return eTermType.Text; }
  21. public override void GetFinalTerms ( Localize cmp, string Main, string Secondary, out string primaryTerm, out string secondaryTerm )
  22. {
  23. primaryTerm = mTarget.mainTexture ? mTarget.mainTexture.name : "";
  24. if (mTarget.sprite!=null && mTarget.sprite.name!=primaryTerm)
  25. primaryTerm += "." + mTarget.sprite.name;
  26. secondaryTerm = null;
  27. }
  28. public override void DoLocalize ( Localize cmp, string mainTranslation, string secondaryTranslation )
  29. {
  30. Sprite Old = mTarget.sprite;
  31. if (Old==null || Old.name!=mainTranslation)
  32. mTarget.sprite = cmp.FindTranslatedObject<Sprite>( mainTranslation );
  33. // If the old value is not in the translatedObjects, then unload it as it most likely was loaded from Resources
  34. //if (!HasTranslatedObject(Old))
  35. // Resources.UnloadAsset(Old);
  36. // In the editor, sometimes unity "forgets" to show the changes
  37. #if UNITY_EDITOR
  38. if (!Application.isPlaying)
  39. EditorUtility.SetDirty( mTarget );
  40. #endif
  41. }
  42. }
  43. }