void GetFavorites() { if (BlockoutEditorSettings.CurrentSceneSetting) { var dict = BlockoutEditorSettings.CurrentSceneSetting.assetDictionary; if (dict != null) { var sortedFavourites = dict.ToList().OrderByDescending(x => x.assetQuantity).ToList(); favouriteItems = new BlockoutItemPreview[(sortedFavourites.Count > amountOfItemsToShow ? amountOfItemsToShow : sortedFavourites.Count)]; for (var i = 0; i < sortedFavourites.Count; ++i) { if (sortedFavourites[i] == null || i >= amountOfItemsToShow) { break; } var fa = AssetDatabase.FindAssets(sortedFavourites[i].assetName + " t:prefab"); if (fa.Length != 0) { var path = AssetDatabase.GUIDToAssetPath(fa[0]); favouriteItems[i].prefab = (GameObject)AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)); favouriteItems[i].name = favouriteItems[i].prefab.name; favouriteItems[i].previewImage = AssetPreview.GetAssetPreview(favouriteItems[i].prefab); } } favouriteItems = favouriteItems.ToList().Distinct().ToArray(); var toRemove = favouriteItems.ToList().Where(x => x.previewImage == null).ToList(); favouriteItems = favouriteItems.ToList().Except(toRemove).ToArray(); if (favouriteItems.Length > 0) { repaint = true; } } } }
// Drag and drop logic protected void DragDropGUI(BlockoutItemPreview targetPreview, Rect previewArea) { // Chache event data Event currentEvent = Event.current; EventType currentEventType = currentEvent.type; // The DragExited event does not have the same mouse position data as the other events, // so it must be checked now: if (currentEventType == EventType.DragExited) { DragAndDrop.PrepareStartDrag(); // Clear generic data when user pressed escape. (Unfortunately, DragExited is also called when the mouse leaves the drag area) } switch (currentEventType) { case EventType.MouseDown: if (!previewArea.Contains(currentEvent.mousePosition)) { return; } // Mouse is within the preview area and has been clicked. Reset drag data DragAndDrop.PrepareStartDrag(); // reset data dragging = false; currentEvent.Use(); break; case EventType.MouseDrag: // If drag was started here: if (!previewArea.Contains(currentEvent.mousePosition)) { return; } // Start the drag event with drag references dragging = true; Object[] objectReferences = new Object[1] { targetPreview.prefab }; // Careful, null values cause exceptions in existing editor code. DragAndDrop.objectReferences = objectReferences; // Note: this object won't be 'get'-able until the next GUI event. DragAndDrop.StartDrag(targetPreview.name); currentEvent.Use(); break; case EventType.DragUpdated: // Drag positioning has been updated so check if its valid. if (IsDragTargetValid()) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; // Spawn the asset if it doesnt exist if (!spwanedAsset) { spwanedAsset = Instantiate(targetPreview.prefab); } PlaceDraggedAsset(); } else { DragAndDrop.visualMode = DragAndDropVisualMode.Rejected; } dragging = true; currentEvent.Use(); break; case EventType.DragPerform: // When the drag event has finished, place it and end the event if its valid. If it isn't valid, // destroy the object if (IsDragTargetValid()) { DragAndDrop.AcceptDrag(); PlaceDraggedAsset(); } else if (spwanedAsset != null) { DestroyImmediate(spwanedAsset); } dragging = true; currentEvent.Use(); break; case EventType.DragExited: // If the drag event has ben canceled, destroy the spawned asset if its already spawned if (spwanedAsset != null) { DestroyImmediate(spwanedAsset); } dragging = false; break; case EventType.MouseUp: // Clean up, in case MouseDrag never occurred: DragAndDrop.PrepareStartDrag(); if (!dragging && previewArea.Contains(currentEvent.mousePosition)) { // if the mouse is still within the preview area and no drag event has occured, then its only been clicked // So selected it in the project window Selection.activeGameObject = targetPreview.prefab; EditorGUIUtility.PingObject(targetPreview.prefab); repaint = true; } break; } }
void Update() { // If there is an obejcts selected, then find assets with a similar name to it in the asset database. // Only is its selected in the scene and not in the project window if (Selection.gameObjects.Length > 0) { var selected = Selection.gameObjects.ToList(); foreach (var x in selected) { if (!AssetDatabase.Contains(x.gameObject)) { var helper = x.GetComponent <BlockoutHelper> (); if (helper) { if (previousHelper != helper) { previousHelper = helper; if (parentWindow.CurrentSceneSetting.assetDictionary == null) { parentWindow.CurrentSceneSetting.assetDictionary = new List <AssetDefinition> (); } var names = helper.gameObject.name.Split('_').ToList(); string targetName = names [0]; int amount = ((names.Count - 1) > 2 ? 2 : names.Count - 1); for (int i = 1; i < amount; ++i) { targetName += "_" + names [i]; } var foundAssets = AssetDatabase.FindAssets(targetName + " t:prefab"); bool cap = (foundAssets.Length > amountOfItemsToShow); GameObject[] loadedSuggestions = new GameObject[cap ? amountOfItemsToShow : foundAssets.Length]; suggestedItems = new BlockoutItemPreview[cap ? amountOfItemsToShow : foundAssets.Length]; for (int i = 0; i < (cap ? amountOfItemsToShow : foundAssets.Length); ++i) { var path = AssetDatabase.GUIDToAssetPath(foundAssets [i]); suggestedItems[i].prefab = (GameObject)AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)); suggestedItems [i].name = suggestedItems [i].prefab.name; suggestedItems[i].previewImage = AssetPreview.GetAssetPreview(loadedSuggestions[i]); } break; } repaint = true; break; } } } } // If no assets are selected then default the suggested assets to a selection of Block prefabs else { var foundAssets = AssetDatabase.FindAssets("Block t:prefab"); bool cap = (foundAssets.Length > amountOfItemsToShow); GameObject[] loadedSuggestions = new GameObject[cap ? amountOfItemsToShow : foundAssets.Length]; suggestedItems = new BlockoutItemPreview[cap ? amountOfItemsToShow : foundAssets.Length]; for (int i = 0; i < (cap ? amountOfItemsToShow : foundAssets.Length); ++i) { var path = AssetDatabase.GUIDToAssetPath(foundAssets[i]); suggestedItems[i].prefab = (GameObject)AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)); suggestedItems [i].name = suggestedItems [i].prefab.name; suggestedItems[i].previewImage = AssetPreview.GetAssetPreview(loadedSuggestions[i]); } previousHelper = null; repaint = true; } // Do a redundancy check to see if the main editor window exists. If it does, then get the favourite assets list from the scene definition if (parentWindow) { var dict = parentWindow.CurrentSceneSetting.assetDictionary; #region Favourites if (dict != null) { var sortedFavourites = dict.ToList().OrderByDescending(x => x.assetQuantity).ToList(); favouriteItems = new BlockoutItemPreview[(sortedFavourites.Count > amountOfItemsToShow ? amountOfItemsToShow : sortedFavourites.Count)]; for (var i = 0; i < sortedFavourites.Count; ++i) { if (sortedFavourites[i] == null || i >= amountOfItemsToShow) { break; } var fa = AssetDatabase.FindAssets(sortedFavourites[i].assetName + " t:prefab"); if (fa.Length != 0) { var path = AssetDatabase.GUIDToAssetPath(fa[0]); favouriteItems[i].prefab = (GameObject)AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)); favouriteItems [i].name = favouriteItems [i].prefab.name; favouriteItems[i].previewImage = AssetPreview.GetAssetPreview(favouriteItems[i].prefab); } } favouriteItems = favouriteItems.ToList().Distinct().ToArray(); var toRemove = favouriteItems.ToList().Where(x => x.previewImage == null).ToList(); favouriteItems = favouriteItems.ToList().Except(toRemove).ToArray(); } #endregion } if (repaint) { Repaint(); repaint = false; } }