Last active
September 7, 2019 20:41
-
-
Save psuong/58d96996fb96cb99baf78b9a5d486b4e to your computer and use it in GitHub Desktop.
Prefab Utility to Instantiate a Test Prefab
This file contains hidden or 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 static class PrefabUtils { | |
public static T GetInstantiatedAsset<T>(string searchParams, string folder) where T : Object { | |
var guids = AssetDatabase.FindAssets(searchParams, new [] { folder }); | |
if (guids.Length != 1) { | |
throw new System.InvalidOperationException( | |
$"Found {guids.Length} assets, please narrow down the search result!"); | |
} | |
var path = AssetDatabase.GUIDToAssetPath(guids[0]); | |
return Object.Instantiate(AssetDatabase.LoadAssetAtPath(path, typeof(T)) as T); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment