Created
December 28, 2019 01:51
-
-
Save JesseTG/c54f63a9363f4e975e988236a19d0a56 to your computer and use it in GitHub Desktop.
List all assets of a given type for Minibuffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Text; | |
using DesperateDevs.Utils; | |
using SeawispHunter.MinibufferConsole; | |
using SeawispHunter.MinibufferConsole.Extensions; | |
using Sirenix.Utilities; | |
using UnityEngine; | |
namespace CorundumGames.Chromavaders.Behaviours.MinibufferConsole | |
{ | |
[DisallowMultipleComponent] | |
[AddComponentMenu("Chromavaders/Minibuffer Commands/Asset Commands")] | |
public sealed class AssetCommands : MonoBehaviour | |
{ | |
private StringBuilder builder; | |
private const string AssetTypes = "AssetTypes"; | |
void Start() | |
{ | |
builder = new StringBuilder(4096); | |
Minibuffer.Register(this); | |
Minibuffer.With(m => | |
{ | |
var types = AppDomain | |
.CurrentDomain | |
.GetAllTypes() | |
.Where(t => typeof(UnityEngine.Object).IsAssignableFrom(t)) | |
.ToArray() | |
; | |
var listCompleter = new ListCompleter<Type>(types).ToEntity(); | |
m.completers[AssetTypes] = listCompleter; | |
}); | |
} | |
[Command(description = "List all assets of the given type")] | |
public void ListAssets([Prompt(completer = AssetTypes)] Type type) | |
{ | |
if (typeof(UnityEngine.Object).IsAssignableFrom(type)) | |
{ | |
builder.Clear(); | |
var bufferName = $"Asset List ({type})"; | |
Minibuffer.instance.ClearBuffer(bufferName); | |
var resources = Resources.FindObjectsOfTypeAll(type); | |
resources | |
.OrderBy(o => o.name) | |
.ForEach(o => | |
{ | |
builder.Append('"'); | |
builder.Append(o.name); | |
builder.Append("\" (ID: "); | |
builder.Append(o.GetInstanceID()); | |
builder.Append(")\n"); | |
}) | |
; | |
Minibuffer.instance.ToBuffer(bufferName, builder.ToString()); | |
Minibuffer.instance.Message($"{resources.Length} instances of {type.FullName} found"); | |
} | |
else | |
{ | |
Minibuffer.instance.Message($"{type.FullName} is not a Unity asset type"); | |
} | |
} | |
void OnDestroy() | |
{ | |
Minibuffer.Unregister(this); | |
} | |
/* Include this line to show the commands in the inspector. */ | |
public MinibufferListing minibufferExtensions; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment