Created
May 22, 2018 15:30
-
-
Save GFX47/bbbf7a269ef4464bea148617523a9780 to your computer and use it in GitHub Desktop.
Unity3D - Select assets
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 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