Skip to content

Instantly share code, notes, and snippets.

@GFX47
Created May 22, 2018 15:30
Show Gist options
  • Save GFX47/bbbf7a269ef4464bea148617523a9780 to your computer and use it in GitHub Desktop.
Save GFX47/bbbf7a269ef4464bea148617523a9780 to your computer and use it in GitHub Desktop.
Unity3D - Select assets
using UnityEditor;
using UnityEngine;
public class EditorTools
{
public static Object GetAsset(string name = null, string type = null)
{
string search = string.Empty;
if (!string.IsNullOrEmpty(name))
{
search += name;
}
if (!string.IsNullOrEmpty(type))
{
search = string.Format("{0} t:{1}", search, type);
}
string[] guids = AssetDatabase.FindAssets(search);
if (guids == null || guids.Length < 1)
{
return null;
}
string guid = guids[0];
string path = AssetDatabase.GUIDToAssetPath(guid);
Object asset = AssetDatabase.LoadMainAssetAtPath(path);
return asset;
}
public static void SelectAsset(string name = null, string type = null)
{
Object asset = EditorTools.GetAsset(name, type);
if (asset != null)
{
EditorTools.SelectAsset(asset);
}
}
public static void SelectAsset(Object asset)
{
Selection.activeObject = asset;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment