using System; using System.IO; using UnityEngine; #if UNITY_EDITOR using System.Collections.Generic; using UnityEditor; #endif namespace UNSDK { public static class AssetTools { /// /// Returns a reference to a scriptable object of type T with the given fileName at the relative resourcesPath. /// If the asset is not found, one will get created automatically (in the Editor only) /// /// /// /// /// /// /// public static T GetScriptableObject(string fileName, string resourcesPath, bool saveAssetDatabase, bool refreshAssetDatabase) where T : ScriptableObject { if (string.IsNullOrEmpty(resourcesPath)) return null; if (string.IsNullOrEmpty(fileName)) return null; // ReSharper disable once SuspiciousTypeConversion.Global // if (!resourcesPath[resourcesPath.Length - 1].Equals(@"\")) resourcesPath += @"\"; // resourcesPath = resourcesPath.Replace(@"\", "/"); resourcesPath = CleanPath(resourcesPath); var obj = (T)Resources.Load(fileName, typeof(T)); if (obj == null) { string simpleResourcesPath = resourcesPath.Replace(resourcesPath.Substring(0, resourcesPath.LastIndexOf("Resources", StringComparison.Ordinal)), ""); simpleResourcesPath = simpleResourcesPath.Replace("Resources", "").Remove(0, 1); obj = (T)Resources.Load(Path.Combine(simpleResourcesPath, fileName), typeof(T)); } #if UNITY_EDITOR if (obj != null) return obj; if (!Directory.Exists("Assets/Resources")) { Directory.CreateDirectory("Assets/Resources"); } obj = CreateAsset(resourcesPath, fileName, ".asset", saveAssetDatabase, refreshAssetDatabase); #endif return obj; } public static T GetResource(string resourcesPath, string fileName) where T : ScriptableObject { if (string.IsNullOrEmpty(resourcesPath)) return null; if (string.IsNullOrEmpty(fileName)) return null; resourcesPath = CleanPath(resourcesPath); // ReSharper disable once SuspiciousTypeConversion.Global // if (!resourcesPath[resourcesPath.Length - 1].Equals(@"\")) resourcesPath += @"\"; // resourcesPath = resourcesPath.Replace(@"\", "/"); return (T)Resources.Load(resourcesPath + fileName, typeof(T)); } public static string CleanPath(string path) { // ReSharper disable once SuspiciousTypeConversion.Global if (!path[path.Length - 1].Equals(@"\")) path += @"\"; path = path.Replace(@"\\", @"\"); path = path.Replace(@"\", "/"); return path; } #if UNITY_EDITOR public static T CreateAsset(string relativePath, string fileName, string extension = ".asset", bool saveAssetDatabase = true, bool refreshAssetDatabase = true) where T : ScriptableObject { if (string.IsNullOrEmpty(relativePath)) return null; if (string.IsNullOrEmpty(fileName)) return null; relativePath = CleanPath(relativePath); // ReSharper disable once SuspiciousTypeConversion.Global // if (!relativePath[relativePath.Length - 1].Equals(@"\")) relativePath += @"\"; // relativePath = relativePath.Replace(@"\\", @"\"); var asset = ScriptableObject.CreateInstance(); AssetDatabase.CreateAsset(asset, relativePath + fileName + extension); EditorUtility.SetDirty(asset); if (saveAssetDatabase) AssetDatabase.SaveAssets(); if (refreshAssetDatabase) AssetDatabase.Refresh(); return asset; } public static List GetAssets() where T : ScriptableObject { var list = new List(); string[] guids = AssetDatabase.FindAssets("t:" + typeof(T).Name); foreach (string guid in guids) { var asset = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid)); if (asset == null) continue; list.Add(asset); } return list; } public static void MoveAssetToTrash(string relativePath, string fileName, bool saveAssetDatabase = true, bool refreshAssetDatabase = true, bool printDebugMessage = true) { if (string.IsNullOrEmpty(relativePath)) return; if (string.IsNullOrEmpty(fileName)) return; // ReSharper disable once SuspiciousTypeConversion.Global // if (!relativePath[relativePath.Length - 1].Equals(@"\")) relativePath += @"\"; relativePath = CleanPath(relativePath); if (!AssetDatabase.MoveAssetToTrash(relativePath + fileName + ".asset")) return; if (printDebugMessage) Debug.Log("The " + fileName + ".asset file has been moved to trash."); if (saveAssetDatabase) AssetDatabase.SaveAssets(); if (refreshAssetDatabase) AssetDatabase.Refresh(); } public static Texture GetTexture(string filePath, string fileName, string fileExtension = ".png") { if (string.IsNullOrEmpty(filePath)) return null; if (string.IsNullOrEmpty(fileName)) return null; // ReSharper disable once SuspiciousTypeConversion.Global // if (!filePath[filePath.Length - 1].Equals(@"\")) filePath += @"\"; filePath = CleanPath(filePath); return AssetDatabase.LoadAssetAtPath(filePath + fileName + fileExtension); } public static Texture2D GetTexture2D(string filePath, string fileName, string fileExtension = ".png") { if (string.IsNullOrEmpty(filePath)) return null; if (string.IsNullOrEmpty(fileName)) return null; // ReSharper disable once SuspiciousTypeConversion.Global // if (!filePath[filePath.Length - 1].Equals(@"\")) filePath += @"\"; filePath = CleanPath(filePath); return AssetDatabase.LoadAssetAtPath(filePath + fileName + fileExtension); } #endif } }