//Create and set a UnityObject variable node on drop protected override void OnDropAccepted(Object o, Vector2 mousePos) { if (o == null) { return; } if (UnityEditor.EditorUtility.IsPersistent(this) && !UnityEditor.EditorUtility.IsPersistent(o)) { Debug.LogError("This Graph is an asset. The dragged object is a scene reference. The reference will not persist"); } //macro? if (o is Macro) { this.AddMacroNode((Macro)o, mousePos, null, null); return; } var targetType = o.GetType(); //wrappable object? var wrapperTypes = CustomObjectWrapper.FindCustomObjectWrappersForType(targetType); //show menu var menu = new UnityEditor.GenericMenu(); menu.AddItem(new GUIContent(string.Format("Make Variable ({0})", targetType.FriendlyName())), false, (x) => { this.AddVariableGet(targetType, null, mousePos, null, x); }, o); foreach (var _wrapperType in wrapperTypes) { var wrapperType = _wrapperType; menu.AddItem(new GUIContent(string.Format("Add Wrapper ({0})", wrapperType.FriendlyName())), false, (x) => { this.AddObjectWrapper(wrapperType, mousePos, null, (UnityEngine.Object)x); }, o); } //append reflection menu.AddSeparator("/"); menu = this.AppendTypeReflectionNodesMenu(menu, targetType, "", mousePos, null, o); if (o is GameObject) { var go = (GameObject)o; foreach (var component in go.GetComponents <Component>().Where(c => c.hideFlags == 0)) { var cType = component.GetType(); var category = cType.Name + "/"; menu.AddItem(new GUIContent(string.Format(category + "Make Variable ({0})", cType.Name)), false, (x) => { this.AddVariableGet(cType, null, mousePos, null, x); }, component); foreach (var _wrapperType in wrapperTypes) { var wrapperType = _wrapperType; menu.AddItem(new GUIContent(string.Format("Add Wrapper ({0})", wrapperType.FriendlyName())), false, (x) => { this.AddObjectWrapper(wrapperType, mousePos, null, (UnityEngine.Object)x); }, o); } menu = this.AppendTypeReflectionNodesMenu(menu, cType, "", mousePos, null, component); } } menu.ShowAsContext(); Event.current.Use(); }
//used above for convenience UnityEditor.GenericMenu AppendDragAndDropObjectMenu(UnityEditor.GenericMenu menu, UnityEngine.Object o, string category, Vector2 mousePos) { var targetType = o.GetType(); foreach (var _wrapperType in CustomObjectWrapper.FindCustomObjectWrappersForType(targetType)) { var wrapperType = _wrapperType; menu.AddItem(new GUIContent(string.Format(category + "Add Wrapper ({0})", wrapperType.FriendlyName())), false, (x) => { this.AddObjectWrapper(wrapperType, mousePos, null, (UnityEngine.Object)x); }, o); } menu.AddItem(new GUIContent(string.Format(category + "Make Variable ({0})", targetType.FriendlyName())), false, (x) => { this.AddVariableGet(targetType, null, mousePos, null, x); }, o); return(menu); }