UpgradeManager.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. namespace I2.Loc
  6. {
  7. [InitializeOnLoad]
  8. public class UpgradeManager
  9. {
  10. static bool mAlreadyCheckedPlugins = false;
  11. static UpgradeManager()
  12. {
  13. EditorApplication.update += AutoCheckPlugins;
  14. }
  15. public static void AutoCheckPlugins()
  16. {
  17. CheckPlugins ();
  18. }
  19. public static void CheckPlugins( bool bForce = false )
  20. {
  21. EditorApplication.update -= AutoCheckPlugins;
  22. if (mAlreadyCheckedPlugins && !bForce)
  23. return;
  24. mAlreadyCheckedPlugins = true;
  25. EnablePlugins(bForce);
  26. CreateLanguageSources();
  27. //CreateScriptLocalization();
  28. }
  29. const string EditorPrefs_AutoEnablePlugins = "I2Loc AutoEnablePlugins";
  30. [MenuItem( "Tools/I2 Localization/Enable Plugins/Force Detection", false, 0 )]
  31. public static void ForceCheckPlugins()
  32. {
  33. CheckPlugins( true );
  34. }
  35. [MenuItem( "Tools/I2 Localization/Enable Plugins/Enable Auto Detection", false, 1 )]
  36. public static void EnableAutoCheckPlugins()
  37. {
  38. EditorPrefs.SetBool(EditorPrefs_AutoEnablePlugins, true);
  39. }
  40. [MenuItem( "Tools/I2 Localization/Enable Plugins/Enable Auto Detection", true)]
  41. public static bool ValidEnableAutoCheckPlugins()
  42. {
  43. return !EditorPrefs.GetBool(EditorPrefs_AutoEnablePlugins, true);
  44. }
  45. [MenuItem( "Tools/I2 Localization/Enable Plugins/Disable Auto Detection", false, 2 )]
  46. public static void DisableAutoCheckPlugins()
  47. {
  48. EditorPrefs.SetBool(EditorPrefs_AutoEnablePlugins, false);
  49. }
  50. [MenuItem( "Tools/I2 Localization/Enable Plugins/Disable Auto Detection", true)]
  51. public static bool ValidDisableAutoCheckPlugins()
  52. {
  53. return EditorPrefs.GetBool(EditorPrefs_AutoEnablePlugins, true);
  54. }
  55. [MenuItem("Tools/I2 Localization/Toggle Highlight Localized", false, 17)]
  56. public static void ToogleH()
  57. {
  58. LocalizationManager.HighlightLocalizedTargets = !LocalizationManager.HighlightLocalizedTargets;
  59. LocalizationManager.LocalizeAll(true);
  60. }
  61. [MenuItem("Tools/I2 Localization/Create Temp")]
  62. public static void CreateTemp()
  63. {
  64. LanguageSourceData source = LocalizationManager.Sources[0];
  65. for (int i = 0; i < 1000; ++i)
  66. source.AddTerm("Term " + i, eTermType.Text, false);
  67. source.UpdateDictionary(true);
  68. }
  69. public static void EnablePlugins( bool bForce = false )
  70. {
  71. if (!bForce)
  72. {
  73. bool AutoEnablePlugins = EditorPrefs.GetBool(EditorPrefs_AutoEnablePlugins, true);
  74. if (!AutoEnablePlugins)
  75. return;
  76. }
  77. //var tar = System.Enum.GetValues(typeof(BuildTargetGroup));
  78. foreach (BuildTargetGroup target in System.Enum.GetValues(typeof(BuildTargetGroup)))
  79. if (target!=BuildTargetGroup.Unknown && !target.HasAttributeOfType<System.ObsoleteAttribute>())
  80. {
  81. #if UNITY_5_6
  82. if (target == BuildTargetGroup.Switch) continue; // some releases of 5.6 defined BuildTargetGroup.Switch but didn't handled it correctly
  83. #endif
  84. EnablePluginsOnPlatform( target );
  85. }
  86. // Force these one (iPhone has the same # than iOS and iPhone is deprecated, so iOS was been skipped)
  87. EnablePluginsOnPlatform(BuildTargetGroup.iOS);
  88. }
  89. static void EnablePluginsOnPlatform( BuildTargetGroup Platform )
  90. {
  91. string Settings = PlayerSettings.GetScriptingDefineSymbolsForGroup(Platform );
  92. bool HasChanged = false;
  93. List<string> symbols = new List<string>( Settings.Split(';'));
  94. HasChanged |= UpdateSettings("NGUI", "NGUIDebug", "", ref symbols);
  95. HasChanged |= UpdateSettings("DFGUI", "dfPanel", "", ref symbols);
  96. HasChanged |= UpdateSettings("TK2D", "tk2dTextMesh", "", ref symbols);
  97. HasChanged |= UpdateSettings( "TextMeshPro", "TMPro.TMP_FontAsset", "TextMeshPro", ref symbols );
  98. HasChanged |= UpdateSettings( "SVG", "SVGImporter.SVGAsset", "", ref symbols );
  99. if (HasChanged)
  100. {
  101. try
  102. {
  103. Settings = string.Empty;
  104. for (int i=0,imax=symbols.Count; i<imax; ++i)
  105. {
  106. if (i>0) Settings += ";";
  107. Settings += symbols[i];
  108. }
  109. PlayerSettings.SetScriptingDefineSymbolsForGroup(Platform, Settings );
  110. }
  111. catch (System.Exception)
  112. {
  113. }
  114. }
  115. }
  116. static bool UpdateSettings( string mPlugin, string mType, string AssemblyType, ref List<string> symbols)
  117. {
  118. try
  119. {
  120. bool hasPluginClass = false;
  121. if (!string.IsNullOrEmpty( AssemblyType ))
  122. {
  123. var rtype = System.AppDomain.CurrentDomain.GetAssemblies()
  124. .Where( assembly => assembly.FullName.Contains(AssemblyType) )
  125. .Select( assembly => assembly.GetType( mType, false ) )
  126. .Where( t => t!=null )
  127. .FirstOrDefault();
  128. if (rtype != null)
  129. hasPluginClass = true;
  130. }
  131. if (!hasPluginClass)
  132. hasPluginClass = typeof( Localize ).Assembly.GetType( mType, false )!=null;
  133. bool hasPluginDef = (symbols.IndexOf(mPlugin)>=0);
  134. if (hasPluginClass != hasPluginDef)
  135. {
  136. if (hasPluginClass) symbols.Add(mPlugin);
  137. else symbols.Remove(mPlugin);
  138. return true;
  139. }
  140. }
  141. catch(System.Exception)
  142. {
  143. }
  144. return false;
  145. }
  146. //[MenuItem( "Tools/I2 Localization/Create I2Languages", false, 1)]
  147. public static void CreateLanguageSources()
  148. {
  149. if (LocalizationManager.GlobalSources==null || LocalizationManager.GlobalSources.Length==0)
  150. return;
  151. Object GlobalSource = Resources.Load(LocalizationManager.GlobalSources[0]);
  152. LanguageSourceData sourceData = null;
  153. string sourcePath = null;
  154. if (GlobalSource != null)
  155. {
  156. if (GlobalSource is GameObject)
  157. {
  158. // I2Languages was a prefab before 2018.3, it should be converted to an ScriptableObject
  159. sourcePath = AssetDatabase.GetAssetPath(GlobalSource);
  160. LanguageSource langSourceObj = (GlobalSource as GameObject).GetComponent<LanguageSource>();
  161. sourceData = langSourceObj.mSource;
  162. }
  163. else
  164. {
  165. return;
  166. }
  167. }
  168. LanguageSourceAsset asset = ScriptableObject.CreateInstance<LanguageSourceAsset>();
  169. if (sourceData != null)
  170. {
  171. asset.mSource = sourceData;
  172. AssetDatabase.DeleteAsset(sourcePath);
  173. }
  174. if (string.IsNullOrEmpty(sourcePath))
  175. {
  176. //string PluginPath = GetI2LocalizationPath();
  177. string ResourcesFolder = "Assets/Resources";//PluginPath.Substring(0, PluginPath.Length-"/Localization".Length) + "/Resources";
  178. string fullresFolder = Application.dataPath + ResourcesFolder.Replace("Assets", "");
  179. if (!System.IO.Directory.Exists(fullresFolder))
  180. System.IO.Directory.CreateDirectory(fullresFolder);
  181. sourcePath = ResourcesFolder + "/" + LocalizationManager.GlobalSources[0] + ".asset";
  182. }
  183. else
  184. {
  185. sourcePath = sourcePath.Replace(".prefab", ".asset");
  186. }
  187. AssetDatabase.CreateAsset(asset, sourcePath);
  188. AssetDatabase.SaveAssets();
  189. AssetDatabase.Refresh();
  190. }
  191. [MenuItem("Tools/I2 Localization/Help", false, 30)]
  192. [MenuItem("Help/I2 Localization")]
  193. public static void MainHelp()
  194. {
  195. Application.OpenURL(LocalizeInspector.HelpURL_Documentation);
  196. }
  197. [MenuItem("Tools/I2 Localization/Open I2Languages.asset", false, 0)]
  198. public static void OpenGlobalSource()
  199. {
  200. CreateLanguageSources();
  201. LanguageSourceAsset GO = Resources.Load<LanguageSourceAsset>(LocalizationManager.GlobalSources[0]);
  202. if (GO == null)
  203. Debug.Log("Unable to find Global Language at Assets/Resources/" + LocalizationManager.GlobalSources[0] + ".asset");
  204. Selection.activeObject = GO;
  205. }
  206. /*static void CreateScriptLocalization()
  207. {
  208. string[] assets = AssetDatabase.FindAssets("ScriptLocalization");
  209. if (assets.Length>0)
  210. return;
  211. string ScriptsFolder = "Assets";
  212. string ScriptText = LocalizationEditor.mScriptLocalizationHeader + " }\n}";
  213. System.IO.File.WriteAllText(ScriptsFolder + "/ScriptLocalization.cs", ScriptText);
  214. AssetDatabase.SaveAssets();
  215. AssetDatabase.Refresh();
  216. }*/
  217. public static string GetI2LocalizationPath()
  218. {
  219. string[] assets = AssetDatabase.FindAssets("LocalizationManager");
  220. if (assets.Length==0)
  221. return string.Empty;
  222. string PluginPath = AssetDatabase.GUIDToAssetPath(assets[0]);
  223. PluginPath = PluginPath.Substring(0, PluginPath.Length - "/Scripts/LocalizationManager.cs".Length);
  224. return PluginPath;
  225. }
  226. public static string GetI2Path()
  227. {
  228. string pluginPath = GetI2LocalizationPath();
  229. return pluginPath.Substring(0, pluginPath.Length-"/Localization".Length);
  230. }
  231. public static string GetI2CommonResourcesPath()
  232. {
  233. string I2Path = GetI2Path();
  234. return I2Path + "/Resources";
  235. }
  236. }
  237. public static class UpgradeManagerHelper
  238. {
  239. public static bool HasAttributeOfType<T>(this System.Enum enumVal) where T:System.Attribute
  240. {
  241. var type = enumVal.GetType();
  242. var memInfo = type.GetMember(enumVal.ToString());
  243. var attributes = memInfo[0].GetCustomAttributes(typeof(T), false);
  244. return attributes.Length > 0;
  245. }
  246. }
  247. }