Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TMP_PackageUtilities.cs 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using UnityEditor;
  4. using System;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.Threading;
  11. using TMPro.EditorUtilities;
  12. #if UNITY_2023_3_OR_NEWER
  13. using PhysicsMaterialAsset = UnityEngine.PhysicsMaterial;
  14. #else
  15. using PhysicsMaterialAsset = UnityEngine.PhysicMaterial;
  16. #endif
  17. namespace TMPro
  18. {
  19. // Suppressing warnings related to the use of private structures which are confusing the compiler as these data structures are used by .json files.
  20. #pragma warning disable 0649
  21. /// <summary>
  22. /// Data structure containing the target and replacement fileIDs and GUIDs which will require remapping from previous version of TextMesh Pro to the new TextMesh Pro UPM package.
  23. /// </summary>
  24. [System.Serializable]
  25. struct AssetConversionRecord
  26. {
  27. public string referencedResource;
  28. public string target;
  29. public string replacement;
  30. }
  31. /// <summary>
  32. /// Data structure containing a list of target and replacement fileID and GUID requiring remapping from previous versions of TextMesh Pro to the new TextMesh Pro UPM package.
  33. /// This data structure is populated with the data contained in the PackageConversionData.json file included in the package.
  34. /// </summary>
  35. [System.Serializable]
  36. class AssetConversionData
  37. {
  38. public List<AssetConversionRecord> assetRecords;
  39. }
  40. internal class TMP_ProjectTextSpacingConversionTool : EditorWindow
  41. {
  42. // Create Text Spacing Conversion Tool window
  43. [MenuItem("Window/TextMeshPro/Project Text Spacing Conversion Tool", false, 2110)]
  44. static void ShowConverterWindow()
  45. {
  46. var window = GetWindow<TMP_ProjectTextSpacingConversionTool>();
  47. window.titleContent = new GUIContent("Conversion Tool");
  48. window.Focus();
  49. }
  50. /// <summary>
  51. ///
  52. /// </summary>
  53. struct AssetModificationRecord
  54. {
  55. public string assetFilePath;
  56. public string assetDataFile;
  57. }
  58. struct AssetFileRecord
  59. {
  60. public string assetFilePath;
  61. public AssetFileRecord(string filePath, string metaFilePath)
  62. {
  63. this.assetFilePath = filePath;
  64. }
  65. }
  66. private static string m_ProjectPath;
  67. [SerializeField] private string m_ProjectFolderToScan;
  68. private static bool m_IsAlreadyScanningProject;
  69. private static bool m_CancelScanProcess;
  70. private static string k_ProjectScanReportDefaultText = "<color=#FFFF80><b>" +
  71. " Character Word Line Paragraph\n" +
  72. "Project Scan Results Spacing Spacing Spacing Spacing</b></color>\n" +
  73. "------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n";
  74. [SerializeField] private GUIStyle m_OutputWindowStyle;
  75. [SerializeField] private Font m_OutputWindowMonospacedFont;
  76. private static string k_ProjectScanLabelPrefix = "Scanning: ";
  77. private static string m_ProjectScanResults = string.Empty;
  78. private static Vector2 m_ProjectScanResultScrollPosition;
  79. private static float m_ProgressPercentage = 0;
  80. private static int m_ScanningTotalFiles;
  81. private static int m_ScanningCurrentFileIndex;
  82. private static string m_ScanningCurrentFileName;
  83. private static string k_TextMeshProScriptID = "m_Script: {fileID: 11500000, guid: 9541d86e2fd84c1d9990edf0852d74ab, type: 3}";
  84. private static string k_TextMeshProUGUIScriptID = "m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}";
  85. //private static string k_FontAssetScriptID = "m_Script: {fileID: 11500000, guid: 71c1514a6bd24e1e882cebbe1904ce04, type: 3}";
  86. private static string k_FontAssetProperty = "m_fontAsset: ";
  87. private static string k_FontSizeProperty = "m_fontSize: ";
  88. private static string k_LineSpacingProperty = "m_lineSpacing: ";
  89. private static string k_CharacterSpacingProperty = "m_characterSpacing: ";
  90. private static string k_WordSpacingProperty = "m_wordSpacing: ";
  91. private static string k_ParagraphSpacingProperty = "m_paragraphSpacing: ";
  92. private static AssetConversionData m_ConversionData;
  93. private static readonly List<AssetModificationRecord> m_ModifiedAssetList = new List<AssetModificationRecord>();
  94. void OnEnable()
  95. {
  96. // Set Editor Window Size
  97. SetEditorWindowSize();
  98. m_ProjectScanResults = k_ProjectScanReportDefaultText;
  99. // Define new style with monospaced font (if we have not already done so).
  100. if (m_OutputWindowStyle == null || m_OutputWindowMonospacedFont == null)
  101. {
  102. if (m_OutputWindowMonospacedFont == null)
  103. m_OutputWindowMonospacedFont = Font.CreateDynamicFontFromOSFont("Courier New", 13);
  104. if (m_OutputWindowStyle == null)
  105. {
  106. m_OutputWindowStyle = new GUIStyle() {font = m_OutputWindowMonospacedFont, richText = true};
  107. m_OutputWindowStyle.normal.textColor = new Color(0.95f, 0.95f, 0.95f, 1f);
  108. }
  109. else
  110. {
  111. m_OutputWindowStyle.font = m_OutputWindowMonospacedFont;
  112. }
  113. }
  114. }
  115. void OnGUI()
  116. {
  117. // Define new style with monospaced font (if we have not already done so).
  118. if (m_OutputWindowStyle == null || m_OutputWindowMonospacedFont == null)
  119. {
  120. if (m_OutputWindowMonospacedFont == null)
  121. m_OutputWindowMonospacedFont = Font.CreateDynamicFontFromOSFont("Courier New", 13);
  122. if (m_OutputWindowStyle == null)
  123. {
  124. m_OutputWindowStyle = new GUIStyle() {font = m_OutputWindowMonospacedFont, richText = true};
  125. m_OutputWindowStyle.normal.textColor = new Color(0.95f, 0.95f, 0.95f, 1f);
  126. }
  127. else
  128. {
  129. m_OutputWindowStyle.font = m_OutputWindowMonospacedFont;
  130. }
  131. }
  132. GUILayout.BeginVertical();
  133. {
  134. // Scan project files and resources
  135. GUILayout.BeginVertical(EditorStyles.helpBox);
  136. {
  137. GUILayout.Label("Scan Project Files", EditorStyles.boldLabel);
  138. GUILayout.Label("Press the <i>Scan Project Files</i> button to begin scanning your project for Scenes and Prefabs containing text objects whose line spacing values might need to be converted to the new (em) line spacing values.", TMP_UIStyleManager.label);
  139. GUILayout.Space(10f);
  140. GUILayout.Label("Project folder to be scanned. Example \"Assets/TextMesh Pro\"");
  141. m_ProjectFolderToScan = EditorGUILayout.TextField("Folder Path: Assets/", m_ProjectFolderToScan);
  142. GUILayout.Space(5f);
  143. GUI.enabled = m_IsAlreadyScanningProject == false ? true : false;
  144. if (GUILayout.Button("Scan Project Files"))
  145. {
  146. m_CancelScanProcess = false;
  147. // Make sure Asset Serialization mode is set to ForceText and Version Control mode to Visible Meta Files.
  148. if (CheckProjectSerializationAndSourceControlModes() == true)
  149. {
  150. m_ProjectPath = Path.GetFullPath("Assets/..");
  151. TMP_EditorCoroutine.StartCoroutine(ScanProjectFiles());
  152. }
  153. else
  154. {
  155. EditorUtility.DisplayDialog("Project Settings Change Required", "In menu options \"Edit - Project Settings - Editor\", please change Asset Serialization Mode to ForceText and Source Control Mode to Visible Meta Files.", "OK", string.Empty);
  156. }
  157. }
  158. GUI.enabled = true;
  159. // Display progress bar
  160. Rect rect = GUILayoutUtility.GetRect(0f, 20f, GUILayout.ExpandWidth(true));
  161. EditorGUI.ProgressBar(rect, m_ProgressPercentage, "Scan Progress (" + m_ScanningCurrentFileIndex + "/" + m_ScanningTotalFiles + ")");
  162. // Display cancel button and name of file currently being scanned.
  163. if (m_IsAlreadyScanningProject)
  164. {
  165. Rect cancelRect = new Rect(rect.width - 20, rect.y + 2, 20, 16);
  166. if (GUI.Button(cancelRect, "X"))
  167. {
  168. m_CancelScanProcess = true;
  169. }
  170. GUILayout.Label(k_ProjectScanLabelPrefix + m_ScanningCurrentFileName, TMP_UIStyleManager.label);
  171. }
  172. else
  173. GUILayout.Label(string.Empty);
  174. GUILayout.Space(5);
  175. // Creation Feedback
  176. GUILayout.BeginVertical(TMP_UIStyleManager.textAreaBoxWindow, GUILayout.ExpandHeight(true));
  177. {
  178. m_ProjectScanResultScrollPosition = EditorGUILayout.BeginScrollView(m_ProjectScanResultScrollPosition, GUILayout.ExpandHeight(true));
  179. GUILayout.Label(m_ProjectScanResults, m_OutputWindowStyle);
  180. EditorGUILayout.EndScrollView();
  181. }
  182. GUILayout.EndVertical();
  183. GUILayout.Space(5f);
  184. }
  185. GUILayout.EndVertical();
  186. // Scan project files and resources
  187. GUILayout.BeginVertical(EditorStyles.helpBox);
  188. {
  189. GUILayout.Label("Save Modified Project Files", EditorStyles.boldLabel);
  190. GUILayout.Label("Pressing the <i>Save Modified Project Files</i> button will update the files in the <i>Project Scan Results</i> listed above. <color=#FFFF80>Please make sure that you have created a backup of your project first</color> as these file modifications are permanent and cannot be undone.", TMP_UIStyleManager.label);
  191. GUILayout.Space(5f);
  192. GUI.enabled = m_IsAlreadyScanningProject == false && m_ModifiedAssetList.Count > 0 ? true : false;
  193. if (GUILayout.Button("Save Modified Project Files"))
  194. {
  195. UpdateProjectFiles();
  196. }
  197. GUILayout.Space(10f);
  198. }
  199. GUILayout.EndVertical();
  200. }
  201. GUILayout.EndVertical();
  202. GUILayout.Space(5f);
  203. }
  204. void OnInspectorUpdate()
  205. {
  206. Repaint();
  207. }
  208. /// <summary>
  209. /// Limits the minimum size of the editor window.
  210. /// </summary>
  211. void SetEditorWindowSize()
  212. {
  213. EditorWindow editorWindow = this;
  214. Vector2 currentWindowSize = editorWindow.minSize;
  215. editorWindow.minSize = new Vector2(Mathf.Max(1024, currentWindowSize.x), Mathf.Max(420, currentWindowSize.y));
  216. }
  217. private IEnumerator ScanProjectFiles()
  218. {
  219. m_IsAlreadyScanningProject = true;
  220. string packageFullPath = EditorUtilities.TMP_EditorUtility.packageFullPath;
  221. // List containing assets that have been modified.
  222. m_ProjectScanResults = k_ProjectScanReportDefaultText;
  223. m_ModifiedAssetList.Clear();
  224. m_ProgressPercentage = 0;
  225. // Get list of GUIDs for assets that might contain references to previous GUIDs that require updating.
  226. string searchFolder = string.IsNullOrEmpty(m_ProjectFolderToScan) ? "Assets" : ("Assets/" + m_ProjectFolderToScan);
  227. string[] guids = AssetDatabase.FindAssets("t:Object", new string[] { searchFolder }).Distinct().ToArray();
  228. k_ProjectScanLabelPrefix = "<b>Phase 1 - Filtering:</b> ";
  229. m_ScanningTotalFiles = guids.Length;
  230. m_ScanningCurrentFileIndex = 0;
  231. List<AssetFileRecord> projectFilesToScan = new List<AssetFileRecord>();
  232. foreach (var guid in guids)
  233. {
  234. if (m_CancelScanProcess)
  235. break;
  236. string assetFilePath = AssetDatabase.GUIDToAssetPath(guid);
  237. m_ScanningCurrentFileIndex += 1;
  238. m_ScanningCurrentFileName = assetFilePath;
  239. m_ProgressPercentage = (float)m_ScanningCurrentFileIndex / m_ScanningTotalFiles;
  240. string fileExtension = Path.GetExtension(assetFilePath);
  241. Type fileType = AssetDatabase.GetMainAssetTypeAtPath(assetFilePath);
  242. // Ignore all files other than Scenes and Prefabs.
  243. if ((fileType == typeof(SceneAsset) || (fileType == typeof(GameObject) && fileExtension.ToLower() == ".prefab")) == false)
  244. continue;
  245. string assetMetaFilePath = AssetDatabase.GetTextMetaFilePathFromAssetPath(assetFilePath);
  246. projectFilesToScan.Add(new AssetFileRecord(assetFilePath, assetMetaFilePath));
  247. yield return null;
  248. }
  249. m_ScanningTotalFiles = projectFilesToScan.Count;
  250. k_ProjectScanLabelPrefix = "<b>Phase 2 - Scanning:</b> ";
  251. m_ScanningCurrentFileIndex = 0;
  252. for (int i = 0; i < m_ScanningTotalFiles; i++)
  253. {
  254. if (m_CancelScanProcess)
  255. break;
  256. AssetFileRecord fileRecord = projectFilesToScan[i];
  257. ScanProjectFile(fileRecord);
  258. m_ScanningCurrentFileName = fileRecord.assetFilePath;
  259. m_ScanningCurrentFileIndex += 1;
  260. m_ProgressPercentage = (float)m_ScanningCurrentFileIndex / m_ScanningTotalFiles;
  261. yield return null;
  262. }
  263. m_IsAlreadyScanningProject = false;
  264. m_ScanningCurrentFileName = string.Empty;
  265. }
  266. static void ScanProjectFile(AssetFileRecord fileRecord)
  267. {
  268. if (m_CancelScanProcess)
  269. return;
  270. // Read the asset data file
  271. string assetDataFile;
  272. bool hasDataFileChanged = false;
  273. try
  274. {
  275. assetDataFile = File.ReadAllText(m_ProjectPath + "/" + fileRecord.assetFilePath);
  276. }
  277. catch
  278. {
  279. // Continue to the next asset if we can't read the current one.
  280. return;
  281. }
  282. // Check if asset file references any text components.
  283. if (assetDataFile.Contains(k_TextMeshProScriptID) || assetDataFile.Contains(k_TextMeshProUGUIScriptID))
  284. {
  285. float characterSpacingValue = 0;
  286. float newCharacterSpacingValue = 0;
  287. float wordSpacingValue = 0;
  288. float newWordSpacingValue = 0;
  289. float lineSpacingValue = 0;
  290. float newLineSpacingValue = 0;
  291. float paragraphSpacingValue = 0;
  292. float newParagraphSpacingValue = 0;
  293. float fontSize = 0;
  294. float samplingPointSize = 0;
  295. float faceScale = 1;
  296. List<string> lines = assetDataFile.Split('\n').ToList();
  297. int serializedVersionInsertionIndex = 0;
  298. int readingFlag = 0;
  299. // Read through each lines of the asset file
  300. for (int i = 0; i < lines.Count; i++)
  301. {
  302. string line = lines[i];
  303. // Track potential line index to insert serializedVersion property
  304. if (line.Contains("MonoBehaviour:"))
  305. {
  306. serializedVersionInsertionIndex = i + 1;
  307. continue;
  308. }
  309. // Read until we find the line that contains a reference to a text component
  310. if (readingFlag == 0 && (line.Contains(k_TextMeshProScriptID) || line.Contains(k_TextMeshProUGUIScriptID)))
  311. {
  312. // Check if spacing values for this component have already been converted
  313. if (lines[serializedVersionInsertionIndex].Contains(" m_SerializedVersion: 1"))
  314. {
  315. readingFlag = 0;
  316. continue;
  317. }
  318. lines.Insert(serializedVersionInsertionIndex, " m_SerializedVersion: 1");
  319. readingFlag = 1;
  320. continue;
  321. }
  322. // Keep reading until we find the font asset property field.
  323. if (readingFlag == 1)
  324. {
  325. // Check for font asset property
  326. if (line.Contains(k_FontAssetProperty))
  327. {
  328. int guidIndex = line.IndexOf("guid: ", StringComparison.InvariantCulture);
  329. if (guidIndex != -1)
  330. {
  331. string guid = line.Substring(guidIndex + 6, 32);
  332. TMP_FontAsset fontAsset = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(AssetDatabase.GUIDToAssetPath(guid));
  333. if (fontAsset != null)
  334. {
  335. samplingPointSize = fontAsset.faceInfo.pointSize;
  336. faceScale = fontAsset.faceInfo.scale;
  337. }
  338. }
  339. readingFlag = 2;
  340. continue;
  341. }
  342. }
  343. // Read font size property
  344. if (readingFlag == 2)
  345. {
  346. if (line.Contains(k_FontSizeProperty))
  347. {
  348. fontSize = float.Parse(line.Split(':')[1], NumberStyles.Float, CultureInfo.InvariantCulture);
  349. readingFlag = 3;
  350. continue;
  351. }
  352. }
  353. // Check for the spacing properties that need to be converted
  354. if (readingFlag == 3)
  355. {
  356. // Read character spacing
  357. if (line.Contains(k_CharacterSpacingProperty))
  358. {
  359. characterSpacingValue = float.Parse(line.Split(':')[1], NumberStyles.Float, CultureInfo.InvariantCulture);
  360. if (characterSpacingValue != 0)
  361. {
  362. // Convert character spacing value.
  363. newCharacterSpacingValue = characterSpacingValue * faceScale / (samplingPointSize * 0.01f);
  364. lines[i] = lines[i].Replace(k_CharacterSpacingProperty + characterSpacingValue, k_CharacterSpacingProperty + newCharacterSpacingValue);
  365. hasDataFileChanged = true;
  366. }
  367. continue;
  368. }
  369. // Read word spacing
  370. if (line.Contains(k_WordSpacingProperty))
  371. {
  372. // Get the character spacing value
  373. wordSpacingValue = float.Parse(line.Split(':')[1], NumberStyles.Float, CultureInfo.InvariantCulture);
  374. if (wordSpacingValue != 0)
  375. {
  376. // Convert character spacing value.
  377. newWordSpacingValue = wordSpacingValue * faceScale / (samplingPointSize * 0.01f);
  378. lines[i] = lines[i].Replace(k_WordSpacingProperty + wordSpacingValue, k_WordSpacingProperty + newWordSpacingValue);
  379. hasDataFileChanged = true;
  380. }
  381. continue;
  382. }
  383. // Read line spacing
  384. if (line.Contains(k_LineSpacingProperty))
  385. {
  386. // Get the value of line spacing value
  387. lineSpacingValue = float.Parse(line.Split(':')[1], NumberStyles.Float, CultureInfo.InvariantCulture);
  388. if (lineSpacingValue != 0)
  389. {
  390. // Convert line spacing value.
  391. newLineSpacingValue = lineSpacingValue / (fontSize * 0.01f) * fontSize / samplingPointSize * faceScale;
  392. lines[i] = lines[i].Replace(k_LineSpacingProperty + lineSpacingValue, k_LineSpacingProperty + newLineSpacingValue);
  393. hasDataFileChanged = true;
  394. }
  395. continue;
  396. }
  397. // Read paragraph spacing
  398. if (line.Contains(k_ParagraphSpacingProperty))
  399. {
  400. // Get the value of line spacing value
  401. paragraphSpacingValue = float.Parse(line.Split(':')[1], NumberStyles.Float, CultureInfo.InvariantCulture);
  402. if (paragraphSpacingValue != 0)
  403. {
  404. // Convert line spacing value.
  405. newParagraphSpacingValue = paragraphSpacingValue / (fontSize * 0.01f) * fontSize / samplingPointSize * faceScale;
  406. lines[i] = lines[i].Replace(k_ParagraphSpacingProperty + paragraphSpacingValue, k_ParagraphSpacingProperty + newParagraphSpacingValue);
  407. hasDataFileChanged = true;
  408. }
  409. readingFlag = 4;
  410. continue;
  411. }
  412. }
  413. // Done reading text component serialized data.
  414. if (readingFlag == 4 && line.Contains("---"))
  415. {
  416. readingFlag = 0;
  417. string characterSpacingFormat = $"{(characterSpacingValue == 0 ? " " : $"{characterSpacingValue,10:F}{newCharacterSpacingValue,10:F}")}";
  418. string wordSpacingFormat = $"{(wordSpacingValue == 0 ? " " : $"{wordSpacingValue,10:F}{newWordSpacingValue,10:F}")}";
  419. string lineSpacingFormat = $"{(lineSpacingValue == 0 ? " " : $"{lineSpacingValue,10:F}{newLineSpacingValue,10:F}")}";
  420. string paragraphSpacingFormat = $"{(paragraphSpacingValue == 0 ? " " : $"{paragraphSpacingValue,10:F}{newParagraphSpacingValue,10:F}")}";
  421. if (characterSpacingValue != 0 || lineSpacingValue != 0)
  422. m_ProjectScanResults += $"{fileRecord.assetFilePath,-100}" + characterSpacingFormat + wordSpacingFormat + lineSpacingFormat + paragraphSpacingFormat + "\n";
  423. // Update asset data file
  424. assetDataFile = string.Join("\n", lines);
  425. newCharacterSpacingValue = 0;
  426. newWordSpacingValue = 0;
  427. newLineSpacingValue = 0;
  428. newParagraphSpacingValue = 0;
  429. }
  430. }
  431. }
  432. // Check if asset file is a font asset
  433. // if (assetDataFile.Contains(k_FontAssetScriptID))
  434. // {
  435. // float samplingPointSize;
  436. // float normalSpacing;
  437. // float newNormalSpacing;
  438. // float boldSpacing;
  439. // float newBoldSpacing;
  440. // }
  441. if (hasDataFileChanged)
  442. {
  443. AssetModificationRecord modifiedAsset;
  444. modifiedAsset.assetFilePath = fileRecord.assetFilePath;
  445. modifiedAsset.assetDataFile = assetDataFile;
  446. m_ModifiedAssetList.Add(modifiedAsset);
  447. }
  448. }
  449. /// <summary>
  450. ///
  451. /// </summary>
  452. private static void ResetScanProcess()
  453. {
  454. m_IsAlreadyScanningProject = false;
  455. m_ScanningCurrentFileName = string.Empty;
  456. m_ProgressPercentage = 0;
  457. m_ScanningCurrentFileIndex = 0;
  458. m_ScanningTotalFiles = 0;
  459. }
  460. /// <summary>
  461. ///
  462. /// </summary>
  463. private static void UpdateProjectFiles()
  464. {
  465. // Make sure Asset Serialization mode is set to ForceText with Visible Meta Files.
  466. CheckProjectSerializationAndSourceControlModes();
  467. string projectPath = Path.GetFullPath("Assets/..");
  468. // Display dialogue to show user a list of project files that will be modified upon their consent.
  469. if (EditorUtility.DisplayDialog("Save Modified Asset(s)?", "Are you sure you want to save all modified assets?", "YES", "NO"))
  470. {
  471. for (int i = 0; i < m_ModifiedAssetList.Count; i++)
  472. {
  473. // Make sure all file streams that might have been opened by Unity are closed.
  474. //AssetDatabase.ReleaseCachedFileHandles();
  475. //Debug.Log("Writing asset file [" + m_ModifiedAssetList[i].assetFilePath + "].");
  476. File.WriteAllText(projectPath + "/" + m_ModifiedAssetList[i].assetFilePath, m_ModifiedAssetList[i].assetDataFile);
  477. }
  478. }
  479. AssetDatabase.Refresh();
  480. m_ProgressPercentage = 0;
  481. m_ProjectScanResults = k_ProjectScanReportDefaultText;
  482. }
  483. /// <summary>
  484. /// Check project Asset Serialization and Source Control modes
  485. /// </summary>
  486. private static bool CheckProjectSerializationAndSourceControlModes()
  487. {
  488. // Check Project Asset Serialization and Visible Meta Files mode.
  489. if (EditorSettings.serializationMode != SerializationMode.ForceText || VersionControlSettings.mode != "Visible Meta Files")
  490. {
  491. return false;
  492. }
  493. return true;
  494. }
  495. }
  496. public class TMP_ProjectConversionUtility : EditorWindow
  497. {
  498. // Create Project Files GUID Remapping Tool window
  499. [MenuItem("Window/TextMeshPro/Project Files GUID Remapping Tool", false, 2100)]
  500. static void ShowConverterWindow()
  501. {
  502. var window = GetWindow<TMP_ProjectConversionUtility>();
  503. window.titleContent = new GUIContent("Conversion Tool");
  504. window.Focus();
  505. }
  506. private static HashSet<Type> m_IgnoreAssetTypes = new HashSet<Type>()
  507. {
  508. typeof(AnimatorOverrideController),
  509. typeof(AudioClip),
  510. typeof(AvatarMask),
  511. typeof(ComputeShader),
  512. typeof(Cubemap),
  513. typeof(DefaultAsset),
  514. typeof(Flare),
  515. typeof(Font),
  516. typeof(GUISkin),
  517. typeof(HumanTemplate),
  518. typeof(LightingDataAsset),
  519. typeof(Mesh),
  520. typeof(MonoScript),
  521. typeof(PhysicsMaterialAsset),
  522. typeof(PhysicsMaterial2D),
  523. typeof(RenderTexture),
  524. typeof(Shader),
  525. typeof(TerrainData),
  526. typeof(TextAsset),
  527. typeof(Texture2D),
  528. typeof(Texture2DArray),
  529. typeof(Texture3D),
  530. typeof(UnityEditorInternal.AssemblyDefinitionAsset),
  531. typeof(UnityEngine.AI.NavMeshData),
  532. typeof(UnityEngine.Tilemaps.Tile),
  533. typeof(UnityEngine.U2D.SpriteAtlas),
  534. typeof(UnityEngine.Video.VideoClip),
  535. };
  536. /// <summary>
  537. ///
  538. /// </summary>
  539. struct AssetModificationRecord
  540. {
  541. public string assetFilePath;
  542. public string assetDataFile;
  543. }
  544. struct AssetFileRecord
  545. {
  546. public string assetFilePath;
  547. public string assetMetaFilePath;
  548. public AssetFileRecord(string filePath, string metaFilePath)
  549. {
  550. this.assetFilePath = filePath;
  551. this.assetMetaFilePath = metaFilePath;
  552. }
  553. }
  554. private static string m_ProjectPath;
  555. private static string m_ProjectFolderToScan;
  556. private static bool m_IsAlreadyScanningProject;
  557. private static bool m_CancelScanProcess;
  558. private static string k_ProjectScanReportDefaultText = "<color=#FFFF80><b>Project Scan Results</b></color>\n";
  559. private static string k_ProjectScanLabelPrefix = "Scanning: ";
  560. private static string m_ProjectScanResults = string.Empty;
  561. private static Vector2 m_ProjectScanResultScrollPosition;
  562. private static float m_ProgressPercentage = 0;
  563. private static int m_ScanningTotalFiles;
  564. private static int m_RemainingFilesToScan;
  565. private static int m_ScanningCurrentFileIndex;
  566. private static string m_ScanningCurrentFileName;
  567. private static AssetConversionData m_ConversionData;
  568. private static List<AssetModificationRecord> m_ModifiedAssetList = new List<AssetModificationRecord>();
  569. void OnEnable()
  570. {
  571. // Set Editor Window Size
  572. SetEditorWindowSize();
  573. m_ProjectScanResults = k_ProjectScanReportDefaultText;
  574. }
  575. void OnGUI()
  576. {
  577. GUILayout.BeginVertical();
  578. {
  579. // Scan project files and resources
  580. GUILayout.BeginVertical(EditorStyles.helpBox);
  581. {
  582. GUILayout.Label("Scan Project Files", EditorStyles.boldLabel);
  583. GUILayout.Label("Press the <i>Scan Project Files</i> button to begin scanning your project for files & resources that were created with a previous version of TextMesh Pro.", TMP_UIStyleManager.label);
  584. GUILayout.Space(10f);
  585. GUILayout.Label("Project folder to be scanned. Example \"Assets/TextMesh Pro\"");
  586. m_ProjectFolderToScan = EditorGUILayout.TextField("Folder Path: Assets/", m_ProjectFolderToScan);
  587. GUILayout.Space(5f);
  588. GUI.enabled = m_IsAlreadyScanningProject == false ? true : false;
  589. if (GUILayout.Button("Scan Project Files"))
  590. {
  591. m_CancelScanProcess = false;
  592. // Make sure Asset Serialization mode is set to ForceText and Version Control mode to Visible Meta Files.
  593. if (CheckProjectSerializationAndSourceControlModes() == true)
  594. {
  595. m_ProjectPath = Path.GetFullPath("Assets/..");
  596. TMP_EditorCoroutine.StartCoroutine(ScanProjectFiles());
  597. }
  598. else
  599. {
  600. EditorUtility.DisplayDialog("Project Settings Change Required", "In menu options \"Edit - Project Settings - Editor\", please change Asset Serialization Mode to ForceText and Source Control Mode to Visible Meta Files.", "OK", string.Empty);
  601. }
  602. }
  603. GUI.enabled = true;
  604. // Display progress bar
  605. Rect rect = GUILayoutUtility.GetRect(0f, 20f, GUILayout.ExpandWidth(true));
  606. EditorGUI.ProgressBar(rect, m_ProgressPercentage, "Scan Progress (" + m_ScanningCurrentFileIndex + "/" + m_ScanningTotalFiles + ")");
  607. // Display cancel button and name of file currently being scanned.
  608. if (m_IsAlreadyScanningProject)
  609. {
  610. Rect cancelRect = new Rect(rect.width - 20, rect.y + 2, 20, 16);
  611. if (GUI.Button(cancelRect, "X"))
  612. {
  613. m_CancelScanProcess = true;
  614. }
  615. GUILayout.Label(k_ProjectScanLabelPrefix + m_ScanningCurrentFileName, TMP_UIStyleManager.label);
  616. }
  617. else
  618. GUILayout.Label(string.Empty);
  619. GUILayout.Space(5);
  620. // Creation Feedback
  621. GUILayout.BeginVertical(TMP_UIStyleManager.textAreaBoxWindow, GUILayout.ExpandHeight(true));
  622. {
  623. m_ProjectScanResultScrollPosition = EditorGUILayout.BeginScrollView(m_ProjectScanResultScrollPosition, GUILayout.ExpandHeight(true));
  624. GUILayout.Label(m_ProjectScanResults, TMP_UIStyleManager.label);
  625. EditorGUILayout.EndScrollView();
  626. }
  627. GUILayout.EndVertical();
  628. GUILayout.Space(5f);
  629. }
  630. GUILayout.EndVertical();
  631. // Scan project files and resources
  632. GUILayout.BeginVertical(EditorStyles.helpBox);
  633. {
  634. GUILayout.Label("Save Modified Project Files", EditorStyles.boldLabel);
  635. GUILayout.Label("Pressing the <i>Save Modified Project Files</i> button will update the files in the <i>Project Scan Results</i> listed above. <color=#FFFF80>Please make sure that you have created a backup of your project first</color> as these file modifications are permanent and cannot be undone.", TMP_UIStyleManager.label);
  636. GUILayout.Space(5f);
  637. GUI.enabled = m_IsAlreadyScanningProject == false && m_ModifiedAssetList.Count > 0 ? true : false;
  638. if (GUILayout.Button("Save Modified Project Files"))
  639. {
  640. UpdateProjectFiles();
  641. }
  642. GUILayout.Space(10f);
  643. }
  644. GUILayout.EndVertical();
  645. }
  646. GUILayout.EndVertical();
  647. GUILayout.Space(5f);
  648. }
  649. void OnInspectorUpdate()
  650. {
  651. Repaint();
  652. }
  653. /// <summary>
  654. /// Limits the minimum size of the editor window.
  655. /// </summary>
  656. void SetEditorWindowSize()
  657. {
  658. EditorWindow editorWindow = this;
  659. Vector2 currentWindowSize = editorWindow.minSize;
  660. editorWindow.minSize = new Vector2(Mathf.Max(640, currentWindowSize.x), Mathf.Max(420, currentWindowSize.y));
  661. }
  662. /// <summary>
  663. ///
  664. /// </summary>
  665. /// <param name="filePath"></param>
  666. /// <returns></returns>
  667. private static bool ShouldIgnoreFile(string filePath)
  668. {
  669. string fileExtension = Path.GetExtension(filePath);
  670. Type fileType = AssetDatabase.GetMainAssetTypeAtPath(filePath);
  671. if (m_IgnoreAssetTypes.Contains(fileType))
  672. return true;
  673. // Exclude FBX
  674. if (fileType == typeof(GameObject) && (fileExtension.ToLower() == ".fbx" || fileExtension.ToLower() == ".blend"))
  675. return true;
  676. return false;
  677. }
  678. private IEnumerator ScanProjectFiles()
  679. {
  680. m_IsAlreadyScanningProject = true;
  681. string packageFullPath = EditorUtilities.TMP_EditorUtility.packageFullPath;
  682. // List containing assets that have been modified.
  683. m_ProjectScanResults = k_ProjectScanReportDefaultText;
  684. m_ModifiedAssetList.Clear();
  685. m_ProgressPercentage = 0;
  686. // Read Conversion Data from Json file.
  687. if (m_ConversionData == null)
  688. m_ConversionData = JsonUtility.FromJson<AssetConversionData>(File.ReadAllText(packageFullPath + "/PackageConversionData.json"));
  689. // Get list of GUIDs for assets that might contain references to previous GUIDs that require updating.
  690. string searchFolder = string.IsNullOrEmpty(m_ProjectFolderToScan) ? "Assets" : ("Assets/" + m_ProjectFolderToScan);
  691. string[] guids = AssetDatabase.FindAssets("t:Object", new string[] { searchFolder }).Distinct().ToArray();
  692. k_ProjectScanLabelPrefix = "<b>Phase 1 - Filtering:</b> ";
  693. m_ScanningTotalFiles = guids.Length;
  694. m_ScanningCurrentFileIndex = 0;
  695. List<AssetFileRecord> projectFilesToScan = new List<AssetFileRecord>();
  696. foreach (var guid in guids)
  697. {
  698. if (m_CancelScanProcess)
  699. break;
  700. string assetFilePath = AssetDatabase.GUIDToAssetPath(guid);
  701. m_ScanningCurrentFileIndex += 1;
  702. m_ScanningCurrentFileName = assetFilePath;
  703. m_ProgressPercentage = (float)m_ScanningCurrentFileIndex / m_ScanningTotalFiles;
  704. // Filter out file types we have no interest in searching
  705. if (ShouldIgnoreFile(assetFilePath))
  706. continue;
  707. string assetMetaFilePath = AssetDatabase.GetTextMetaFilePathFromAssetPath(assetFilePath);
  708. projectFilesToScan.Add(new AssetFileRecord(assetFilePath, assetMetaFilePath));
  709. yield return null;
  710. }
  711. m_RemainingFilesToScan = m_ScanningTotalFiles = projectFilesToScan.Count;
  712. k_ProjectScanLabelPrefix = "<b>Phase 2 - Scanning:</b> ";
  713. for (int i = 0; i < m_ScanningTotalFiles; i++)
  714. {
  715. if (m_CancelScanProcess)
  716. break;
  717. AssetFileRecord fileRecord = projectFilesToScan[i];
  718. ThreadPool.QueueUserWorkItem(Task =>
  719. {
  720. ScanProjectFileAsync(fileRecord);
  721. m_ScanningCurrentFileName = fileRecord.assetFilePath;
  722. int completedScans = m_ScanningTotalFiles - Interlocked.Decrement(ref m_RemainingFilesToScan);
  723. m_ScanningCurrentFileIndex = completedScans;
  724. m_ProgressPercentage = (float)completedScans / m_ScanningTotalFiles;
  725. });
  726. if (i % 64 == 0)
  727. yield return new WaitForSeconds(2.0f);
  728. }
  729. while (m_RemainingFilesToScan > 0 && !m_CancelScanProcess)
  730. yield return null;
  731. m_IsAlreadyScanningProject = false;
  732. m_ScanningCurrentFileName = string.Empty;
  733. }
  734. static void ScanProjectFileAsync(AssetFileRecord fileRecord)
  735. {
  736. if (m_CancelScanProcess)
  737. return;
  738. // Read the asset data file
  739. string assetDataFile = string.Empty;
  740. bool hasFileChanged = false;
  741. try
  742. {
  743. assetDataFile = File.ReadAllText(m_ProjectPath + "/" + fileRecord.assetFilePath);
  744. }
  745. catch
  746. {
  747. // Continue to the next asset if we can't read the current one.
  748. return;
  749. }
  750. // Read the asset meta data file
  751. string assetMetaFile = File.ReadAllText(m_ProjectPath + "/" + fileRecord.assetMetaFilePath);
  752. bool hasMetaFileChanges = false;
  753. foreach (AssetConversionRecord record in m_ConversionData.assetRecords)
  754. {
  755. if (assetDataFile.Contains(record.target))
  756. {
  757. hasFileChanged = true;
  758. assetDataFile = assetDataFile.Replace(record.target, record.replacement);
  759. }
  760. //// Check meta file
  761. if (assetMetaFile.Contains(record.target))
  762. {
  763. hasMetaFileChanges = true;
  764. assetMetaFile = assetMetaFile.Replace(record.target, record.replacement);
  765. }
  766. }
  767. if (hasFileChanged)
  768. {
  769. AssetModificationRecord modifiedAsset;
  770. modifiedAsset.assetFilePath = fileRecord.assetFilePath;
  771. modifiedAsset.assetDataFile = assetDataFile;
  772. m_ModifiedAssetList.Add(modifiedAsset);
  773. m_ProjectScanResults += fileRecord.assetFilePath + "\n";
  774. }
  775. if (hasMetaFileChanges)
  776. {
  777. AssetModificationRecord modifiedAsset;
  778. modifiedAsset.assetFilePath = fileRecord.assetMetaFilePath;
  779. modifiedAsset.assetDataFile = assetMetaFile;
  780. m_ModifiedAssetList.Add(modifiedAsset);
  781. m_ProjectScanResults += fileRecord.assetMetaFilePath + "\n";
  782. }
  783. }
  784. /// <summary>
  785. ///
  786. /// </summary>
  787. private static void ResetScanProcess()
  788. {
  789. m_IsAlreadyScanningProject = false;
  790. m_ScanningCurrentFileName = string.Empty;
  791. m_ProgressPercentage = 0;
  792. m_ScanningCurrentFileIndex = 0;
  793. m_ScanningTotalFiles = 0;
  794. }
  795. /// <summary>
  796. ///
  797. /// </summary>
  798. private static void UpdateProjectFiles()
  799. {
  800. // Make sure Asset Serialization mode is set to ForceText with Visible Meta Files.
  801. CheckProjectSerializationAndSourceControlModes();
  802. string projectPath = Path.GetFullPath("Assets/..");
  803. // Display dialogue to show user a list of project files that will be modified upon their consent.
  804. if (EditorUtility.DisplayDialog("Save Modified Asset(s)?", "Are you sure you want to save all modified assets?", "YES", "NO"))
  805. {
  806. for (int i = 0; i < m_ModifiedAssetList.Count; i++)
  807. {
  808. // Make sure all file streams that might have been opened by Unity are closed.
  809. //AssetDatabase.ReleaseCachedFileHandles();
  810. //Debug.Log("Writing asset file [" + m_ModifiedAssetList[i].assetFilePath + "].");
  811. File.WriteAllText(projectPath + "/" + m_ModifiedAssetList[i].assetFilePath, m_ModifiedAssetList[i].assetDataFile);
  812. }
  813. }
  814. AssetDatabase.Refresh();
  815. m_ProgressPercentage = 0;
  816. m_ProjectScanResults = k_ProjectScanReportDefaultText;
  817. }
  818. /// <summary>
  819. /// Check project Asset Serialization and Source Control modes
  820. /// </summary>
  821. private static bool CheckProjectSerializationAndSourceControlModes()
  822. {
  823. // Check Project Asset Serialization and Visible Meta Files mode.
  824. if (EditorSettings.serializationMode != SerializationMode.ForceText || VersionControlSettings.mode != "Visible Meta Files")
  825. {
  826. return false;
  827. }
  828. return true;
  829. }
  830. }
  831. public class TMP_PackageUtilities : Editor
  832. {
  833. /// <summary>
  834. ///
  835. /// </summary>
  836. [MenuItem("Window/TextMeshPro/Import TMP Essential Resources", false, 2050)]
  837. public static void ImportProjectResourcesMenu()
  838. {
  839. ImportEssentialResources();
  840. }
  841. /// <summary>
  842. ///
  843. /// </summary>
  844. [MenuItem("Window/TextMeshPro/Import TMP Examples and Extras", false, 2051)]
  845. public static void ImportExamplesContentMenu()
  846. {
  847. ImportExamplesAndExtras();
  848. }
  849. /// <summary>
  850. ///
  851. /// </summary>
  852. private static void ImportExamplesAndExtras()
  853. {
  854. string packageFullPath = TMP_EditorUtility.packageFullPath;
  855. AssetDatabase.ImportPackage(packageFullPath + "/Package Resources/TMP Examples & Extras.unitypackage", true);
  856. }
  857. private static string k_SettingsFilePath;
  858. private static byte[] k_SettingsBackup;
  859. /// <summary>
  860. ///
  861. /// </summary>
  862. private static void ImportEssentialResources()
  863. {
  864. // Check if the TMP Settings asset is already present in the project.
  865. string[] settings = AssetDatabase.FindAssets("t:TMP_Settings");
  866. if (settings.Length > 0)
  867. {
  868. // Save assets just in case the TMP Setting were modified before import.
  869. AssetDatabase.SaveAssets();
  870. // Copy existing TMP Settings asset to a byte[]
  871. k_SettingsFilePath = AssetDatabase.GUIDToAssetPath(settings[0]);
  872. k_SettingsBackup = File.ReadAllBytes(k_SettingsFilePath);
  873. RegisterResourceImportCallback();
  874. }
  875. string packageFullPath = TMP_EditorUtility.packageFullPath;
  876. AssetDatabase.ImportPackage(packageFullPath + "/Package Resources/TMP Essential Resources.unitypackage", true);
  877. }
  878. internal static void RegisterResourceImportCallback()
  879. {
  880. AssetDatabase.importPackageCompleted += ImportCallback;
  881. }
  882. private static void ImportCallback(string packageName)
  883. {
  884. // Restore backup of TMP Settings from byte[]
  885. File.WriteAllBytes(k_SettingsFilePath, k_SettingsBackup);
  886. AssetDatabase.Refresh();
  887. TMP_Settings.instance.SetAssetVersion();
  888. EditorUtility.SetDirty(TMP_Settings.instance);
  889. AssetDatabase.SaveAssetIfDirty(TMP_Settings.instance);
  890. AssetDatabase.importPackageCompleted -= ImportCallback;
  891. }
  892. }
  893. }