0.6.0: Added ui layer and canvas picker
This commit is contained in:
parent
27d7ecb2dd
commit
169d11a2dd
10 changed files with 206 additions and 11 deletions
98
Editor/Toolbar/Tools/ToolUICanvasPicker.cs
Normal file
98
Editor/Toolbar/Tools/ToolUICanvasPicker.cs
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
using System.Collections.Generic;
|
||||
using Game.NavigationTool.Editor.Toolbar;
|
||||
using JetBrains.Annotations;
|
||||
using UnityEditor;
|
||||
using UnityEditorInternal;
|
||||
using UnityEngine;
|
||||
using UTools = UnityEditor.Tools;
|
||||
|
||||
namespace Game.NavigationTool.Editor.Tools
|
||||
{
|
||||
[UsedImplicitly]
|
||||
internal sealed class ToolUICanvasPicker : IToolbarDrawer
|
||||
{
|
||||
public bool Visible => true;
|
||||
public bool Enabled => !EditorApplication.isPlayingOrWillChangePlaymode;
|
||||
public EToolbarPlacement Placement => EToolbarPlacement.Left;
|
||||
|
||||
private static bool IS_DIRTY = true;
|
||||
private static string[] OPTIONS = new string[0];
|
||||
private static int[] INSTANCE_IDS = new int[0];
|
||||
|
||||
private void Initialize()
|
||||
{
|
||||
if (!IS_DIRTY)
|
||||
return;
|
||||
|
||||
Canvas[] canvases = Object.FindObjectsOfType<Canvas>();
|
||||
var list = new List<Canvas>(canvases.Length);
|
||||
var listNames = new List<string>(canvases.Length);
|
||||
var listIds = new List<int>(canvases.Length);
|
||||
|
||||
listNames.Add("Select");
|
||||
listIds.Add(-1);
|
||||
|
||||
for (var i = 0; i < canvases.Length; i++)
|
||||
{
|
||||
Canvas root = canvases[i].rootCanvas;
|
||||
|
||||
if (list.Contains(root))
|
||||
continue;
|
||||
|
||||
list.Add(root);
|
||||
listNames.Add(root.name);
|
||||
listIds.Add(root.GetInstanceID());
|
||||
}
|
||||
|
||||
OPTIONS = listNames.ToArray();
|
||||
INSTANCE_IDS = listIds.ToArray();
|
||||
IS_DIRTY = false;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
public void Draw(Rect rect, Styles styles)
|
||||
{
|
||||
Initialize();
|
||||
int temp = EditorGUI.Popup(rect, 0, OPTIONS, styles.popup);
|
||||
|
||||
if (temp != 0 && OPTIONS.Length != 1)
|
||||
Focus(INSTANCE_IDS[temp]);
|
||||
}
|
||||
|
||||
public float CalculateWidth()
|
||||
{
|
||||
return 100.0f;
|
||||
}
|
||||
|
||||
private static void Focus(int instanceId)
|
||||
{
|
||||
var canvas = EditorUtility.InstanceIDToObject(instanceId) as Canvas;
|
||||
|
||||
if (canvas == null)
|
||||
return;
|
||||
|
||||
Selection.activeObject = canvas;
|
||||
Bounds bounds = InternalEditorUtility.CalculateSelectionBounds(false, true, true);
|
||||
Vector3 point = bounds.center;
|
||||
Quaternion direction = canvas.transform.rotation;
|
||||
float size = Mathf.Max(bounds.extents.x, bounds.extents.y);
|
||||
bool ortho = canvas.renderMode == RenderMode.ScreenSpaceOverlay;
|
||||
|
||||
for (var i = 0; i < SceneView.sceneViews.Count; i++)
|
||||
{
|
||||
var view = (SceneView)SceneView.sceneViews[i];
|
||||
view.in2DMode = false;
|
||||
view.LookAt(point, direction, size, ortho, false);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetAsDirty()
|
||||
{
|
||||
IS_DIRTY = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue