This commit is contained in:
monitor1394
2022-03-27 12:49:20 +08:00
parent 3bccbd0ab2
commit acba2f5c1e
17 changed files with 111 additions and 119 deletions

View File

@@ -21,8 +21,7 @@ namespace XCharts.Runtime
public void OnGUI()
{
m_EssentialResourcesImported = Resources.Load<XCSettings>("XCSettings") != null ||
XCSettings.ExistAssetFile();
m_EssentialResourcesImported = Resources.Load<XCSettings>("XCSettings") != null;
GUILayout.BeginVertical();
{
@@ -33,15 +32,19 @@ namespace XCharts.Runtime
GUILayout.Space(5f);
GUI.enabled = !m_EssentialResourcesImported;
GUI.enabled = true;
if (GUILayout.Button("Import XCharts Essentials"))
{
string packageFullPath = GetPackageFullPath();
var sourPath = Path.Combine(packageFullPath, "Resources");
var destPath = Path.Combine(Application.dataPath, "XCharts/Resources");
if (RuntimeUtil.CopyFolder(sourPath, destPath))
string packageFullPath = XChartsMgr.GetPackageFullPath();
if (packageFullPath != null)
{
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
var sourPath = Path.Combine(packageFullPath, "Resources");
var destPath = Path.Combine(Application.dataPath, "XCharts/Resources");
if (RuntimeUtil.CopyFolder(sourPath, destPath))
{
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
}
GUILayout.Space(5f);
@@ -75,55 +78,6 @@ namespace XCharts.Runtime
AssetDatabase.importPackageCompleted -= ImportCallback;
}
static string GetPackageFullPath()
{
// Check for potential UPM package
string packagePath = Path.GetFullPath("Packages/com.monitor1394.xcharts");
if (Directory.Exists(packagePath))
{
return packagePath;
}
packagePath = Path.GetFullPath("Assets/..");
if (Directory.Exists(packagePath))
{
// Search default location for development package
if (Directory.Exists(packagePath + "/Assets/Packages/com.monitor1394.xcharts/Resources"))
{
return packagePath + "/Assets/Packages/com.monitor1394.xcharts";
}
// Search for default location of normal XCharts AssetStore package
if (Directory.Exists(packagePath + "/Assets/XCharts/Resources"))
{
return packagePath + "/Assets/XCharts";
}
// Search for potential alternative locations in the user project
string[] matchingPaths = Directory.GetDirectories(packagePath, "XCharts", SearchOption.AllDirectories);
string path = ValidateLocation(matchingPaths, packagePath);
if (path != null) return packagePath + path;
}
return null;
}
static string ValidateLocation(string[] paths, string projectPath)
{
for (int i = 0; i < paths.Length; i++)
{
// Check if the Editor Resources folder exists.
if (Directory.Exists(paths[i] + "/Package Resources"))
{
string folderPath = paths[i].Replace(projectPath, "");
folderPath = folderPath.TrimStart('\\', '/');
return folderPath;
}
}
return null;
}
}
public class XCResourceImporterWindow : UnityEditor.EditorWindow
@@ -134,13 +88,16 @@ namespace XCharts.Runtime
public static void ShowPackageImporterWindow()
{
if (m_ImporterWindow == null)
var packagePath = XChartsMgr.GetPackageFullPath();
if (packagePath != null)
{
m_ImporterWindow = GetWindow<XCResourceImporterWindow>();
m_ImporterWindow.titleContent = new GUIContent("XCharts Importer");
if (m_ImporterWindow == null)
{
m_ImporterWindow = GetWindow<XCResourceImporterWindow>();
m_ImporterWindow.titleContent = new GUIContent("XCharts Importer");
}
m_ImporterWindow.Focus();
}
m_ImporterWindow.Focus();
}
void OnEnable()

View File

@@ -2,6 +2,7 @@
using UnityEngine;
using System;
using System.Collections.Generic;
using System.IO;
#if dUI_TextMeshPro
using TMPro;
#endif
@@ -129,8 +130,11 @@ namespace XCharts.Runtime
#if UNITY_EDITOR
if (s_Instance == null)
{
if (!ExistAssetFile())
var assetPath = GetSettingAssetPath();
if (string.IsNullOrEmpty(assetPath))
XCResourceImporterWindow.ShowPackageImporterWindow();
else
s_Instance = AssetDatabase.LoadAssetAtPath<XCSettings>(assetPath);
}
else
{
@@ -156,6 +160,32 @@ namespace XCharts.Runtime
return System.IO.File.Exists("Assets/XCharts/Resources/XCSettings.asset");
}
public static string GetSettingAssetPath()
{
var path = "Assets/XCharts/Resources/XCSettings.asset";
if (File.Exists(path)) return path;
var dir = Application.dataPath;
string[] matchingPaths = Directory.GetDirectories(dir);
foreach (var match in matchingPaths)
{
if (match.Contains("XCharts"))
{
var jsonPath = string.Format("{0}/package.json", match);
if (File.Exists(jsonPath))
{
var jsonText = File.ReadAllText(jsonPath);
if (jsonText.Contains("\"displayName\": \"XCharts\""))
{
path = string.Format("{0}/Resources/XCSettings.asset", match);
if (File.Exists(path))
return path.Substring(path.IndexOf("/Assets/") + 1);
}
}
}
}
return null;
}
public static bool AddCustomTheme(Theme theme)
{
if (theme == null) return false;

View File

@@ -109,24 +109,20 @@ namespace XCharts.Runtime
packagePath = Path.GetFullPath("Assets/..");
if (Directory.Exists(packagePath))
{
// Search default location for development package
if (File.Exists(packagePath + "/Assets/Packages/com.monitor1394.xcharts/package.json"))
if (File.Exists(packagePath + "/Assets/Packages/XCharts/package.json"))
{
return packagePath + "/Assets/Packages/com.monitor1394.xcharts";
return packagePath + "/Assets/Packages/XCharts";
}
// Search for default location of normal XCharts AssetStore package
if (File.Exists(packagePath + "/Assets/XCharts/package.json"))
{
return packagePath + "/Assets/XCharts";
}
// Search for potential alternative locations in the user project
string[] matchingPaths = Directory.GetDirectories(packagePath, "XCharts", SearchOption.AllDirectories);
string path = ValidateLocation(matchingPaths, packagePath);
if (path != null) return Path.Combine(packagePath, path);
}
return null;
}
@@ -141,7 +137,6 @@ namespace XCharts.Runtime
return folderPath;
}
}
return null;
}