Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #56 from techno-dwarf-works/dev
Browse files Browse the repository at this point in the history
Version: 1.5.1
  • Loading branch information
OpOpYaDev committed Feb 27, 2024
1 parent 50f1278 commit 6c31a6b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 19 deletions.
30 changes: 12 additions & 18 deletions Runtime/Utility/AssetDatabaseUtility.cs
Original file line number Diff line number Diff line change
@@ -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<T>()
{
Expand All @@ -31,22 +32,9 @@ public static T[] FindPrefabsOfType<T>()
return prefabs.ToArray();
}

public static T[] FindScriptableObjectsOfType<T>() where T : ScriptableObject
{
return FindAssetsOfType<T>(ScriptableObjectFilter);
}

public static T[] FindAssetsOfType<T>(string filter) where T : Object
{
if (filter.IsNullOrEmpty() || filter.IsNullOrWhiteSpace())
{
var message = $"{nameof(filter)} cannot be Null or Empty";
DebugUtility.LogException<ArgumentException>(message);
return Array.Empty<T>();
}

var assets = new List<T>();
#if UNITY_EDITOR
var guids = AssetDatabase.FindAssets(filter);

foreach (var guid in guids)
Expand All @@ -60,9 +48,15 @@ public static T[] FindAssetsOfType<T>(string filter) where T : Object
}
}

#endif

return assets.ToArray();
}

public static T[] FindAssetsOfType<T>() where T : Object
{
var typeName = typeof(T).Name;
var filter = $"t:{typeName}";
return FindAssetsOfType<T>(filter);
}
#endif
}
}
54 changes: 54 additions & 0 deletions Runtime/Utility/ScriptableObjectUtility.cs
Original file line number Diff line number Diff line change
@@ -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<T>(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<T>();
AssetDatabase.CreateAsset(asset, path);
return asset;
}

public static T CreateScriptableObjectAsset<T>(params string[] pathParts)
where T : ScriptableObject
{
var path = Path.Combine(pathParts);
return CreateScriptableObjectAsset<T>(path: path);
}
#endif
}
}
3 changes: 3 additions & 0 deletions Runtime/Utility/ScriptableObjectUtility.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 6c31a6b

Please sign in to comment.