UILocalize.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. [RequireComponent(typeof(UIWidget))]
  5. [AddComponentMenu("NGUI/UI/Localize")]
  6. public class UILocalize : MonoBehaviour
  7. {
  8. public string value
  9. {
  10. set
  11. {
  12. if (!string.IsNullOrEmpty(value))
  13. {
  14. UIWidget component = base.GetComponent<UIWidget>();
  15. UILabel uilabel = component as UILabel;
  16. UISprite uisprite = component as UISprite;
  17. if (uilabel != null)
  18. {
  19. UIInput uiinput = NGUITools.FindInParents<UIInput>(uilabel.gameObject);
  20. if (uiinput != null && uiinput.label == uilabel)
  21. {
  22. uiinput.defaultText = value;
  23. }
  24. else
  25. {
  26. uilabel.text = value;
  27. }
  28. }
  29. else if (uisprite != null)
  30. {
  31. UIButton uibutton = NGUITools.FindInParents<UIButton>(uisprite.gameObject);
  32. if (uibutton != null && uibutton.tweenTarget == uisprite.gameObject)
  33. {
  34. uibutton.normalSprite = value;
  35. }
  36. uisprite.spriteName = value;
  37. uisprite.MakePixelPerfect();
  38. }
  39. }
  40. }
  41. }
  42. private void OnEnable()
  43. {
  44. if (this.mStarted)
  45. {
  46. this.OnLocalize();
  47. }
  48. }
  49. private void Start()
  50. {
  51. this.mStarted = true;
  52. this.OnLocalize();
  53. }
  54. private void OnLocalize()
  55. {
  56. if (string.IsNullOrEmpty(this.key))
  57. {
  58. UILabel component = base.GetComponent<UILabel>();
  59. if (component != null)
  60. {
  61. this.key = component.text;
  62. }
  63. }
  64. if (!string.IsNullOrEmpty(this.key))
  65. {
  66. this.value = Localization.Get(this.key);
  67. }
  68. }
  69. public string key;
  70. private bool mStarted;
  71. }