void treeView_OnDragDropSuccess(DragDropTreeViewItem source, DragDropTreeViewItem target, System.ComponentModel.CancelEventArgs e) { if (!(source.Tag is GameObject)) { e.Cancel = true; return; } List <TreeViewItem> movedItems = TreeViewExtension.GetSelectedTreeViewItems(treeView); // verify if the moved items will not be placed on any of their children or in themselfs foreach (var ti in movedItems) { var tx = (DragDropTreeViewItem)ti; if (ti == target) { // trying to drop on one of the moved items e.Cancel = true; return; } if (DragDropTreeView.TreeContainsNode(treeView, tx, target)) { e.Cancel = true; return; } } if (target.Tag is GameObject) { foreach (var ti in movedItems) { GameObject _source = ti.Tag as GameObject; // no parent? if (_source.Transform.Parent == null) { SceneManager.ActiveScene.GameObjects.Remove(_source, false); } else { GameObject parent = (ti.Parent as DragDropTreeViewItem).Tag as GameObject; parent.Children.Remove(_source, false); } if (DragDropHelper.insertionPlace == DragDropHelper.InsertionPlace.Center) { (target.Tag as GameObject).Children.Insert(0, _source); } else { int index = 0; // no parent? if ((target.Tag as GameObject).Transform.Parent == null) { index = SceneManager.ActiveScene.GameObjects.IndexOf(target.Tag as GameObject); } else { GameObject parent = (target.Parent as DragDropTreeViewItem).Tag as GameObject; index = parent.Children.IndexOf(target.Tag as GameObject); } if (DragDropHelper.insertionPlace == DragDropHelper.InsertionPlace.Top) { index++; } if ((target.Tag as GameObject).Transform.Parent == null) { SceneManager.ActiveScene.GameObjects.Insert(index, _source); } else { GameObject parent = (target.Parent as DragDropTreeViewItem).Tag as GameObject; parent.Children.Insert(index, _source); } } } } else { e.Cancel = true; } }
// Determine whether one node is a parent // or ancestor of a second node. private bool ContainsNode(DragDropTreeViewItem node1, DragDropTreeViewItem node2) { return(DragDropTreeView.TreeContainsNode(this, node1, node2)); }