335 lines
9.7 KiB
C#
335 lines
9.7 KiB
C#
using UnityEngine;
|
|
using UnityEditor;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using UnityEditor.SceneManagement;
|
|
|
|
public class FavoritesTab : EditorWindow
|
|
{
|
|
private List<UnityEngine.Object> favoriteObjects = new List<UnityEngine.Object>();
|
|
private Vector2 scrollPosition;
|
|
private bool showPrefabs = true;
|
|
private bool showScenes = true;
|
|
private bool showScripts = true;
|
|
private bool showOthers = true;
|
|
private string searchFilter = "";
|
|
|
|
private const string FAVORITES_FILE = "Library/FavoritesData.json";
|
|
|
|
[System.Serializable]
|
|
public class FavoritesData
|
|
{
|
|
public List<string> favoritePaths = new List<string>();
|
|
}
|
|
|
|
[MenuItem("Window/Favorites Tab")]
|
|
public static void ShowWindow()
|
|
{
|
|
GetWindow<FavoritesTab>("Favorites");
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
LoadFavorites();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
SaveFavorites();
|
|
}
|
|
|
|
private void OnGUI()
|
|
{
|
|
DrawHeader();
|
|
DrawFilters();
|
|
DrawSearchBar();
|
|
DrawFavoritesList();
|
|
DrawDropArea();
|
|
|
|
// Handle drag and drop
|
|
HandleDragAndDrop();
|
|
}
|
|
|
|
private void DrawHeader()
|
|
{
|
|
EditorGUILayout.BeginHorizontal();
|
|
GUILayout.Label("Favorites", EditorStyles.boldLabel);
|
|
|
|
GUILayout.FlexibleSpace();
|
|
|
|
if (GUILayout.Button("Clear All", GUILayout.Width(80)))
|
|
{
|
|
if (EditorUtility.DisplayDialog("Clear Favorites", "Are you sure you want to clear all favorites?", "Yes", "No"))
|
|
{
|
|
favoriteObjects.Clear();
|
|
SaveFavorites();
|
|
}
|
|
}
|
|
|
|
EditorGUILayout.EndHorizontal();
|
|
EditorGUILayout.Space();
|
|
}
|
|
|
|
private void DrawFilters()
|
|
{
|
|
EditorGUILayout.BeginHorizontal();
|
|
GUILayout.Label("Show:", GUILayout.Width(40));
|
|
|
|
showPrefabs = GUILayout.Toggle(showPrefabs, "Prefabs", GUILayout.Width(60));
|
|
showScenes = GUILayout.Toggle(showScenes, "Scenes", GUILayout.Width(60));
|
|
showScripts = GUILayout.Toggle(showScripts, "Scripts", GUILayout.Width(60));
|
|
showOthers = GUILayout.Toggle(showOthers, "Others", GUILayout.Width(60));
|
|
|
|
EditorGUILayout.EndHorizontal();
|
|
EditorGUILayout.Space();
|
|
}
|
|
|
|
private void DrawSearchBar()
|
|
{
|
|
EditorGUILayout.BeginHorizontal();
|
|
GUILayout.Label("Search:", GUILayout.Width(50));
|
|
searchFilter = EditorGUILayout.TextField(searchFilter);
|
|
|
|
if (GUILayout.Button("X", GUILayout.Width(20)))
|
|
{
|
|
searchFilter = "";
|
|
GUI.FocusControl(null);
|
|
}
|
|
|
|
EditorGUILayout.EndHorizontal();
|
|
EditorGUILayout.Space();
|
|
}
|
|
|
|
private void DrawFavoritesList()
|
|
{
|
|
if (favoriteObjects.Count == 0)
|
|
{
|
|
EditorGUILayout.HelpBox("No favorites added yet. Drag assets here to add them to your favorites!", MessageType.Info);
|
|
return;
|
|
}
|
|
|
|
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
|
|
|
|
var filteredObjects = GetFilteredObjects();
|
|
|
|
for (int i = 0; i < filteredObjects.Count; i++)
|
|
{
|
|
var obj = filteredObjects[i];
|
|
if (obj == null) continue;
|
|
|
|
DrawFavoriteItem(obj, i);
|
|
}
|
|
|
|
EditorGUILayout.EndScrollView();
|
|
}
|
|
|
|
private List<UnityEngine.Object> GetFilteredObjects()
|
|
{
|
|
var filtered = favoriteObjects.Where(obj => obj != null).ToList();
|
|
|
|
// Apply type filters
|
|
if (!showPrefabs || !showScenes || !showScripts || !showOthers)
|
|
{
|
|
filtered = filtered.Where(obj => ShouldShowObject(obj)).ToList();
|
|
}
|
|
|
|
// Apply search filter
|
|
if (!string.IsNullOrEmpty(searchFilter))
|
|
{
|
|
filtered = filtered.Where(obj => obj.name.ToLower().Contains(searchFilter.ToLower())).ToList();
|
|
}
|
|
|
|
return filtered;
|
|
}
|
|
|
|
private bool ShouldShowObject(UnityEngine.Object obj)
|
|
{
|
|
var path = AssetDatabase.GetAssetPath(obj);
|
|
var extension = Path.GetExtension(path).ToLower();
|
|
|
|
if (showPrefabs && extension == ".prefab") return true;
|
|
if (showScenes && (extension == ".unity" || obj is SceneAsset)) return true;
|
|
if (showScripts && (extension == ".cs" || extension == ".js")) return true;
|
|
if (showOthers && extension != ".prefab" && extension != ".unity" && extension != ".cs" && extension != ".js") return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
private void DrawFavoriteItem(UnityEngine.Object obj, int index)
|
|
{
|
|
EditorGUILayout.BeginHorizontal();
|
|
|
|
// Icon
|
|
var icon = AssetPreview.GetMiniThumbnail(obj);
|
|
if (icon != null)
|
|
{
|
|
GUILayout.Label(icon, GUILayout.Width(20), GUILayout.Height(20));
|
|
}
|
|
|
|
// Object field (clickable)
|
|
var newObj = EditorGUILayout.ObjectField(obj, typeof(UnityEngine.Object), false);
|
|
if (newObj != obj)
|
|
{
|
|
favoriteObjects[favoriteObjects.IndexOf(obj)] = newObj;
|
|
SaveFavorites();
|
|
}
|
|
|
|
// Quick action buttons
|
|
var path = AssetDatabase.GetAssetPath(obj);
|
|
var extension = Path.GetExtension(path).ToLower();
|
|
|
|
if (extension == ".unity" || obj is SceneAsset)
|
|
{
|
|
if (GUILayout.Button("Open", GUILayout.Width(50)))
|
|
{
|
|
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
|
|
{
|
|
EditorSceneManager.OpenScene(path);
|
|
}
|
|
}
|
|
}
|
|
else if (extension == ".prefab")
|
|
{
|
|
if (GUILayout.Button("Select", GUILayout.Width(50)))
|
|
{
|
|
Selection.activeObject = obj;
|
|
EditorGUIUtility.PingObject(obj);
|
|
}
|
|
}
|
|
else if (extension == ".cs")
|
|
{
|
|
if (GUILayout.Button("Edit", GUILayout.Width(50)))
|
|
{
|
|
AssetDatabase.OpenAsset(obj);
|
|
}
|
|
}
|
|
|
|
// Remove button
|
|
if (GUILayout.Button("X", GUILayout.Width(20)))
|
|
{
|
|
favoriteObjects.Remove(obj);
|
|
SaveFavorites();
|
|
}
|
|
|
|
EditorGUILayout.EndHorizontal();
|
|
}
|
|
|
|
private void DrawDropArea()
|
|
{
|
|
EditorGUILayout.Space();
|
|
|
|
var dropArea = GUILayoutUtility.GetRect(0.0f, 50.0f, GUILayout.ExpandWidth(true));
|
|
GUI.Box(dropArea, "Drag assets here to add to favorites");
|
|
|
|
switch (Event.current.type)
|
|
{
|
|
case EventType.DragUpdated:
|
|
case EventType.DragPerform:
|
|
if (!dropArea.Contains(Event.current.mousePosition))
|
|
break;
|
|
|
|
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
|
|
|
|
if (Event.current.type == EventType.DragPerform)
|
|
{
|
|
DragAndDrop.AcceptDrag();
|
|
|
|
foreach (var draggedObject in DragAndDrop.objectReferences)
|
|
{
|
|
AddToFavorites(draggedObject);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void HandleDragAndDrop()
|
|
{
|
|
if (Event.current.type == EventType.DragUpdated)
|
|
{
|
|
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
|
|
Event.current.Use();
|
|
}
|
|
else if (Event.current.type == EventType.DragPerform)
|
|
{
|
|
DragAndDrop.AcceptDrag();
|
|
|
|
foreach (var draggedObject in DragAndDrop.objectReferences)
|
|
{
|
|
AddToFavorites(draggedObject);
|
|
}
|
|
|
|
Event.current.Use();
|
|
}
|
|
}
|
|
|
|
private void AddToFavorites(UnityEngine.Object obj)
|
|
{
|
|
if (obj == null) return;
|
|
|
|
if (!favoriteObjects.Contains(obj))
|
|
{
|
|
favoriteObjects.Add(obj);
|
|
SaveFavorites();
|
|
}
|
|
}
|
|
|
|
private void SaveFavorites()
|
|
{
|
|
var data = new FavoritesData();
|
|
|
|
foreach (var obj in favoriteObjects)
|
|
{
|
|
if (obj != null)
|
|
{
|
|
var path = AssetDatabase.GetAssetPath(obj);
|
|
if (!string.IsNullOrEmpty(path))
|
|
{
|
|
data.favoritePaths.Add(path);
|
|
}
|
|
}
|
|
}
|
|
|
|
var json = JsonUtility.ToJson(data, true);
|
|
File.WriteAllText(FAVORITES_FILE, json);
|
|
}
|
|
|
|
private void LoadFavorites()
|
|
{
|
|
favoriteObjects.Clear();
|
|
|
|
if (File.Exists(FAVORITES_FILE))
|
|
{
|
|
try
|
|
{
|
|
var json = File.ReadAllText(FAVORITES_FILE);
|
|
var data = JsonUtility.FromJson<FavoritesData>(json);
|
|
|
|
foreach (var path in data.favoritePaths)
|
|
{
|
|
UnityEngine.Object obj = null;
|
|
|
|
// Handle scenes specifically
|
|
if (path.EndsWith(".unity"))
|
|
{
|
|
obj = AssetDatabase.LoadAssetAtPath<SceneAsset>(path);
|
|
}
|
|
else
|
|
{
|
|
obj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(path);
|
|
}
|
|
|
|
if (obj != null)
|
|
{
|
|
favoriteObjects.Add(obj);
|
|
}
|
|
}
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Debug.LogError($"Error loading favorites: {e.Message}");
|
|
}
|
|
}
|
|
}
|
|
} |