LocalizeTarget_UnityStandard_Prefab.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using UnityEngine;
  3. namespace I2.Loc
  4. {
  5. public class LocalizeTarget_UnityStandard_Prefab : LocalizeTarget<GameObject>
  6. {
  7. static LocalizeTarget_UnityStandard_Prefab()
  8. {
  9. LocalizeTarget_UnityStandard_Prefab.AutoRegister();
  10. }
  11. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  12. private static void AutoRegister()
  13. {
  14. LocalizationManager.RegisterTarget(new LocalizeTargetDesc_Prefab
  15. {
  16. Name = "Prefab",
  17. Priority = 250
  18. });
  19. }
  20. public override bool IsValid(Localize cmp)
  21. {
  22. return true;
  23. }
  24. public override eTermType GetPrimaryTermType(Localize cmp)
  25. {
  26. return eTermType.GameObject;
  27. }
  28. public override eTermType GetSecondaryTermType(Localize cmp)
  29. {
  30. return eTermType.Text;
  31. }
  32. public override bool CanUseSecondaryTerm()
  33. {
  34. return false;
  35. }
  36. public override bool AllowMainTermToBeRTL()
  37. {
  38. return false;
  39. }
  40. public override bool AllowSecondTermToBeRTL()
  41. {
  42. return false;
  43. }
  44. public override void GetFinalTerms(Localize cmp, string Main, string Secondary, out string primaryTerm, out string secondaryTerm)
  45. {
  46. primaryTerm = cmp.name;
  47. secondaryTerm = null;
  48. }
  49. public override void DoLocalize(Localize cmp, string mainTranslation, string secondaryTranslation)
  50. {
  51. if (string.IsNullOrEmpty(mainTranslation))
  52. {
  53. return;
  54. }
  55. if (this.mTarget && this.mTarget.name == mainTranslation)
  56. {
  57. return;
  58. }
  59. Transform transform = cmp.transform;
  60. string text = mainTranslation;
  61. int num = mainTranslation.LastIndexOfAny(LanguageSource.CategorySeparators);
  62. if (num >= 0)
  63. {
  64. text = text.Substring(num + 1);
  65. }
  66. Transform transform2 = this.InstantiateNewPrefab(cmp, mainTranslation);
  67. if (transform2 == null)
  68. {
  69. return;
  70. }
  71. transform2.name = text;
  72. for (int i = transform.childCount - 1; i >= 0; i--)
  73. {
  74. Transform child = transform.GetChild(i);
  75. if (child != transform2)
  76. {
  77. UnityEngine.Object.Destroy(child.gameObject);
  78. }
  79. }
  80. }
  81. private Transform InstantiateNewPrefab(Localize cmp, string mainTranslation)
  82. {
  83. GameObject gameObject = cmp.FindTranslatedObject<GameObject>(mainTranslation);
  84. if (gameObject == null)
  85. {
  86. return null;
  87. }
  88. GameObject mTarget = this.mTarget;
  89. this.mTarget = UnityEngine.Object.Instantiate<GameObject>(gameObject);
  90. if (this.mTarget == null)
  91. {
  92. return null;
  93. }
  94. Transform transform = cmp.transform;
  95. Transform transform2 = this.mTarget.transform;
  96. transform2.SetParent(transform);
  97. Transform transform3 = (!mTarget) ? transform : mTarget.transform;
  98. transform2.rotation = transform3.rotation;
  99. transform2.position = transform3.position;
  100. return transform2;
  101. }
  102. }
  103. }