XChartsPackageResourceImporter.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace XCharts
  7. {
  8. [System.Serializable]
  9. public class XChartsPackageResourceImporter
  10. {
  11. bool m_EssentialResourcesImported;
  12. public XChartsPackageResourceImporter() { }
  13. public void OnDestroy()
  14. {
  15. }
  16. public void OnGUI()
  17. {
  18. m_EssentialResourcesImported = File.Exists("Assets/XCharts/Resources/XChartsSettings.asset");
  19. GUILayout.BeginVertical();
  20. {
  21. GUILayout.BeginVertical(EditorStyles.helpBox);
  22. {
  23. GUILayout.Label("XCharts Essentials", EditorStyles.boldLabel);
  24. GUILayout.Label("This appears to be the first time you access XCharts, as such we need to add resources to your project that are essential for using XCharts. These new resources will be placed at the root of your project in the \"XCharts\" folder.", new GUIStyle(EditorStyles.label) { wordWrap = true });
  25. GUILayout.Space(5f);
  26. GUI.enabled = !m_EssentialResourcesImported;
  27. if (GUILayout.Button("Import XCharts Essentials"))
  28. {
  29. string packageFullPath = GetPackageFullPath();
  30. var sourPath = Path.Combine(packageFullPath, "Resources");
  31. var destPath = Path.Combine(Application.dataPath, "XCharts/Resources");
  32. if (CopyFolder(sourPath, destPath))
  33. {
  34. AssetDatabase.SaveAssets();
  35. AssetDatabase.Refresh();
  36. }
  37. }
  38. GUILayout.Space(5f);
  39. GUI.enabled = true;
  40. }
  41. GUILayout.EndVertical();
  42. }
  43. GUILayout.EndVertical();
  44. GUILayout.Space(5f);
  45. }
  46. private static bool CopyFolder(string sourPath, string destPath)
  47. {
  48. try
  49. {
  50. if (!Directory.Exists(destPath))
  51. {
  52. Directory.CreateDirectory(destPath);
  53. }
  54. var files = Directory.GetFiles(sourPath);
  55. foreach (var file in files)
  56. {
  57. var name = Path.GetFileName(file);
  58. var path = Path.Combine(destPath, name);
  59. File.Copy(file, path);
  60. }
  61. var folders = Directory.GetDirectories(sourPath);
  62. foreach (var folder in folders)
  63. {
  64. var name = Path.GetFileName(folder);
  65. var path = Path.Combine(destPath, name);
  66. CopyFolder(folder, path);
  67. }
  68. return true;
  69. }
  70. catch (Exception e)
  71. {
  72. Debug.LogError("CopyFolder:" + e.Message);
  73. return false;
  74. }
  75. }
  76. internal void RegisterResourceImportCallback()
  77. {
  78. AssetDatabase.importPackageCompleted += ImportCallback;
  79. }
  80. /// <summary>
  81. ///
  82. /// </summary>
  83. /// <param name="packageName"></param>
  84. void ImportCallback(string packageName)
  85. {
  86. if (packageName == "XCharts Essential Resources")
  87. {
  88. m_EssentialResourcesImported = true;
  89. #if UNITY_2018_3_OR_NEWER
  90. SettingsService.NotifySettingsProviderChanged();
  91. #endif
  92. }
  93. Debug.Log("[" + packageName + "] have been imported.");
  94. AssetDatabase.importPackageCompleted -= ImportCallback;
  95. }
  96. static string GetPackageFullPath()
  97. {
  98. // Check for potential UPM package
  99. string packagePath = Path.GetFullPath("Packages/com.monitor1394.xcharts");
  100. if (Directory.Exists(packagePath))
  101. {
  102. return packagePath;
  103. }
  104. packagePath = Path.GetFullPath("Assets/..");
  105. if (Directory.Exists(packagePath))
  106. {
  107. // Search default location for development package
  108. if (Directory.Exists(packagePath + "/Assets/Packages/com.monitor1394.xcharts/Package Resources"))
  109. {
  110. return packagePath + "/Assets/Packages/com.monitor1394.xcharts";
  111. }
  112. // Search for default location of normal XCharts AssetStore package
  113. if (Directory.Exists(packagePath + "/Assets/XCharts/Package Resources"))
  114. {
  115. return packagePath + "/Assets/XCharts";
  116. }
  117. // Search for potential alternative locations in the user project
  118. string[] matchingPaths = Directory.GetDirectories(packagePath, "XCharts", SearchOption.AllDirectories);
  119. string path = ValidateLocation(matchingPaths, packagePath);
  120. if (path != null) return packagePath + path;
  121. }
  122. return null;
  123. }
  124. static string ValidateLocation(string[] paths, string projectPath)
  125. {
  126. for (int i = 0; i < paths.Length; i++)
  127. {
  128. // Check if the Editor Resources folder exists.
  129. if (Directory.Exists(paths[i] + "/Package Resources"))
  130. {
  131. string folderPath = paths[i].Replace(projectPath, "");
  132. folderPath = folderPath.TrimStart('\\', '/');
  133. return folderPath;
  134. }
  135. }
  136. return null;
  137. }
  138. }
  139. public class XChartsPackageResourceImporterWindow : EditorWindow
  140. {
  141. [SerializeField]
  142. XChartsPackageResourceImporter m_ResourceImporter;
  143. static XChartsPackageResourceImporterWindow m_ImporterWindow;
  144. public static void ShowPackageImporterWindow()
  145. {
  146. if (m_ImporterWindow == null)
  147. {
  148. m_ImporterWindow = GetWindow<XChartsPackageResourceImporterWindow>();
  149. m_ImporterWindow.titleContent = new GUIContent("XCharts Importer");
  150. }
  151. m_ImporterWindow.Focus();
  152. }
  153. void OnEnable()
  154. {
  155. SetEditorWindowSize();
  156. if (m_ResourceImporter == null)
  157. m_ResourceImporter = new XChartsPackageResourceImporter();
  158. }
  159. void OnDestroy()
  160. {
  161. m_ResourceImporter.OnDestroy();
  162. }
  163. void OnGUI()
  164. {
  165. m_ResourceImporter.OnGUI();
  166. }
  167. void OnInspectorUpdate()
  168. {
  169. Repaint();
  170. }
  171. /// <summary>
  172. /// Limits the minimum size of the editor window.
  173. /// </summary>
  174. void SetEditorWindowSize()
  175. {
  176. EditorWindow editorWindow = this;
  177. Vector2 windowSize = new Vector2(640, 210);
  178. editorWindow.minSize = windowSize;
  179. editorWindow.maxSize = windowSize;
  180. }
  181. }
  182. }
  183. #endif