LanguageSource.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace I2.Loc
  5. {
  6. [AddComponentMenu("I2/Localization/Source")]
  7. [ExecuteInEditMode]
  8. public class LanguageSource : MonoBehaviour, ISerializationCallbackReceiver, ILanguageSource
  9. {
  10. public LanguageSourceData SourceData
  11. {
  12. get { return mSource; }
  13. set { mSource = value; }
  14. }
  15. public LanguageSourceData mSource = new LanguageSourceData();
  16. // Because of Unity2018.3 change in Prefabs, now all the source variables are moved into LanguageSourceData
  17. // But to avoid loosing previously serialized data, these vars are copied into mSource.XXXX when deserializing)
  18. // These are going to be removed once everyone port their projects to the new I2L version.
  19. #region Legacy Variables
  20. // TODO: also copy public string name; and owner
  21. public int version;
  22. public bool NeverDestroy; // Keep between scenes (will call DontDestroyOnLoad )
  23. public bool UserAgreesToHaveItOnTheScene;
  24. public bool UserAgreesToHaveItInsideThePluginsFolder;
  25. public bool GoogleLiveSyncIsUptoDate = true;
  26. public List<Object> Assets = new List<Object>(); // References to Fonts, Atlasses and other objects the localization may need
  27. public string Google_WebServiceURL;
  28. public string Google_SpreadsheetKey;
  29. public string Google_SpreadsheetName;
  30. public string Google_LastUpdatedVersion;
  31. public LanguageSourceData.eGoogleUpdateFrequency GoogleUpdateFrequency = LanguageSourceData.eGoogleUpdateFrequency.Weekly;
  32. public float GoogleUpdateDelay = 5; // How many second to delay downloading data from google (to avoid lag on the startup)
  33. public delegate void fnOnSourceUpdated(LanguageSourceData source, bool ReceivedNewData, string errorMsg);
  34. public event fnOnSourceUpdated Event_OnSourceUpdateFromGoogle;
  35. public List<LanguageData> mLanguages = new List<LanguageData>();
  36. public bool IgnoreDeviceLanguage; // If false, it will use the Device's language as the initial Language, otherwise it will use the first language in the source.
  37. public LanguageSourceData.eAllowUnloadLanguages _AllowUnloadingLanguages = LanguageSourceData.eAllowUnloadLanguages.Never;
  38. public List<TermData> mTerms = new List<TermData>();
  39. public bool CaseInsensitiveTerms;
  40. public LanguageSourceData.MissingTranslationAction OnMissingTranslation = LanguageSourceData.MissingTranslationAction.Fallback;
  41. public string mTerm_AppName;
  42. #endregion
  43. #region EditorVariables
  44. #if UNITY_EDITOR
  45. public string Spreadsheet_LocalFileName;
  46. public string Spreadsheet_LocalCSVSeparator = ",";
  47. public string Spreadsheet_LocalCSVEncoding = "utf-8";
  48. public bool Spreadsheet_SpecializationAsRows = true;
  49. public string Google_Password = "change_this";
  50. public LanguageSourceData.eGoogleUpdateFrequency GoogleInEditorCheckFrequency = LanguageSourceData.eGoogleUpdateFrequency.Daily;
  51. #endif
  52. #endregion
  53. void Awake()
  54. {
  55. #if UNITY_EDITOR
  56. if (BuildPipeline.isBuildingPlayer)
  57. return;
  58. #endif
  59. // NeverDestroy = false;
  60. // if (NeverDestroy)
  61. //{
  62. // if (mSource.ManagerHasASimilarSource())
  63. // {
  64. // Object.Destroy (this);
  65. // return;
  66. // }
  67. // else
  68. // {
  69. // if (Application.isPlaying)
  70. // DontDestroyOnLoad (gameObject);
  71. // }
  72. //}
  73. mSource.owner = this;
  74. mSource.Awake();
  75. }
  76. private void OnDestroy()
  77. {
  78. NeverDestroy = false;
  79. if (!NeverDestroy)
  80. {
  81. mSource.OnDestroy();
  82. }
  83. }
  84. public string GetSourceName()
  85. {
  86. string s = gameObject.name;
  87. Transform tr = transform.parent;
  88. while (tr)
  89. {
  90. s = string.Concat(tr.name, "_", s);
  91. tr = tr.parent;
  92. }
  93. return s;
  94. }
  95. public void OnBeforeSerialize()
  96. {
  97. version = 1;
  98. }
  99. public void OnAfterDeserialize()
  100. {
  101. if (version==0 || mSource==null)
  102. {
  103. mSource = new LanguageSourceData();
  104. mSource.owner = this;
  105. mSource.UserAgreesToHaveItOnTheScene = UserAgreesToHaveItOnTheScene;
  106. mSource.UserAgreesToHaveItInsideThePluginsFolder = UserAgreesToHaveItInsideThePluginsFolder;
  107. mSource.IgnoreDeviceLanguage = IgnoreDeviceLanguage;
  108. mSource._AllowUnloadingLanguages = _AllowUnloadingLanguages;
  109. mSource.CaseInsensitiveTerms = CaseInsensitiveTerms;
  110. mSource.OnMissingTranslation = OnMissingTranslation;
  111. mSource.mTerm_AppName = mTerm_AppName;
  112. mSource.GoogleLiveSyncIsUptoDate = GoogleLiveSyncIsUptoDate;
  113. mSource.Google_WebServiceURL = Google_WebServiceURL;
  114. mSource.Google_SpreadsheetKey = Google_SpreadsheetKey;
  115. mSource.Google_SpreadsheetName = Google_SpreadsheetName;
  116. mSource.Google_LastUpdatedVersion = Google_LastUpdatedVersion;
  117. mSource.GoogleUpdateFrequency = GoogleUpdateFrequency;
  118. mSource.GoogleUpdateDelay = GoogleUpdateDelay;
  119. mSource.Event_OnSourceUpdateFromGoogle += Event_OnSourceUpdateFromGoogle;
  120. if (mLanguages != null && mLanguages.Count>0)
  121. {
  122. mSource.mLanguages.Clear();
  123. mSource.mLanguages.AddRange(mLanguages);
  124. mLanguages.Clear();
  125. }
  126. if (Assets != null && Assets.Count > 0)
  127. {
  128. mSource.Assets.Clear();
  129. mSource.Assets.AddRange(Assets);
  130. Assets.Clear();
  131. }
  132. if (mTerms != null && mTerms.Count>0)
  133. {
  134. mSource.mTerms.Clear();
  135. for (int i=0; i<mTerms.Count; ++i)
  136. mSource.mTerms.Add(mTerms[i]);
  137. mTerms.Clear();
  138. }
  139. version = 1;
  140. Event_OnSourceUpdateFromGoogle = null;
  141. }
  142. }
  143. }
  144. }