LanguageSource.cs 6.4 KB

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