ScenePartition/Editor/ScenePartitionToolbar.cs

130 lines
4.2 KiB
C#

using UnityEditor;
using UnityEditor.Overlays;
using UnityEditor.Toolbars;
using UnityEngine;
namespace VertexColor.ScenePartition.Editor
{
[Overlay(typeof(SceneView), "ScenePartition")]
public class ScenePartitionToolbar : ToolbarOverlay
{
private const string iconPath = "Packages/com.vertexcolor.scenepartition/Editor/Icons";
public ScenePartitionToolbar() : base(Load.Id, Save.Id, Reload.Id, Unload.Id, GenerateGrid.Id) { }
[EditorToolbarElement(Id, typeof(SceneView))]
public class Load : EditorToolbarButton, IAccessContainerWindow
{
public const string Id = "ScenePartition/Load";
public EditorWindow containerWindow { get; set; }
public Load()
{
text = "Load";
icon = AssetDatabase.LoadAssetAtPath<Texture2D>($"{iconPath}/Load.png");
tooltip = "Load the entire scene";
clicked += OnClick;
}
void OnClick()
{
if (!ScenePartitionUtils.TryGetScenePartitionSOForActiveScene(out ScenePartitionSO scenePartitionSO)) return;
scenePartitionSO.LoadAll();
}
}
[EditorToolbarElement(Id, typeof(SceneView))]
public class Save : EditorToolbarButton, IAccessContainerWindow
{
public const string Id = "ScenePartition/Save";
public EditorWindow containerWindow { get; set; }
public Save()
{
text = "Save";
icon = AssetDatabase.LoadAssetAtPath<Texture2D>($"{iconPath}/Save.png");
tooltip = "Save scene";
clicked += OnClick;
}
void OnClick()
{
if (!ScenePartitionUtils.TryGetScenePartitionSOForActiveScene(out ScenePartitionSO scenePartitionSO)) return;
scenePartitionSO.Save();
}
}
[EditorToolbarElement(Id, typeof(SceneView))]
public class Reload : EditorToolbarButton, IAccessContainerWindow
{
public const string Id = "ScenePartition/Reload";
public EditorWindow containerWindow { get; set; }
public Reload()
{
text = "Reload";
icon = AssetDatabase.LoadAssetAtPath<Texture2D>($"{iconPath}/Reload.png");
tooltip = "Discard changes and reload scene";
clicked += OnClick;
}
void OnClick()
{
if (!ScenePartitionUtils.TryGetScenePartitionSOForActiveScene(out ScenePartitionSO scenePartitionSO)) return;
scenePartitionSO.Reload();
}
}
[EditorToolbarElement(Id, typeof(SceneView))]
public class Unload : EditorToolbarButton, IAccessContainerWindow
{
public const string Id = "ScenePartition/Unload";
public EditorWindow containerWindow { get; set; }
public Unload()
{
text = "Unload";
icon = AssetDatabase.LoadAssetAtPath<Texture2D>($"{iconPath}/Unload.png");
tooltip = "Unload the scene";
clicked += OnClick;
}
void OnClick()
{
if (!ScenePartitionUtils.TryGetScenePartitionSOForActiveScene(out ScenePartitionSO scenePartitionSO)) return;
scenePartitionSO.Unload();
}
}
[EditorToolbarElement(Id, typeof(SceneView))]
public class GenerateGrid : EditorToolbarButton, IAccessContainerWindow
{
public const string Id = "ScenePartition/GenerateGrid";
public EditorWindow containerWindow { get; set; }
public GenerateGrid()
{
text = "GenerateGrid";
icon = AssetDatabase.LoadAssetAtPath<Texture2D>($"{iconPath}/GenerateGrid.png");
tooltip = "Generate scene grid";
clicked += OnClick;
}
void OnClick()
{
if (!ScenePartitionUtils.TryGetScenePartitionSOForActiveScene(out ScenePartitionSO scenePartitionSO)) return;
scenePartitionSO.GenerateSceneGridData();
}
}
}
}