169 lines
5.2 KiB
C#
169 lines
5.2 KiB
C#
|
using System.Collections.Generic;
|
|||
|
using UnityEditor;
|
|||
|
using UnityEditor.IMGUI.Controls;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace CaoCao.Editor
|
|||
|
{
|
|||
|
public class BuildsWindow : EditorWindow
|
|||
|
{
|
|||
|
private const int k_SearchHeight = 20;
|
|||
|
[SerializeField] private TreeViewState _treeViewState;
|
|||
|
[SerializeField] private MultiColumnHeaderState _multiColumnHeaderState;
|
|||
|
|
|||
|
[MenuItem("Window/CaoCao/XAsset/Builds")]
|
|||
|
public static void OpenBuilds()
|
|||
|
{
|
|||
|
GetWindow<BuildsWindow>(false, "Builds");
|
|||
|
}
|
|||
|
|
|||
|
private readonly List<Build> _builds = new List<Build>();
|
|||
|
private readonly GUIContent buildName = new GUIContent("Build");
|
|||
|
|
|||
|
private Build _build;
|
|||
|
|
|||
|
|
|||
|
private SearchField _searchField;
|
|||
|
private BuildAssetTreeView _treeView;
|
|||
|
|
|||
|
private bool initialized;
|
|||
|
|
|||
|
private bool rebuildNow;
|
|||
|
|
|||
|
private int selected;
|
|||
|
|
|||
|
private void Update()
|
|||
|
{
|
|||
|
if (!rebuildNow) return;
|
|||
|
rebuildNow = false;
|
|||
|
Builder.BuildBundles(_build);
|
|||
|
}
|
|||
|
|
|||
|
public void OnGUI()
|
|||
|
{
|
|||
|
Initialize();
|
|||
|
|
|||
|
if (_treeViewState == null)
|
|||
|
{
|
|||
|
_treeViewState = new TreeViewState();
|
|||
|
var headerState =
|
|||
|
BuildAssetTreeView.CreateDefaultMultiColumnHeaderState(); // multiColumnTreeViewRect.width);
|
|||
|
if (MultiColumnHeaderState.CanOverwriteSerializedFields(_multiColumnHeaderState, headerState))
|
|||
|
MultiColumnHeaderState.OverwriteSerializedFields(_multiColumnHeaderState, headerState);
|
|||
|
|
|||
|
_multiColumnHeaderState = headerState;
|
|||
|
}
|
|||
|
|
|||
|
if (_treeView == null)
|
|||
|
{
|
|||
|
_treeView = new BuildAssetTreeView(_treeViewState, _multiColumnHeaderState);
|
|||
|
_treeView.Reload();
|
|||
|
}
|
|||
|
|
|||
|
if (_searchField == null)
|
|||
|
{
|
|||
|
_searchField = new SearchField();
|
|||
|
_searchField.downOrUpArrowKeyPressed += _treeView.SetFocusAndEnsureSelectedItem;
|
|||
|
}
|
|||
|
|
|||
|
if (_builds.Count == 0)
|
|||
|
{
|
|||
|
GUILayout.Label("No builds");
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
var rect = new Rect(0, 0, position.width, position.height);
|
|||
|
|
|||
|
DrawToolbar();
|
|||
|
DrawTreeView(rect);
|
|||
|
}
|
|||
|
|
|||
|
private void DrawTreeView(Rect rect)
|
|||
|
{
|
|||
|
const int toolbarHeight = k_SearchHeight + 4;
|
|||
|
var treeRect = new Rect(
|
|||
|
rect.xMin,
|
|||
|
rect.yMin + toolbarHeight,
|
|||
|
rect.width,
|
|||
|
rect.height - toolbarHeight);
|
|||
|
|
|||
|
_treeView?.OnGUI(treeRect);
|
|||
|
}
|
|||
|
|
|||
|
public ulong GetAssetSize(string path)
|
|||
|
{
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
private void DrawToolbar()
|
|||
|
{
|
|||
|
using (new EditorGUILayout.HorizontalScope(EditorStyles.toolbar))
|
|||
|
{
|
|||
|
DrawBuild();
|
|||
|
if (GUILayout.Button("Refresh", EditorStyles.toolbarButton)) Reload();
|
|||
|
|
|||
|
if (GUILayout.Button("Find References", EditorStyles.toolbarButton)) Builder.FindReferences();
|
|||
|
|
|||
|
if (_treeView != null) _treeView.searchString = _searchField.OnToolbarGUI(_treeView.searchString);
|
|||
|
|
|||
|
GUILayout.FlexibleSpace();
|
|||
|
if (GUILayout.Button("Build Bundles", EditorStyles.toolbarButton)) rebuildNow = true;
|
|||
|
|
|||
|
if (GUILayout.Button("Inspector", EditorStyles.toolbarButton)) EditorGUIUtility.PingObject(_build);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void DrawBuild()
|
|||
|
{
|
|||
|
buildName.text = _build.name;
|
|||
|
var rect = GUILayoutUtility.GetRect(buildName, EditorStyles.toolbarDropDown, GUILayout.MinWidth(128));
|
|||
|
if (!EditorGUI.DropdownButton(rect, buildName, FocusType.Keyboard,
|
|||
|
EditorStyles.toolbarDropDown))
|
|||
|
return;
|
|||
|
|
|||
|
var menu = new GenericMenu();
|
|||
|
for (var index = 0; index < _builds.Count; index++)
|
|||
|
{
|
|||
|
var version = _builds[index];
|
|||
|
menu.AddItem(new GUIContent(version.name), selected == index,
|
|||
|
data =>
|
|||
|
{
|
|||
|
if (selected != (int) data)
|
|||
|
{
|
|||
|
selected = (int) data;
|
|||
|
_build = _builds[selected];
|
|||
|
Reload();
|
|||
|
}
|
|||
|
}, index);
|
|||
|
}
|
|||
|
|
|||
|
menu.DropDown(rect);
|
|||
|
}
|
|||
|
|
|||
|
private void Reload()
|
|||
|
{
|
|||
|
if (_treeView == null) return;
|
|||
|
_treeView.assets.Clear();
|
|||
|
_build.parameters.name = _build.name;
|
|||
|
var task = new BuildJob(_build.parameters);
|
|||
|
task.Start(new CollectAssets());
|
|||
|
_treeView.assets.AddRange(task.rawAssets);
|
|||
|
_treeView.assets.AddRange(task.bundledAssets);
|
|||
|
_treeView.Reload();
|
|||
|
}
|
|||
|
|
|||
|
private void Initialize()
|
|||
|
{
|
|||
|
if (initialized) return;
|
|||
|
|
|||
|
_builds.AddRange(Settings.FindAssets<Build>());
|
|||
|
if (_builds.Count > 0)
|
|||
|
{
|
|||
|
_build = _builds[selected];
|
|||
|
Reload();
|
|||
|
}
|
|||
|
|
|||
|
initialized = true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|