From 6c31a6b91ef7288100e692e0ea8e12fa72bd528d Mon Sep 17 00:00:00 2001 From: OpOpYaDev Date: Tue, 27 Feb 2024 14:44:14 +0000 Subject: [PATCH] Merge pull request #56 from techno-dwarf-works/dev Version: 1.5.1 --- Runtime/Utility/AssetDatabaseUtility.cs | 30 +++++------ Runtime/Utility/ScriptableObjectUtility.cs | 54 +++++++++++++++++++ .../Utility/ScriptableObjectUtility.cs.meta | 3 ++ package.json | 2 +- 4 files changed, 70 insertions(+), 19 deletions(-) create mode 100644 Runtime/Utility/ScriptableObjectUtility.cs create mode 100644 Runtime/Utility/ScriptableObjectUtility.cs.meta diff --git a/Runtime/Utility/AssetDatabaseUtility.cs b/Runtime/Utility/AssetDatabaseUtility.cs index 12fc466..d787627 100644 --- a/Runtime/Utility/AssetDatabaseUtility.cs +++ b/Runtime/Utility/AssetDatabaseUtility.cs @@ -1,15 +1,16 @@ -using System; using System.Collections.Generic; using UnityEditor; using UnityEngine; -using Object = UnityEngine.Object; namespace Better.Extensions.Runtime { public static class AssetDatabaseUtility { +#if UNITY_EDITOR + public const string AssetsPath = "Assets"; + public const string AssetExtension = ".asset"; + private const string PrefabFilter = "t:prefab"; - private const string ScriptableObjectFilter = "t:ScriptableObject"; public static T[] FindPrefabsOfType() { @@ -31,22 +32,9 @@ public static T[] FindPrefabsOfType() return prefabs.ToArray(); } - public static T[] FindScriptableObjectsOfType() where T : ScriptableObject - { - return FindAssetsOfType(ScriptableObjectFilter); - } - public static T[] FindAssetsOfType(string filter) where T : Object { - if (filter.IsNullOrEmpty() || filter.IsNullOrWhiteSpace()) - { - var message = $"{nameof(filter)} cannot be Null or Empty"; - DebugUtility.LogException(message); - return Array.Empty(); - } - var assets = new List(); -#if UNITY_EDITOR var guids = AssetDatabase.FindAssets(filter); foreach (var guid in guids) @@ -60,9 +48,15 @@ public static T[] FindAssetsOfType(string filter) where T : Object } } -#endif - return assets.ToArray(); } + + public static T[] FindAssetsOfType() where T : Object + { + var typeName = typeof(T).Name; + var filter = $"t:{typeName}"; + return FindAssetsOfType(filter); + } +#endif } } \ No newline at end of file diff --git a/Runtime/Utility/ScriptableObjectUtility.cs b/Runtime/Utility/ScriptableObjectUtility.cs new file mode 100644 index 0000000..ff7ea08 --- /dev/null +++ b/Runtime/Utility/ScriptableObjectUtility.cs @@ -0,0 +1,54 @@ +using System.IO; +using UnityEditor; +using UnityEngine; + +namespace Better.Extensions.Runtime +{ + public static class ScriptableObjectUtility + { +#if UNITY_EDITOR + public static T CreateScriptableObjectAsset(string path) + where T : ScriptableObject + { + const string assetsPath = AssetDatabaseUtility.AssetsPath; + const string assetExtension = AssetDatabaseUtility.AssetExtension; + + var lastFolder = Path.GetDirectoryName(path); + lastFolder ??= string.Empty; + if (lastFolder.StartsWith(assetsPath)) + { + lastFolder = lastFolder.Remove(0, assetsPath.Length); + } + + var absolutePath = Path.Combine(Application.dataPath, lastFolder); + if (!Directory.Exists(absolutePath)) + { + Directory.CreateDirectory(absolutePath); + AssetDatabase.Refresh(ImportAssetOptions.Default); + } + + if (!path.StartsWith(assetsPath)) + { + path = Path.Combine(assetsPath, path); + } + + if (!path.EndsWith(assetExtension)) + { + var name = typeof(T).Name; + path = Path.Combine(path, $"{name}{assetExtension}"); + } + + var asset = ScriptableObject.CreateInstance(); + AssetDatabase.CreateAsset(asset, path); + return asset; + } + + public static T CreateScriptableObjectAsset(params string[] pathParts) + where T : ScriptableObject + { + var path = Path.Combine(pathParts); + return CreateScriptableObjectAsset(path: path); + } +#endif + } +} \ No newline at end of file diff --git a/Runtime/Utility/ScriptableObjectUtility.cs.meta b/Runtime/Utility/ScriptableObjectUtility.cs.meta new file mode 100644 index 0000000..f7b7785 --- /dev/null +++ b/Runtime/Utility/ScriptableObjectUtility.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 99b275ebb9ff4d209ecb7ff42f3ae1dd +timeCreated: 1709043640 \ No newline at end of file diff --git a/package.json b/package.json index 0eca01c..a9e9640 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "com.uurha.betterextensions", "displayName": "Better Extensions", "description": "Unity extensions, serialize extension, async extension, string extension and UI extensions", - "version": "1.5.0", + "version": "1.5.1", "unity": "2020.1", "author": { "name": "Better Plugins",