Skip to content

Instantly share code, notes, and snippets.

@psuong
Last active September 7, 2019 20:41
Show Gist options
  • Save psuong/58d96996fb96cb99baf78b9a5d486b4e to your computer and use it in GitHub Desktop.
Save psuong/58d96996fb96cb99baf78b9a5d486b4e to your computer and use it in GitHub Desktop.
Prefab Utility to Instantiate a Test Prefab
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