Skip to content

Commit

Permalink
Merge pull request liuhaopen#1 from yika-aixi/master
Browse files Browse the repository at this point in the history
!W
  • Loading branch information
liuhaopen authored Jul 25, 2019
2 parents 31a24f1 + be09d1a commit 3144f29
Showing 1 changed file with 68 additions and 28 deletions.
96 changes: 68 additions & 28 deletions Editor/PrefabWin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace U3DExtends {
public class PrefabWin : EditorWindow
{
private static int _labelDefaultFontSize;

[MenuItem("Window/PrefabWin", false, 9)]
static public void OpenPrefabTool()
{
_labelDefaultFontSize = EditorStyles.label.fontSize;
PrefabWin prefabWin = (PrefabWin)EditorWindow.GetWindow<PrefabWin>(false, "Prefab Win", true);
prefabWin.autoRepaintOnSceneChange = true;
prefabWin.Show();
Expand Down Expand Up @@ -62,20 +66,21 @@ public float SizePercent

BetterList<Item> mItems = new BetterList<Item>();

GameObject draggedObject
GameObject[] draggedObjects
{
get
{
if (DragAndDrop.objectReferences == null) return null;
if (DragAndDrop.objectReferences.Length == 1) return DragAndDrop.objectReferences[0] as GameObject;
return null;
if (DragAndDrop.objectReferences == null || DragAndDrop.objectReferences.Length == 0)
return null;

return DragAndDrop.objectReferences.Where(x=>x as GameObject).Cast<GameObject>().ToArray();
}
set
{
if (value != null)
{
DragAndDrop.PrepareStartDrag();
DragAndDrop.objectReferences = new Object[1] { value };
DragAndDrop.objectReferences = value;
draggedObjectIsOurs = true;
}
else DragAndDrop.AcceptDrag();
Expand All @@ -96,10 +101,11 @@ bool draggedObjectIsOurs
}
}


void OnEnable ()
{
instance = this;

Load();

mContent = new GUIContent();
Expand Down Expand Up @@ -280,7 +286,7 @@ void DestroyTexture (Item item)

void UpdateVisual ()
{
if (draggedObject == null) DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
if (draggedObjects == null) DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
else if (draggedObjectIsOurs) DragAndDrop.visualMode = DragAndDropVisualMode.Move;
else DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
}
Expand Down Expand Up @@ -386,7 +392,7 @@ int GetCellUnderMouse (int spacingX, int spacingY)
}

bool mReset = false;

private List<Item> _selections = new List<Item>();
void OnGUI ()
{
Event currentEvent = Event.current;
Expand All @@ -398,10 +404,23 @@ void OnGUI ()
int spacingY = spacingX;
if (mMode == Mode.DetailedMode) spacingY += 32;

GameObject dragged = draggedObject;
bool isDragging = (dragged != null);
GameObject[] draggeds = draggedObjects;
bool isDragging = (draggeds != null);
int indexUnderMouse = GetCellUnderMouse(spacingX, spacingY);
Item selection = isDragging ? FindItem(dragged) : null;

if (isDragging)
{
foreach (var gameObject in draggeds)
{
var result = FindItem(gameObject);

if (result != null)
{
_selections.Add(result);
}
}
}

string searchFilter = EditorPrefs.GetString("PrefabWin_SearchFilter", null);

int newTab = mTab;
Expand Down Expand Up @@ -457,16 +476,24 @@ void OnGUI ()
}
else if (type == EventType.DragPerform)
{
if (dragged != null)
if (draggeds != null)
{
if (selection != null)
if (_selections != null)
{
DestroyTexture(selection);
mItems.Remove(selection);
foreach (var selection in _selections)
{
DestroyTexture(selection);
mItems.Remove(selection);
}
}

AddItem(dragged, indexUnderMouse);
draggedObject = null;
foreach (var dragged in draggeds)
{
AddItem(dragged, indexUnderMouse);
++indexUnderMouse;
}

draggeds = null;
}
mMouseIsInside = false;
currentEvent.Use();
Expand All @@ -478,23 +505,25 @@ void OnGUI ()

if (!mMouseIsInside)
{
selection = null;
dragged = null;
_selections.Clear();
draggeds = null;
}

BetterList<int> indices = new BetterList<int>();

for (int i = 0; i < mItems.size; )
{
if (dragged != null && indices.size == indexUnderMouse)
if (draggeds != null && indices.size == indexUnderMouse)
indices.Add(-1);

if (mItems[i] != selection)
var has = _selections.Exists(item => item == mItems[i]);

if (!has)
{
if (string.IsNullOrEmpty(searchFilter) ||
mItems[i].prefab.name.IndexOf(searchFilter, System.StringComparison.CurrentCultureIgnoreCase) != -1)
indices.Add(i);
mItems[i].prefab.name.IndexOf(searchFilter, System.StringComparison.CurrentCultureIgnoreCase) != -1)
indices.Add(i);
}

++i;
}

Expand All @@ -510,9 +539,9 @@ void OnGUI ()

if (index != -1 && index < mItems.size)
{
selection = mItems[index];
draggedObject = selection.prefab;
dragged = selection.prefab;
_selections.Add(mItems[index]);
draggedObjects = _selections.Select(item => item.prefab).ToArray();
draggeds = _selections.Select(item=>item.prefab).ToArray();
currentEvent.Use();
}
}
Expand All @@ -524,7 +553,7 @@ void OnGUI ()
for (int i = 0; i < indices.size; ++i)
{
int index = indices[i];
Item ent = (index != -1) ? mItems[index] : selection;
Item ent = (index != -1) ? mItems[index] : _selections.Count == 0 ? null : _selections[0];

if (ent != null && ent.prefab == null)
{
Expand Down Expand Up @@ -590,6 +619,17 @@ void OnGUI ()
if (ent.tex != null)
{
GUI.DrawTexture(inner, ent.tex);
var labelPos = new Rect(inner);
var labelStyle = EditorStyles.label;
labelPos.height = labelStyle.lineHeight;
labelPos.y = inner.height - labelPos.height + 5;
labelStyle.fontSize = (int) (_labelDefaultFontSize * SizePercent);
labelStyle.alignment = TextAnchor.LowerCenter;
{
GUI.Label(labelPos, ent.prefab.name,labelStyle);
}
labelStyle.alignment = TextAnchor.UpperLeft;
labelStyle.fontSize = _labelDefaultFontSize;
}
else if (mMode != Mode.DetailedMode)
{
Expand Down

0 comments on commit 3144f29

Please sign in to comment.