14 private const string RootName =
"GameObject/" + nameof(
SCG) +
"/" + nameof(
SpaceAdMobile);
16 [MenuItem(RootName,
false, 33)]
17 private static void ShowCreatePopup()
20 if (resource ==
null || resource.Aggregators ==
null)
22 EditorUtility.DisplayDialog(
"Create", $
"The resource file '{nameof(AdAggregatorResource)}' is missing. Please reinstall the package.",
"OK");
27 .SelectMany(e => e.Prefabs)
28 .Where(p => p !=
null && PrefabUtility.GetPrefabAssetType(p) != PrefabAssetType.NotAPrefab)
30 .OrderBy(p => p.name, System.StringComparer.OrdinalIgnoreCase)
33 var fWindows = EditorWindow.focusedWindow !=
null ? EditorWindow.focusedWindow :
null;
34 var ctx = fWindows;
if (ctx ==
null) ctx = SceneView.lastActiveSceneView;
36 var center = ctx !=
null ? ctx.position.center :
new Vector2(400, 300);
37 var rect =
new Rect(center.x, center.y, 1f, 1f);
39 ShowMenu(rect, items);
42 [MenuItem(RootName,
true)]
43 private static bool Validate() =>
true;
45 private static void ShowMenu(Rect screenRect, GameObject[] prefabs)
47 EditorApplication.delayCall += ShowMenuDelayCall;
50 void ShowMenuDelayCall()
52 EditorApplication.delayCall -= ShowMenuDelayCall;
54 var menu =
new GenericMenu();
55 foreach (var p
in prefabs)
59 var label =
new GUIContent($
"Create {p.name}");
60 menu.AddItem(label,
false, OnPick, p);
63 if (menu.GetItemCount() == 0)
65 EditorUtility.DisplayDialog(
"Create",
"No prefabs available.",
"OK");
70 GenericMenuPopupHost.Show(menu, screenRect);
74 private static void OnPick(
object userData) => Instantiate(userData as GameObject);
76 private static void Instantiate(GameObject prefab)
80 Debug.LogError(
"The selected prefab is missing.");
84 if (PrefabUtility.GetPrefabAssetType(prefab) == PrefabAssetType.NotAPrefab)
86 Debug.LogError($
"'{prefab.name}' is not a prefab asset.");
90#if UNITY_2021_3_OR_NEWER
91 var instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab, SceneManager.GetActiveScene());
93 var instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
95 if (Selection.activeTransform)
96 GameObjectUtility.SetParentAndAlign(instance, Selection.activeTransform.gameObject);
98 PrefabUtility.UnpackPrefabInstance(instance, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
99 Undo.RegisterCreatedObjectUndo(instance, $
"Create {prefab.name}");
100 Selection.activeObject = instance;
101 EditorSceneManager.MarkSceneDirty(instance.scene);
133 var local =
new Rect(0f, 0f, Mathf.Max(1f, position.width), Mathf.Max(1f, position.height));