//右键菜单栏 private void OnContextClick() { var menu = new GenericMenu(); string path = AssetDatabase.GetAssetPath(nowSelectObj); /* menu.AddItem(new GUIContent("Inspector"), false, delegate (object data) * { * var obj = AssetDatabase.LoadMainAssetAtPath(path); * Selection.activeObject = obj; * }, null);*/ menu.AddItem(new GUIContent("打开文件夹"), false, delegate(object data) { EditorUtility.RevealInFinder(path); }, null); if (nowSelectObj is Sprite) { menu.AddItem(new GUIContent("SpriteEditor"), false, delegate(object data) { Selection.activeObject = nowSelectObj; System.Type type = System.Type.GetType("UnityEditor.SpriteEditorWindow,UnityEditor"); EditorWindow.GetWindow(type); }, null); } menu.AddItem(new GUIContent("移动到Atlas"), false, delegate(object data) { ServiceResSelectorUtil.MoveToNewFolder("Assets" + ATLAS_ROOT + nowSubDic, nowSelectObjList); }, null); menu.AddItem(new GUIContent("移动到AtlasDynamic"), false, delegate(object data) { ServiceResSelectorUtil.MoveToNewFolder("Assets" + ATLAS_DYNAMIC_ROOT + nowSubDic, nowSelectObjList); }, null); menu.AddItem(new GUIContent("移动到Texture"), false, delegate(object data) { ServiceResSelectorUtil.MoveToNewFolder("Assets" + Texture_ROOT + nowSubDic, nowSelectObjList); }, null); menu.AddItem(new GUIContent("移动到其他文件夹"), false, delegate(object data) { ServiceResSelectorUtil.SelectMoveToNewFolder(nowSelectObjList, nowResRootPath); }, null); menu.AddItem(new GUIContent("------------------------------------"), false, delegate(object data) { }, null); menu.AddItem(new GUIContent("删除"), false, delegate(object data) { nowSelectObj = null; ServiceResSelectorUtil.DeleteObjects(nowSelectObjList); }, null); menu.ShowAsContext(); }
void OnResItemClick(Object obj, bool needPing, bool fromDragEvent) { if (obj != null && nowSelectObj == obj && (Event.current.control || Event.current.shift) == false) { DelayShowTextField(); } else { isInEditorName = false; } nowSelectObj = null; nowSelectObjList = ServiceResSelectorUtil.GetSelectObjList(Event.current, nowSelectObjList, nowResList, obj, fromDragEvent); if (nowSelectObjList.Count > 0) { nowSelectObj = nowSelectObjList[nowSelectObjList.Count - 1]; } GUI.FocusControl(null); if (needPing && nowSelectObj) { EditorGUIUtility.PingObject(obj); } }
Rect DrawResItem(int row_index, int col_index, Object res, int offY, bool isMinMode, int scrollWidth) { bool isNowSelectObj = isSelectObj(res); string resName = res.name; string controlName = GetControlName(res); GUILayout.BeginVertical(); Rect totalRect = new Rect(0, 0, 1, 1); Rect draw_rect = new Rect(col_index * (resItemSize + 10), row_index * (resItemSize + offY), resItemSize, resItemSize); totalRect.x = draw_rect.x; totalRect.y = draw_rect.y; totalRect.width = resItemSize; totalRect.height = resItemSize + RES_ITEM_LABEL_HEIGHT; var nameRect = new Rect(col_index * (resItemSize + 10), row_index * (resItemSize + offY) + resItemSize + 2, resItemSize, RES_ITEM_LABEL_HEIGHT); if (isMinMode) { nameRect.x = resItemSize + 5; nameRect.y = draw_rect.y; nameRect.width = scrollWidth; totalRect.width = scrollWidth; totalRect.height = resItemSize; } if (isNowSelectObj && isMinMode && nowFocusedControlName != controlName) { EditorGUI.DrawRect(totalRect, SelectMaskColor); } if (res is Sprite) { DrawSprit(draw_rect, res as Sprite); } else if (res is Texture2D) { DrawTexture(draw_rect, res as Texture2D); } else { GUI.Box(draw_rect, EditorGUIUtility.IconContent("PrefabNormal Icon")); //EditorGUI.DrawRect(draw_rect, Color.gray); } if (isNowSelectObj && isMinMode == false) { DrawRectOutline(draw_rect, Color.green); } EditorGUI.BeginChangeCheck(); GUI.backgroundColor = new Color(.6f, 1.0f, 1.0f, 0f); if (isNowSelectObj == false || isInEditorName == false) { EditorGUI.LabelField(nameRect, "", ServiceResSelectorUtil.GetFittableText(resName, nameRect, resNameGUIStyle), resNameGUIStyle); /* Rect btnRect = new Rect(col_index * (resItemSize + 10), row_index * (resItemSize + offY), position.width, resItemSize); * if (GUI.Button(btnRect, "")) * { * OnResItemClick(res, true); * }*/ } else { GUI.SetNextControlName(controlName); resName = EditorGUI.DelayedTextField(nameRect, "", resName, resNameGUIStyle); if (EditorGUI.EndChangeCheck() && string.IsNullOrEmpty(resName) == false) { isInEditorName = false; Debug.Log("change:" + resName); string path = AssetDatabase.GetAssetPath(res); AssetDatabase.RenameAsset(path, resName); RefreshAssets(); } } GUI.backgroundColor = Color.white; GUILayout.EndVertical(); return(totalRect); }
void CheckEvent(Dictionary <int, Rect> rectList, List <Object> resList, Rect rectArea) { //void CheckEvent(Rect draw_rect, Object obj,int index) { Event evt = Event.current; Object obj = null; switch (evt.type) { case EventType.ContextClick: //这个不触发 不知道为啥 evt.Use(); break; case EventType.MouseDown: DragAndDrop.PrepareStartDrag(); //obj = GetNowPosItem(rectList, resList, evt); //UpdateDragObjectReferences(obj); evt.Use(); break; case EventType.MouseUp: Debug.Log("MouseUp"); obj = GetNowPosItem(rectList, resList, evt); UpdateDragObjectReferences(obj); if (obj != null) { string path = AssetDatabase.GetAssetPath(nowSelectObj); Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(path);; } evt.Use(); break; case EventType.MouseDrag: // If drag was started here: Debug.Log("MouseDrag"); obj = GetNowPosItem(rectList, resList, evt); UpdateDragObjectReferences(obj, true); if (obj != null && DragAndDrop.GetGenericData("test") != null) { DisposeEditorNameTimer(); DragAndDrop.StartDrag("test"); //Debug.Log("StartDrag:"); evt.Use(); } break; case EventType.DragUpdated: //Debug.Log("DragUpdated"); var objectReferences = DragAndDrop.objectReferences; var paths = DragAndDrop.paths; if ((objectReferences.Length > 0 && objectReferences[0] != nowSelectObj) || (paths.Length > 0 && paths[0].IndexOf("Assets/") != 0)) { DragAndDrop.visualMode = DragAndDropVisualMode.Link; } else { DragAndDrop.visualMode = DragAndDropVisualMode.Rejected; } evt.Use(); break; case EventType.Repaint: break; case EventType.DragPerform: DragAndDrop.AcceptDrag(); ServiceResSelectorUtil.TryImportAssets(nowResRootPath, resRootList, resSubType, nowSubDic); evt.Use(); break; case EventType.DragExited: //Debug.Log("DragExited"); DragAndDrop.PrepareStartDrag(); evt.Use(); break; } }
void DrawResList() { if (string.IsNullOrEmpty(selectService)) { return; } int startX = serviceListTabWidth + 2; int scrollWidth = Screen.width - startX - 2; nowResRootPath = resRubTypeList[resSubType] + nowSubDic; Handles.BeginGUI(); Handles.color = Color.black; float tempY = position.height - RES_LIST_HEIGHT_OFFY; Handles.DrawLine(new Vector3(startX, tempY), new Vector3(position.width, tempY)); Handles.EndGUI(); Rect rectArea = new Rect(serviceListTabWidth, RES_LIST_START_Y, position.width - serviceListTabWidth, position.height - RES_LIST_HEIGHT_OFFY - RES_LIST_START_Y); GUILayout.BeginArea(rectArea); if (Directory.Exists(nowResRootPath) == false) { GUILayout.Label("不存在此目录:" + nowResRootPath); if (GUILayout.Button("创建(带ABName)", GUILayout.Width(serviceListTabWidth - 22))) { if (UnityEditor.EditorUtility.DisplayDialog("创建目录", "是否创建目录并设置ABName:\n" + nowResRootPath + "", "确认", "取消")) { ServiceResSelectorUtil.CreateDirectory(nowResRootPath); } } if (GUILayout.Button("创建(不带ABName)", GUILayout.Width(serviceListTabWidth - 22))) { if (UnityEditor.EditorUtility.DisplayDialog("创建目录", "是否创建目录:\n" + nowResRootPath + "", "确认", "取消")) { ServiceResSelectorUtil.CreateDirectory(nowResRootPath, false); } } } else { nowResList = GetResList(nowResRootPath); scrollWidth = Mathf.Max(100, scrollWidth); resListScrollPos = GUILayout.BeginScrollView(resListScrollPos, GUILayout.Width(scrollWidth), GUILayout.Height(position.height - RES_LIST_HEIGHT_OFFY - RES_LIST_START_Y)); int col_show_count = (scrollWidth) / (resItemSize + 10); int offY = RES_ITEM_LABEL_HEIGHT + 2; bool isMinMode = resItemSize == MIN_RES_ITEM_SIZE; if (isMinMode) { col_show_count = 1; offY = 0; resNameGUIStyle.alignment = TextAnchor.MiddleLeft; } else { resNameGUIStyle.alignment = TextAnchor.MiddleCenter; } col_show_count = Mathf.Max(1, col_show_count); //the following for-loop will run forever if we limit this int total_count = nowResList.Count; int row_index = 0; Dictionary <int, Rect> rectList = new Dictionary <int, Rect>(); for (int res_index = 0; res_index < total_count;) { GUILayout.BeginHorizontal(); for (int col_index = 0; col_index < col_show_count && res_index < total_count; ++col_index) { Object res = nowResList[res_index]; Rect draw_rect = DrawResItem(row_index, col_index, res, offY, isMinMode, scrollWidth); rectList[res_index] = draw_rect; ++res_index; } GUILayout.EndHorizontal(); row_index++; } GUILayout.Space(row_index * (resItemSize + offY)); if (total_count == 0) { GUILayout.Label("当前目录为空,请拖资源进来"); } GUILayout.EndScrollView(); CheckEvent(rectList, nowResList, rectArea); } GUILayout.EndArea(); Rect sliderRect = new Rect(position.width - 100, position.height - 20, 150, 20); resItemSize = (int)EditorGUI.Slider(sliderRect, resItemSize, MIN_RES_ITEM_SIZE, MAX_RES_ITEM_SIZE); //resItemSize = (int)GUILayout.HorizontalSlider(resItemSize, MIN_RES_ITEM_SIZE, MAX_RES_ITEM_SIZE, GUILayout.Width(100)); if (nowSelectObj != null) { Rect selNameRect = new Rect(startX, position.height - 20, position.width - 250, 20); string path = AssetDatabase.GetAssetPath(nowSelectObj); EditorGUI.LabelField(selNameRect, "", Path.GetFileName(path)); } }