-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddToHierarchy.cs
54 lines (44 loc) · 1.64 KB
/
AddToHierarchy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using UnityEngine;
using UnityEditor;
// Simple script I use to spawn prefabs via Toolbar menu items.
namespace BUDDYWORKS.Scripts.AddToHierarchy
{
public class InstantiateAndPingPrefabByGUID : MonoBehaviour
{
public static string prefabGUID = "INSERT GUID";
[MenuItem("BUDDYWORKS/Scripts/Spawn Prefab...")]
public static void SpawnCustomPrefab()
{
SpawnPrefab(prefabGUID);
}
private static void SpawnPrefab(string guid)
{
string prefabPath = AssetDatabase.GUIDToAssetPath(guid);
if (string.IsNullOrEmpty(prefabPath))
{
Debug.LogError("Prefab with GUID " + guid + " not found.");
return;
}
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
GameObject selectedObject = Selection.activeGameObject;
if (prefab == null)
{
Debug.LogError("Failed to load prefab with GUID " + guid + " at path " + prefabPath);
return;
}
GameObject instantiatedPrefab = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
if (selectedObject != null)
{
instantiatedPrefab.transform.parent = selectedObject.transform;
}
if (instantiatedPrefab != null)
{
EditorGUIUtility.PingObject(instantiatedPrefab);
}
else
{
Debug.LogError("Failed to instantiate prefab with GUID " + guid);
}
}
}
}