添加窗口任务栏功能,添加窗口的管理功能
This commit is contained in:
@@ -7,7 +7,6 @@ namespace XericUI.Form
|
||||
{
|
||||
#region 枚举与结构体
|
||||
|
||||
/// <summary>范围坐标空间枚举</summary>
|
||||
public enum FormRangeSpace
|
||||
{
|
||||
RelativeOffset,
|
||||
@@ -22,7 +21,7 @@ namespace XericUI.Form
|
||||
[Tooltip("坐标空间")]
|
||||
public FormRangeSpace space;
|
||||
|
||||
[Tooltip("范围定义。\nRelativeOffset: x,y=像素偏移; w,h=像素宽高(0=用参考尺寸)\nCanvasLocal: Canvas局部坐标\nAnchorToSelf: x,y=锚点0-1; w,h=尺寸比例0-1\nAnchorToCanvas: x,y=锚点0-1; w,h=尺寸比例0-1")]
|
||||
[Tooltip("范围定义")]
|
||||
public Rect rect;
|
||||
}
|
||||
|
||||
@@ -30,31 +29,31 @@ namespace XericUI.Form
|
||||
|
||||
/// <summary>
|
||||
/// UI 窗体控制器 — 挂载到窗口根 GameObject 上,自身即为完整窗口。
|
||||
/// 支持全屏/还原、边缘吸附、自动隐藏、拖拽移动。
|
||||
/// 标题栏功能由独立的 UITitleBar 子组件提供,Controller 只提供公开 API。
|
||||
/// 支持全屏/还原、边缘吸附、自动隐藏、拖拽移动、置顶。
|
||||
/// 通过 WindowEntry 与 Manager 通信。
|
||||
/// </summary>
|
||||
[AddComponentMenu("Xeric UI Vessel/Form/UI Form Controller")]
|
||||
[RequireComponent(typeof(RectTransform))]
|
||||
public class UIFormController : MonoBehaviour, IUIFormTarget, IBeginDragHandler, IDragHandler, IEndDragHandler
|
||||
public class UIFormController : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerDownHandler
|
||||
{
|
||||
#region 序列化字段
|
||||
|
||||
[Header("窗口基本设置")]
|
||||
[SerializeField, Tooltip("窗口标题,供外部读取")]
|
||||
[SerializeField, Tooltip("窗口标题")]
|
||||
private string m_WindowTitle = "Window";
|
||||
|
||||
[Header("边缘吸附")]
|
||||
[SerializeField, Tooltip("是否启用边缘自动吸附")]
|
||||
[SerializeField, Tooltip("启用边缘自动吸附")]
|
||||
private bool m_EnableAutoSnap = true;
|
||||
|
||||
[SerializeField, Tooltip("吸附阈值(像素),窗口距边缘小于此值时自动贴边")]
|
||||
[SerializeField, Tooltip("吸附阈值(像素)")]
|
||||
private float m_SnapThreshold = 8f;
|
||||
|
||||
[Header("自动隐藏")]
|
||||
[SerializeField, Tooltip("是否启用自动隐藏(贴边后滑出屏幕)")]
|
||||
[SerializeField, Tooltip("启用自动隐藏")]
|
||||
private bool m_EnableAutoHide;
|
||||
|
||||
[SerializeField, Tooltip("自动隐藏的滑动方向")]
|
||||
[SerializeField, Tooltip("自动隐藏滑动方向")]
|
||||
private AutoHideDirection m_AutoHideDirection = AutoHideDirection.Left;
|
||||
|
||||
[Header("调试")]
|
||||
@@ -69,28 +68,29 @@ namespace XericUI.Form
|
||||
#region 公开属性
|
||||
|
||||
public string WindowTitle => m_WindowTitle;
|
||||
|
||||
/// <summary>是否处于全屏状态</summary>
|
||||
public bool IsMaximized => _windowState == WindowState.Maximized;
|
||||
|
||||
/// <summary>是否处于边缘隐藏状态</summary>
|
||||
public bool IsEdgeHidden => _windowState == WindowState.EdgeHidden;
|
||||
|
||||
/// <summary>当前窗口状态</summary>
|
||||
public WindowState CurrentState => _windowState;
|
||||
|
||||
/// <summary>自动吸附是否开启</summary>
|
||||
public bool AutoSnapEnabled => m_EnableAutoSnap;
|
||||
|
||||
/// <summary>自动隐藏是否开启</summary>
|
||||
public bool AutoHideEnabled => m_EnableAutoHide;
|
||||
|
||||
/// <summary>自动隐藏方向</summary>
|
||||
public AutoHideDirection HideDirection => m_AutoHideDirection;
|
||||
|
||||
/// <summary>编辑器调试模式</summary>
|
||||
public bool DebugMode => m_DebugMode;
|
||||
|
||||
/// <summary>窗口数据 Entry(由 Manager 在 CreateWindow 时注入)</summary>
|
||||
public WindowEntry Entry
|
||||
{
|
||||
get => _entry;
|
||||
set
|
||||
{
|
||||
_entry = value;
|
||||
if (_entry != null)
|
||||
m_WindowTitle = _entry.Title;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>父级 Manager 引用</summary>
|
||||
public UIFormManager Manager => _manager;
|
||||
|
||||
#endregion
|
||||
|
||||
#region 内部状态
|
||||
@@ -98,13 +98,15 @@ namespace XericUI.Form
|
||||
private RectTransform _rectTransform;
|
||||
private Canvas _canvas;
|
||||
private RectTransform _canvasRt;
|
||||
private UIFormManager _manager;
|
||||
private WindowEntry _entry;
|
||||
|
||||
private WindowState _windowState = WindowState.Normal;
|
||||
|
||||
// 拖拽
|
||||
private Vector2 _dragOffset;
|
||||
|
||||
// 全屏还原数据
|
||||
// 全屏还原
|
||||
private Vector2 _savePos;
|
||||
private Vector2 _saveSize;
|
||||
private Vector2 _saveAnchorMin;
|
||||
@@ -125,110 +127,72 @@ namespace XericUI.Form
|
||||
protected virtual void Awake()
|
||||
{
|
||||
_rectTransform = (RectTransform)transform;
|
||||
_manager = GetComponentInParent<UIFormManager>();
|
||||
_canvas = GetComponentInParent<Canvas>();
|
||||
_canvasRt = _canvas != null ? (RectTransform)_canvas.transform : null;
|
||||
}
|
||||
|
||||
protected virtual void OnEnable()
|
||||
{
|
||||
if (UIFormManager.Main != null)
|
||||
UIFormManager.Main.RegisterWindow(this);
|
||||
}
|
||||
|
||||
protected virtual void OnDisable()
|
||||
{
|
||||
StopAutoHideMonitor();
|
||||
|
||||
if (_windowState == WindowState.EdgeHidden && UIFormManager.Main != null)
|
||||
UIFormManager.Main.UnregisterEdgeHidden(this);
|
||||
|
||||
if (UIFormManager.Main != null)
|
||||
{
|
||||
UIFormManager.Main.UnregisterWindow(this);
|
||||
UIFormManager.Main.UnregisterEdgeHidden(this);
|
||||
}
|
||||
if (_windowState == WindowState.EdgeHidden && _manager != null)
|
||||
_manager.UnregisterEdgeHidden(_entry);
|
||||
|
||||
#if UNITY_EDITOR
|
||||
UnsubscribeEditorUpdate();
|
||||
#endif
|
||||
_isHovering = false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IUIFormTarget 实现
|
||||
#region === 公开 API ===
|
||||
|
||||
public bool IsActive => isActiveAndEnabled;
|
||||
|
||||
public Rect GetEnterRect() => GetWindowScreenRect();
|
||||
public Rect GetExitRect() => GetWindowScreenRect();
|
||||
|
||||
public void OnPointerEnter()
|
||||
{
|
||||
if (m_DebugLogEvents)
|
||||
Debug.Log($"[UIFormController] 鼠标进入窗口范围: {name}", this);
|
||||
}
|
||||
|
||||
public void OnPointerExit()
|
||||
{
|
||||
if (m_DebugLogEvents)
|
||||
Debug.Log($"[UIFormController] 鼠标离开窗口范围: {name}", this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region === 公开 API(供 UITitleBar 及外部调用) ===
|
||||
|
||||
/// <summary>全屏/还原切换</summary>
|
||||
public void ToggleMaximize()
|
||||
{
|
||||
if (IsMaximized)
|
||||
RestoreFromFullscreen();
|
||||
else
|
||||
MaximizeToFullscreen();
|
||||
|
||||
if (_entry != null)
|
||||
_entry.State = _windowState;
|
||||
}
|
||||
|
||||
/// <summary>最小化窗口</summary>
|
||||
public void Minimize()
|
||||
{
|
||||
if (_windowState == WindowState.Minimized) return;
|
||||
if (UIFormManager.Main == null) return;
|
||||
|
||||
UIFormManager.Main.MinimizeWindow(this);
|
||||
if (_manager == null || _entry == null) return;
|
||||
_manager.MinimizeWindow(_entry.Id);
|
||||
}
|
||||
|
||||
/// <summary>关闭窗口(Destroy GameObject)</summary>
|
||||
public void Close()
|
||||
{
|
||||
if (UIFormManager.Main != null)
|
||||
UIFormManager.Main.CloseWindow(this);
|
||||
|
||||
if (Application.isPlaying)
|
||||
if (_manager != null && _entry != null)
|
||||
_manager.DestroyWindow(_entry.Id);
|
||||
else if (Application.isPlaying)
|
||||
Destroy(gameObject);
|
||||
else
|
||||
DestroyImmediate(gameObject);
|
||||
}
|
||||
|
||||
/// <summary>设置边缘吸附开关</summary>
|
||||
public void SetAutoSnapEnabled(bool enabled)
|
||||
{
|
||||
m_EnableAutoSnap = enabled;
|
||||
}
|
||||
|
||||
/// <summary>设置自动隐藏开关</summary>
|
||||
public void SetAutoSnapEnabled(bool enabled) => m_EnableAutoSnap = enabled;
|
||||
public void SetAutoHideEnabled(bool enabled)
|
||||
{
|
||||
m_EnableAutoHide = enabled;
|
||||
|
||||
if (!enabled && _windowState == WindowState.EdgeHidden)
|
||||
{
|
||||
// 关闭自动隐藏 → 还原窗口
|
||||
SlideInFromEdge();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>设置自动隐藏方向</summary>
|
||||
public void SetAutoHideDirection(AutoHideDirection direction)
|
||||
public void SetAutoHideDirection(AutoHideDirection direction) => m_AutoHideDirection = direction;
|
||||
|
||||
/// <summary>置顶窗口</summary>
|
||||
public void MoveToFront()
|
||||
{
|
||||
m_AutoHideDirection = direction;
|
||||
transform.SetAsLastSibling();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -250,9 +214,6 @@ namespace XericUI.Form
|
||||
_rectTransform.offsetMax = Vector2.zero;
|
||||
|
||||
_windowState = WindowState.Maximized;
|
||||
|
||||
if (m_DebugLogEvents)
|
||||
Debug.Log($"[UIFormController] 窗口全屏: {name}", this);
|
||||
}
|
||||
|
||||
private void RestoreFromFullscreen()
|
||||
@@ -265,25 +226,27 @@ namespace XericUI.Form
|
||||
_rectTransform.sizeDelta = _saveSize;
|
||||
|
||||
_windowState = WindowState.Normal;
|
||||
|
||||
if (m_DebugLogEvents)
|
||||
Debug.Log($"[UIFormController] 窗口还原: {name}", this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region === 拖拽 ===
|
||||
#region === 拖拽 & 点击聚焦 ===
|
||||
|
||||
public void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
// 点击窗口任意位置 → 置顶
|
||||
transform.SetAsLastSibling();
|
||||
if (_manager != null && _entry != null)
|
||||
_manager.MoveToFront(_entry.Id);
|
||||
}
|
||||
|
||||
public void OnBeginDrag(PointerEventData eventData)
|
||||
{
|
||||
if (_windowState == WindowState.EdgeHidden || _windowState == WindowState.Minimized)
|
||||
return;
|
||||
if (_windowState == WindowState.EdgeHidden || _windowState == WindowState.Minimized) return;
|
||||
|
||||
// 全屏拖拽 → 先还原
|
||||
if (IsMaximized)
|
||||
{
|
||||
RestoreFromFullscreen();
|
||||
// 将窗口移到鼠标附近
|
||||
_rectTransform.anchoredPosition = eventData.position;
|
||||
}
|
||||
|
||||
@@ -292,8 +255,7 @@ namespace XericUI.Form
|
||||
|
||||
public void OnDrag(PointerEventData eventData)
|
||||
{
|
||||
if (_windowState == WindowState.EdgeHidden || _windowState == WindowState.Minimized)
|
||||
return;
|
||||
if (_windowState == WindowState.EdgeHidden || _windowState == WindowState.Minimized) return;
|
||||
|
||||
_rectTransform.anchoredPosition = eventData.position + _dragOffset;
|
||||
|
||||
@@ -303,14 +265,10 @@ namespace XericUI.Form
|
||||
|
||||
public void OnEndDrag(PointerEventData eventData)
|
||||
{
|
||||
if (_windowState == WindowState.EdgeHidden || _windowState == WindowState.Minimized)
|
||||
return;
|
||||
if (_windowState == WindowState.EdgeHidden || _windowState == WindowState.Minimized) return;
|
||||
|
||||
// 拖拽结束 → 如果贴边且启用自动隐藏,则触发
|
||||
if (m_EnableAutoHide && IsSnappedToEdge())
|
||||
{
|
||||
AutoHideToEdge();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -320,54 +278,35 @@ namespace XericUI.Form
|
||||
private void CheckEdgeSnap()
|
||||
{
|
||||
if (_canvasRt == null) return;
|
||||
|
||||
Vector2 canvasSize = _canvasRt.rect.size;
|
||||
Vector2 pos = _rectTransform.anchoredPosition;
|
||||
Vector2 size = _rectTransform.rect.size;
|
||||
|
||||
bool snapped = false;
|
||||
|
||||
// 左
|
||||
if (Mathf.Abs(pos.x) < m_SnapThreshold)
|
||||
{
|
||||
pos.x = 0;
|
||||
snapped = true;
|
||||
}
|
||||
// 右
|
||||
else if (Mathf.Abs(pos.x + size.x - canvasSize.x) < m_SnapThreshold)
|
||||
{
|
||||
pos.x = canvasSize.x - size.x;
|
||||
snapped = true;
|
||||
}
|
||||
|
||||
_rectTransform.anchoredPosition = pos;
|
||||
}
|
||||
|
||||
/// <summary>检测窗口是否贴边</summary>
|
||||
private bool IsSnappedToEdge()
|
||||
{
|
||||
if (_canvasRt == null) return false;
|
||||
|
||||
Vector2 canvasSize = _canvasRt.rect.size;
|
||||
Vector2 pos = _rectTransform.anchoredPosition;
|
||||
Vector2 size = _rectTransform.rect.size;
|
||||
|
||||
return Mathf.Abs(pos.x) < 1f
|
||||
|| Mathf.Abs(pos.x + size.x - canvasSize.x) < 1f;
|
||||
return Mathf.Abs(pos.x) < 1f || Mathf.Abs(pos.x + size.x - canvasSize.x) < 1f;
|
||||
}
|
||||
|
||||
/// <summary>获取最近的边缘方向</summary>
|
||||
public AutoHideDirection GetClosestEdge()
|
||||
{
|
||||
if (_canvasRt == null) return AutoHideDirection.Left;
|
||||
|
||||
Vector2 canvasSize = _canvasRt.rect.size;
|
||||
Vector2 pos = _rectTransform.anchoredPosition;
|
||||
Vector2 size = _rectTransform.rect.size;
|
||||
|
||||
float distLeft = Mathf.Abs(pos.x);
|
||||
float distRight = Mathf.Abs(pos.x + size.x - canvasSize.x);
|
||||
|
||||
return distLeft < distRight ? AutoHideDirection.Left : AutoHideDirection.Right;
|
||||
}
|
||||
|
||||
@@ -377,8 +316,7 @@ namespace XericUI.Form
|
||||
|
||||
private void AutoHideToEdge()
|
||||
{
|
||||
if (_windowState == WindowState.EdgeHidden) return;
|
||||
if (_canvasRt == null) return;
|
||||
if (_windowState == WindowState.EdgeHidden || _canvasRt == null) return;
|
||||
|
||||
Vector2 canvasSize = _canvasRt.rect.size;
|
||||
Vector2 pos = _rectTransform.anchoredPosition;
|
||||
@@ -390,58 +328,40 @@ namespace XericUI.Form
|
||||
if (dir == AutoHideDirection.Left || dir == AutoHideDirection.Right)
|
||||
dir = GetClosestEdge();
|
||||
|
||||
switch (dir)
|
||||
_edgeHiddenPos = dir switch
|
||||
{
|
||||
case AutoHideDirection.Left:
|
||||
_edgeHiddenPos = new Vector2(-size.x, pos.y);
|
||||
break;
|
||||
case AutoHideDirection.Right:
|
||||
_edgeHiddenPos = new Vector2(canvasSize.x, pos.y);
|
||||
break;
|
||||
case AutoHideDirection.Down:
|
||||
_edgeHiddenPos = new Vector2(pos.x, -size.y);
|
||||
break;
|
||||
case AutoHideDirection.Up:
|
||||
_edgeHiddenPos = new Vector2(pos.x, canvasSize.y);
|
||||
break;
|
||||
}
|
||||
AutoHideDirection.Left => new Vector2(-size.x, pos.y),
|
||||
AutoHideDirection.Right => new Vector2(canvasSize.x, pos.y),
|
||||
AutoHideDirection.Down => new Vector2(pos.x, -size.y),
|
||||
AutoHideDirection.Up => new Vector2(pos.x, canvasSize.y),
|
||||
_ => pos
|
||||
};
|
||||
|
||||
_rectTransform.anchoredPosition = _edgeHiddenPos;
|
||||
_windowState = WindowState.EdgeHidden;
|
||||
|
||||
if (UIFormManager.Main != null)
|
||||
UIFormManager.Main.RegisterEdgeHidden(this, dir);
|
||||
|
||||
if (m_DebugLogEvents)
|
||||
Debug.Log($"[UIFormController] 自动隐藏 (方向={dir}): {name}", this);
|
||||
if (_manager != null && _entry != null)
|
||||
_manager.RegisterEdgeHidden(_entry, dir);
|
||||
}
|
||||
|
||||
/// <summary>由 Manager 调用 — 从边缘滑出</summary>
|
||||
public void SlideInFromEdge()
|
||||
{
|
||||
if (_windowState != WindowState.EdgeHidden) return;
|
||||
|
||||
StopAutoHideMonitor();
|
||||
_rectTransform.anchoredPosition = _edgeVisiblePos;
|
||||
_windowState = WindowState.Normal;
|
||||
|
||||
if (UIFormManager.Main != null)
|
||||
UIFormManager.Main.UnregisterEdgeHidden(this);
|
||||
if (_manager != null)
|
||||
_manager.UnregisterEdgeHidden(_entry);
|
||||
|
||||
// 开始监控鼠标是否离开窗口
|
||||
_autoHideCoroutine = StartCoroutine(MonitorMouseExit());
|
||||
|
||||
if (m_DebugLogEvents)
|
||||
Debug.Log($"[UIFormController] 从边缘滑出: {name}", this);
|
||||
}
|
||||
|
||||
/// <summary>监控鼠标是否离开窗口范围,离开则自动隐藏</summary>
|
||||
private IEnumerator MonitorMouseExit()
|
||||
{
|
||||
while (_windowState == WindowState.Normal && m_EnableAutoHide)
|
||||
{
|
||||
yield return null;
|
||||
|
||||
Rect screenRect = GetWindowScreenRect();
|
||||
if (!screenRect.Contains(Input.mousePosition))
|
||||
{
|
||||
@@ -464,82 +384,29 @@ namespace XericUI.Form
|
||||
|
||||
#region === 窗口屏幕范围 ===
|
||||
|
||||
/// <summary>获取窗口自身在屏幕空间中的 Rect</summary>
|
||||
public Rect GetWindowScreenRect()
|
||||
{
|
||||
if (_rectTransform == null || _canvas == null) return Rect.zero;
|
||||
|
||||
Vector3[] corners = new Vector3[4];
|
||||
_rectTransform.GetWorldCorners(corners);
|
||||
|
||||
Camera cam = _canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : _canvas.worldCamera;
|
||||
|
||||
Vector2 min = Vector2.positiveInfinity;
|
||||
Vector2 max = Vector2.negativeInfinity;
|
||||
|
||||
Vector2 min = Vector2.positiveInfinity, max = Vector2.negativeInfinity;
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
Vector2 sp = cam != null
|
||||
? RectTransformUtility.WorldToScreenPoint(cam, corners[i])
|
||||
: (Vector2)corners[i];
|
||||
|
||||
min = Vector2.Min(min, sp);
|
||||
max = Vector2.Max(max, sp);
|
||||
}
|
||||
|
||||
return new Rect(min.x, min.y, max.x - min.x, max.y - min.y);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region === 悬停检测(供 Manager 调用) ===
|
||||
|
||||
public bool CheckHover(Vector2 screenPoint)
|
||||
{
|
||||
Rect windowRect = GetWindowScreenRect();
|
||||
|
||||
if (!_isHovering)
|
||||
{
|
||||
if (windowRect.Contains(screenPoint))
|
||||
{
|
||||
_isHovering = true;
|
||||
OnPointerEnter();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!windowRect.Contains(screenPoint))
|
||||
{
|
||||
_isHovering = false;
|
||||
OnPointerExit();
|
||||
}
|
||||
}
|
||||
|
||||
return _isHovering;
|
||||
}
|
||||
|
||||
public void SimulateEnter()
|
||||
{
|
||||
_isHovering = true;
|
||||
|
||||
if (m_DebugLogEvents)
|
||||
Debug.Log($"[UIFormController] 模拟进入: {name}", this);
|
||||
}
|
||||
|
||||
public void SimulateExit()
|
||||
{
|
||||
_isHovering = false;
|
||||
|
||||
if (m_DebugLogEvents)
|
||||
Debug.Log($"[UIFormController] 模拟退出: {name}", this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 编辑器调试
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
[System.NonSerialized] private bool _editorDebugSubscribed;
|
||||
[System.NonSerialized] private Vector2 _editorMouseScreenPos;
|
||||
|
||||
@@ -559,87 +426,53 @@ namespace XericUI.Form
|
||||
|
||||
protected virtual void OnValidate()
|
||||
{
|
||||
if (m_DebugMode)
|
||||
SubscribeEditorUpdate();
|
||||
else
|
||||
UnsubscribeEditorUpdate();
|
||||
if (m_DebugMode) SubscribeEditorUpdate();
|
||||
else UnsubscribeEditorUpdate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SceneView 重绘回调 — 在 GUI 上下文中获取鼠标屏幕坐标并执行悬停检测。
|
||||
/// 使用 GUIUtility.GUIToScreenPoint 将 SceneView 内的 GUI 坐标转为 Unity 屏幕坐标。
|
||||
/// </summary>
|
||||
private void OnSceneGUI(UnityEditor.SceneView sceneView)
|
||||
{
|
||||
if (this == null)
|
||||
{
|
||||
UnsubscribeEditorUpdate();
|
||||
return;
|
||||
}
|
||||
if (this == null) { UnsubscribeEditorUpdate(); return; }
|
||||
if (Application.isPlaying || !m_DebugMode) return;
|
||||
|
||||
if (Application.isPlaying) return;
|
||||
if (!m_DebugMode) return;
|
||||
|
||||
// Event.current.mousePosition 是相对于 SceneView 窗口的 GUI 坐标
|
||||
// GUIUtility.GUIToScreenPoint 将其转换为屏幕坐标(左下角为原点)
|
||||
Vector2 guiPos = Event.current.mousePosition;
|
||||
_editorMouseScreenPos = GUIUtility.GUIToScreenPoint(guiPos);
|
||||
|
||||
CheckHover(_editorMouseScreenPos);
|
||||
Rect rect = GetWindowScreenRect();
|
||||
_isHovering = rect.Contains(_editorMouseScreenPos);
|
||||
}
|
||||
|
||||
protected virtual void OnDrawGizmosSelected()
|
||||
{
|
||||
if (!m_DebugMode) return;
|
||||
if (_canvas == null) return;
|
||||
if (_canvasRt == null) return;
|
||||
|
||||
if (!m_DebugMode || _canvas == null || _canvasRt == null) return;
|
||||
Rect screenRect = GetWindowScreenRect();
|
||||
if (screenRect.width <= 0 || screenRect.height <= 0) return;
|
||||
|
||||
// 悬停时高亮边框
|
||||
Color borderColor = _isHovering
|
||||
? new Color(0f, 1f, 0f, 0.6f)
|
||||
: new Color(0f, 0.7f, 1f, 0.3f);
|
||||
Gizmos.color = borderColor;
|
||||
|
||||
DrawScreenRectOnCanvas(screenRect, borderColor, _canvas, _canvasRt);
|
||||
|
||||
// 边缘吸附阈值可视化
|
||||
if (m_EnableAutoSnap)
|
||||
{
|
||||
Gizmos.color = new Color(1f, 0.6f, 0f, 0.15f);
|
||||
DrawScreenRectOnCanvas(
|
||||
new Rect(screenRect.x + m_SnapThreshold, screenRect.y,
|
||||
screenRect.width - m_SnapThreshold * 2, screenRect.height),
|
||||
Gizmos.color, _canvas, _canvasRt);
|
||||
}
|
||||
}
|
||||
|
||||
private static void DrawScreenRectOnCanvas(Rect screenRect, Color color, Canvas canvas, RectTransform canvasRt)
|
||||
{
|
||||
if (screenRect.width <= 0 || screenRect.height <= 0) return;
|
||||
|
||||
Gizmos.color = color;
|
||||
Camera uiCam = canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : canvas.worldCamera;
|
||||
|
||||
Vector3 p0 = ScreenToWorldOnCanvas(new Vector3(screenRect.xMin, screenRect.yMin), uiCam, canvasRt);
|
||||
Vector3 p1 = ScreenToWorldOnCanvas(new Vector3(screenRect.xMax, screenRect.yMin), uiCam, canvasRt);
|
||||
Vector3 p2 = ScreenToWorldOnCanvas(new Vector3(screenRect.xMax, screenRect.yMax), uiCam, canvasRt);
|
||||
Vector3 p3 = ScreenToWorldOnCanvas(new Vector3(screenRect.xMin, screenRect.yMax), uiCam, canvasRt);
|
||||
|
||||
Gizmos.DrawLine(p0, p1);
|
||||
Gizmos.DrawLine(p1, p2);
|
||||
Gizmos.DrawLine(p2, p3);
|
||||
Gizmos.DrawLine(p3, p0);
|
||||
Vector3 p0 = Stw(new Vector3(screenRect.xMin, screenRect.yMin), uiCam, canvasRt);
|
||||
Vector3 p1 = Stw(new Vector3(screenRect.xMax, screenRect.yMin), uiCam, canvasRt);
|
||||
Vector3 p2 = Stw(new Vector3(screenRect.xMax, screenRect.yMax), uiCam, canvasRt);
|
||||
Vector3 p3 = Stw(new Vector3(screenRect.xMin, screenRect.yMax), uiCam, canvasRt);
|
||||
Gizmos.DrawLine(p0, p1); Gizmos.DrawLine(p1, p2);
|
||||
Gizmos.DrawLine(p2, p3); Gizmos.DrawLine(p3, p0);
|
||||
}
|
||||
|
||||
private static Vector3 ScreenToWorldOnCanvas(Vector3 screenPoint, Camera uiCamera, RectTransform canvasRt)
|
||||
private static Vector3 Stw(Vector3 sp, Camera cam, RectTransform rt)
|
||||
{
|
||||
RectTransformUtility.ScreenPointToWorldPointInRectangle(canvasRt, (Vector2)screenPoint, uiCamera, out Vector3 wp);
|
||||
RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, (Vector2)sp, cam, out Vector3 wp);
|
||||
return wp;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user