Space Ad Mobile 9.5.1
Space Ad Mobile makes ads easy to add into your mobile game: one unified workflow for LevelPlay, AdMob, Yandex.Ads across banners, interstitials and rewarded.
Loading...
Searching...
No Matches
PackageCreateMenu.cs
1using System.Linq;
2using UnityEditor;
3using UnityEditor.SceneManagement;
4using UnityEngine;
5using UnityEngine.SceneManagement;
6
7namespace SCG.SpaceAdMobile
8{
12 public static class PackageCreateMenu
13 {
14 private const string RootName = "GameObject/" + nameof(SCG) + "/" + nameof(SpaceAdMobile);
15
16 [MenuItem(RootName, false, 33)]
17 private static void ShowCreatePopup()
18 {
19 var resource = AdAggregatorResource.Instance;
20 if (resource == null || resource.Aggregators == null)
21 {
22 EditorUtility.DisplayDialog("Create", $"The resource file '{nameof(AdAggregatorResource)}' is missing. Please reinstall the package.", "OK");
23 return;
24 }
25
26 var items = resource.Aggregators.Where(e => DefineSymbolsManager.HasDefineSymbol(e.Define))
27 .SelectMany(e => e.Prefabs)
28 .Where(p => p != null && PrefabUtility.GetPrefabAssetType(p) != PrefabAssetType.NotAPrefab)
29 .Distinct()
30 .OrderBy(p => p.name, System.StringComparer.OrdinalIgnoreCase)
31 .ToArray();
32
33 var fWindows = EditorWindow.focusedWindow != null ? EditorWindow.focusedWindow : null;
34 var ctx = fWindows; if (ctx == null) ctx = SceneView.lastActiveSceneView;
35
36 var center = ctx != null ? ctx.position.center : new Vector2(400, 300);
37 var rect = new Rect(center.x, center.y, 1f, 1f);
38
39 ShowMenu(rect, items);
40 }
41
42 [MenuItem(RootName, true)]
43 private static bool Validate() => true;
44
45 private static void ShowMenu(Rect screenRect, GameObject[] prefabs)
46 {
47 EditorApplication.delayCall += ShowMenuDelayCall;
48 return;
49
50 void ShowMenuDelayCall()
51 {
52 EditorApplication.delayCall -= ShowMenuDelayCall;
53
54 var menu = new GenericMenu();
55 foreach (var p in prefabs)
56 {
57 if (!p) continue;
58
59 var label = new GUIContent($"Create {p.name}");
60 menu.AddItem(label, false, OnPick, p);
61 }
62
63 if (menu.GetItemCount() == 0)
64 {
65 EditorUtility.DisplayDialog("Create", "No prefabs available.", "OK");
66 return;
67 }
68
69 // Show menu via a tiny popup host placed at screenRect
70 GenericMenuPopupHost.Show(menu, screenRect);
71 }
72 }
73
74 private static void OnPick(object userData) => Instantiate(userData as GameObject);
75
76 private static void Instantiate(GameObject prefab)
77 {
78 if (prefab == null)
79 {
80 Debug.LogError("The selected prefab is missing.");
81 return;
82 }
83
84 if (PrefabUtility.GetPrefabAssetType(prefab) == PrefabAssetType.NotAPrefab)
85 {
86 Debug.LogError($"'{prefab.name}' is not a prefab asset.");
87 return;
88 }
89
90#if UNITY_2021_3_OR_NEWER
91 var instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab, SceneManager.GetActiveScene());
92#else
93 var instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
94#endif
95 if (Selection.activeTransform)
96 GameObjectUtility.SetParentAndAlign(instance, Selection.activeTransform.gameObject);
97
98 PrefabUtility.UnpackPrefabInstance(instance, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
99 Undo.RegisterCreatedObjectUndo(instance, $"Create {prefab.name}");
100 Selection.activeObject = instance;
101 EditorSceneManager.MarkSceneDirty(instance.scene);
102 }
103 }
104
108 internal sealed class GenericMenuPopupHost : EditorWindow
109 {
110 private GenericMenu _menu;
111 private bool _shown;
112
113 public static void Show(GenericMenu menu, Rect screenRect)
114 {
115 // Ensure non-zero size to avoid odd anchoring
116 if (screenRect.width <= 0f || screenRect.height <= 0f)
117 screenRect.size = Vector2.one;
118
119 var w = CreateInstance<GenericMenuPopupHost>();
120 w._menu = menu;
121 w.position = screenRect; // screen-space
122 w.ShowPopup(); // borderless, always-on-top small window
123 w.Focus();
124 }
125
126 private void OnGUI()
127 {
128 // Call DropDown ONCE from a real GUI context
129 if (_shown) return;
130 if (Event.current.type != EventType.Repaint) return;
131
132 // Local (window) coordinates — anchor at (0,0) of this tiny host
133 var local = new Rect(0f, 0f, Mathf.Max(1f, position.width), Mathf.Max(1f, position.height));
134 _menu.DropDown(local);
135 _shown = true;
136
137 // Close on next tick to avoid stealing focus after selection
138 EditorApplication.delayCall += Close;
139 }
140
141 private void OnLostFocus()
142 {
143 // Safety: if the menu closed without a selection, close host as well
144 if (!_shown) return;
145 Close();
146 }
147 }
148}
Aggregator[] Aggregators
Specifies the aggregators.
static AdAggregatorResource Instance
Singleton Resource instance.
Provides ad aggregator resource functionality.
static bool HasDefineSymbol(string define)
Checks whether a scripting define symbol is present for the currently selected build target.
Provides define symbols manager functionality.
Provides package create menu functionality.
Represents a mobile ad manager component specifically designed for integration with Google AdMob serv...
Definition AdUnit.cs:7