修复 2021.3.2f1 工程中出现的api缺失问题

This commit is contained in:
2026-06-25 14:50:57 +08:00
parent bfce70eb6f
commit af50f72656
2 changed files with 100 additions and 70 deletions
+62 -40
View File
@@ -17,6 +17,7 @@ using UnityEngine.Events;
using System.Linq; using System.Linq;
#endif #endif
#if ENABLE_INPUT_SYSTEM #if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
#endif #endif
@@ -24,22 +25,23 @@ using UnityEngine.InputSystem;
namespace Kirurobo namespace Kirurobo
{ {
/// @cond DOXYGEN_SHOW_INTERNAL_CLASSES /// @cond DOXYGEN_SHOW_INTERNAL_CLASSES
/// <summary> /// <summary>
/// 使布尔属性可编辑 /// 使布尔属性可编辑
/// </summary> /// </summary>
[System.AttributeUsage(System.AttributeTargets.Field, Inherited = true, AllowMultiple = false)] [System.AttributeUsage(System.AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
public class EditablePropertyAttribute : UnityEngine.PropertyAttribute { } public class EditablePropertyAttribute : UnityEngine.PropertyAttribute
{
}
/// <summary> /// <summary>
/// 将属性设置为只读 /// 将属性设置为只读
/// </summary> /// </summary>
[System.AttributeUsage(System.AttributeTargets.Field, Inherited = true, AllowMultiple = false)] [System.AttributeUsage(System.AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
public class ReadOnlyAttribute : UnityEngine.PropertyAttribute { } public class ReadOnlyAttribute : UnityEngine.PropertyAttribute
{
}
/// @endcond /// @endcond
/// <summary> /// <summary>
/// Windows/Mac 统一窗口控制器 /// Windows/Mac 统一窗口控制器
/// </summary> /// </summary>
@@ -113,6 +115,7 @@ namespace Kirurobo
/// 获取当前 UniWindowController 实例 /// 获取当前 UniWindowController 实例
/// </summary> /// </summary>
public static UniWindowController current => _current ? _current : FindOrCreateInstance(); public static UniWindowController current => _current ? _current : FindOrCreateInstance();
private static UniWindowController _current; private static UniWindowController _current;
/// <summary> /// <summary>
@@ -128,6 +131,7 @@ namespace Kirurobo
get { return _isClickThrough; } get { return _isClickThrough; }
set { SetClickThrough(value); } set { SetClickThrough(value); }
} }
private bool _isClickThrough = false; private bool _isClickThrough = false;
/// <summary> /// <summary>
@@ -138,6 +142,7 @@ namespace Kirurobo
get { return _isTransparent; } get { return _isTransparent; }
set { SetTransparent(value); } set { SetTransparent(value); }
} }
[SerializeField, EditableProperty, Tooltip("选中后启动时设置为透明")] [SerializeField, EditableProperty, Tooltip("选中后启动时设置为透明")]
private bool _isTransparent = false; private bool _isTransparent = false;
@@ -149,6 +154,7 @@ namespace Kirurobo
get { return _alphaValue; } get { return _alphaValue; }
set { SetAlphaValue(value); } set { SetAlphaValue(value); }
} }
[SerializeField, EditableProperty, Tooltip("窗口透明度"), Range(0f, 1f)] [SerializeField, EditableProperty, Tooltip("窗口透明度"), Range(0f, 1f)]
private float _alphaValue = 1.0f; private float _alphaValue = 1.0f;
@@ -160,6 +166,7 @@ namespace Kirurobo
get { return ((_uniWinCore == null) ? _isTopmost : _isTopmost = _uniWinCore.IsTopmost); } get { return ((_uniWinCore == null) ? _isTopmost : _isTopmost = _uniWinCore.IsTopmost); }
set { SetTopmost(value); } set { SetTopmost(value); }
} }
[SerializeField, EditableProperty, Tooltip("选中后启动时置顶")] [SerializeField, EditableProperty, Tooltip("选中后启动时置顶")]
private bool _isTopmost = false; private bool _isTopmost = false;
@@ -171,6 +178,7 @@ namespace Kirurobo
get { return ((_uniWinCore == null) ? _isBottommost : _isBottommost = _uniWinCore.IsBottommost); } get { return ((_uniWinCore == null) ? _isBottommost : _isBottommost = _uniWinCore.IsBottommost); }
set { SetBottommost(value); } set { SetBottommost(value); }
} }
[SerializeField, EditableProperty, Tooltip("选中后启动时置底")] [SerializeField, EditableProperty, Tooltip("选中后启动时置底")]
private bool _isBottommost = false; private bool _isBottommost = false;
@@ -182,6 +190,7 @@ namespace Kirurobo
get { return ((_uniWinCore == null) ? _isZoomed : _isZoomed = _uniWinCore.GetZoomed()); } get { return ((_uniWinCore == null) ? _isZoomed : _isZoomed = _uniWinCore.GetZoomed()); }
set { SetZoomed(value); } set { SetZoomed(value); }
} }
[SerializeField, EditableProperty, Tooltip("选中后启动时最大化")] [SerializeField, EditableProperty, Tooltip("选中后启动时最大化")]
private bool _isZoomed = false; private bool _isZoomed = false;
@@ -193,6 +202,7 @@ namespace Kirurobo
get { return _shouldFitMonitor; } get { return _shouldFitMonitor; }
set { FitToMonitor(value, _monitorToFit); } set { FitToMonitor(value, _monitorToFit); }
} }
[SerializeField, EditableProperty, Tooltip("选中后将窗口适配到显示器")] [SerializeField, EditableProperty, Tooltip("选中后将窗口适配到显示器")]
private bool _shouldFitMonitor = false; private bool _shouldFitMonitor = false;
@@ -204,6 +214,7 @@ namespace Kirurobo
get { return _monitorToFit; } get { return _monitorToFit; }
set { FitToMonitor(_shouldFitMonitor, value); } set { FitToMonitor(_shouldFitMonitor, value); }
} }
private int _monitorToFit = 0; private int _monitorToFit = 0;
/// <summary> /// <summary>
@@ -214,6 +225,7 @@ namespace Kirurobo
get { return _allowDropFiles; } get { return _allowDropFiles; }
set { SetAllowDrop(value); } set { SetAllowDrop(value); }
} }
[SerializeField, EditableProperty, Tooltip("启用文件或文件夹拖放")] [SerializeField, EditableProperty, Tooltip("启用文件或文件夹拖放")]
private bool _allowDropFiles = false; private bool _allowDropFiles = false;
@@ -226,8 +238,7 @@ namespace Kirurobo
/// <summary> /// <summary>
/// 点击穿透自动判定的方法 /// 点击穿透自动判定的方法
/// </summary> /// </summary>
[Tooltip("选择方法")] [Tooltip("选择方法")] public HitTestType hitTestType = HitTestType.Opacity;
public HitTestType hitTestType = HitTestType.Opacity;
/// <summary> /// <summary>
/// 点击穿透判定方法为不透明度时使用的阈值 /// 点击穿透判定方法为不透明度时使用的阈值
@@ -244,8 +255,7 @@ namespace Kirurobo
/// <summary> /// <summary>
/// 启用后,窗口透明时会自动将相机背景改为单色黑色透明 /// 启用后,窗口透明时会自动将相机背景改为单色黑色透明
/// </summary> /// </summary>
[Header("高级设置")] [Header("高级设置")] [Tooltip("窗口透明时更改相机背景")]
[Tooltip("窗口透明时更改相机背景")]
public bool autoSwitchCameraBackground = true; public bool autoSwitchCameraBackground = true;
/// <summary> /// <summary>
@@ -255,46 +265,45 @@ namespace Kirurobo
/// 仅在启动时生效 /// 仅在启动时生效
/// 在 Mac 上,即使强制退出全屏,似乎仍会变成另一个画面,效果不大 /// 在 Mac 上,即使强制退出全屏,似乎仍会变成另一个画面,效果不大
/// </summary> /// </summary>
[Tooltip("启动时强制窗口模式")] [Tooltip("启动时强制窗口模式")] public bool forceWindowed = false;
public bool forceWindowed = false;
/// <summary> /// <summary>
/// 相机实例 /// 相机实例
/// </summary> /// </summary>
[Tooltip("未设置时使用主相机")] [Tooltip("未设置时使用主相机")] public Camera currentCamera;
public Camera currentCamera;
/// <summary> /// <summary>
/// 透明方式的指定 /// 透明方式的指定
/// </summary> /// </summary>
[Header("仅限 Windows")] [Header("仅限 Windows")] [Tooltip("选择透明方式。*仅 Windows 可用")]
[Tooltip("选择透明方式。*仅 Windows 可用")]
public TransparentType transparentType = TransparentType.Alpha; public TransparentType transparentType = TransparentType.Alpha;
/// <summary> /// <summary>
/// 当透明类型为 ColorKey 时使用的键色 /// 当透明类型为 ColorKey 时使用的键色
/// </summary> /// </summary>
[Tooltip("将在窗口下次变为透明时使用")] [Tooltip("将在窗口下次变为透明时使用")] public Color32 keyColor = new Color32(0x01, 0x00, 0x01, 0x00);
public Color32 keyColor = new Color32(0x01, 0x00, 0x01, 0x00);
/// <summary> /// <summary>
/// 在 macOS 上,是否允许将窗口放置在菜单栏上方 /// 在 macOS 上,是否允许将窗口放置在菜单栏上方
/// </summary> /// </summary>
public bool isFreePositioningEnabled public bool isFreePositioningEnabled
{ {
get { return ((_uniWinCore == null) ? _isFreePositioningEnabled : _isFreePositioningEnabled = _uniWinCore.IsFreePositioningEnabled); } get
{
return ((_uniWinCore == null)
? _isFreePositioningEnabled
: _isFreePositioningEnabled = _uniWinCore.IsFreePositioningEnabled);
}
set { SetFreePositioning(value); } set { SetFreePositioning(value); }
} }
[Header("仅限 macOS")]
[Tooltip("禁用 constrainFrameRect() *仅 macOS 可用")] [Header("仅限 macOS")] [Tooltip("禁用 constrainFrameRect() *仅 macOS 可用")] [SerializeField, EditableProperty]
[SerializeField, EditableProperty]
private bool _isFreePositioningEnabled = false; private bool _isFreePositioningEnabled = false;
/// <summary> /// <summary>
/// 鼠标指针是否在不透明像素或物体上 /// 鼠标指针是否在不透明像素或物体上
/// </summary> /// </summary>
[Header("状态")] [Header("状态")] [SerializeField, ReadOnly, Tooltip("鼠标指针是否在不透明像素上?(只读)")]
[SerializeField, ReadOnly, Tooltip("鼠标指针是否在不透明像素上?(只读)")]
private bool onObject = true; private bool onObject = true;
/// <summary> /// <summary>
@@ -366,6 +375,7 @@ namespace Kirurobo
/// 窗口样式改变时发生 /// 窗口样式改变时发生
/// </summary> /// </summary>
public event OnStateChangedDelegate OnStateChanged; public event OnStateChangedDelegate OnStateChanged;
public delegate void OnStateChangedDelegate(WindowStateEventType type); public delegate void OnStateChangedDelegate(WindowStateEventType type);
public delegate void FilesDelegate(string[] files); public delegate void FilesDelegate(string[] files);
@@ -379,6 +389,7 @@ namespace Kirurobo
/// 显示器设置或分辨率改变时发生 /// 显示器设置或分辨率改变时发生
/// </summary> /// </summary>
public event OnMonitorChangedDelegate OnMonitorChanged; public event OnMonitorChangedDelegate OnMonitorChanged;
public delegate void OnMonitorChangedDelegate(); public delegate void OnMonitorChangedDelegate();
@@ -421,7 +432,6 @@ namespace Kirurobo
{ {
originalCameraClearFlags = currentCamera.clearFlags; originalCameraClearFlags = currentCamera.clearFlags;
originalCameraBackground = currentCamera.backgroundColor; originalCameraBackground = currentCamera.backgroundColor;
} }
// 鼠标事件信息 // 鼠标事件信息
@@ -451,6 +461,7 @@ namespace Kirurobo
{ {
targetMonitorIndex = 0; targetMonitorIndex = 0;
} }
if (monitors <= targetMonitorIndex) if (monitors <= targetMonitorIndex)
{ {
targetMonitorIndex = monitors - 1; targetMonitorIndex = monitors - 1;
@@ -468,7 +479,11 @@ namespace Kirurobo
/// <returns></returns> /// <returns></returns>
private static UniWindowController FindOrCreateInstance() private static UniWindowController FindOrCreateInstance()
{ {
#if UNITY_2022_1_OR_NEWER
var instance = GameObject.FindAnyObjectByType<UniWindowController>(); var instance = GameObject.FindAnyObjectByType<UniWindowController>();
#else
var instance = GameObject.FindObjectOfType<UniWindowController>();
#endif
// 目前禁止自动创建 // 目前禁止自动创建
// // 场景中未找到时创建新实例 // // 场景中未找到时创建新实例
@@ -535,7 +550,8 @@ namespace Kirurobo
if (_uniWinCore == null || !_uniWinCore.IsActive) if (_uniWinCore == null || !_uniWinCore.IsActive)
{ {
UpdateTargetWindow(); UpdateTargetWindow();
} else }
else
{ {
_uniWinCore.Update(); _uniWinCore.Update();
} }
@@ -682,6 +698,7 @@ namespace Kirurobo
onObject = true; onObject = true;
} }
} }
yield return null; yield return null;
} }
@@ -690,7 +707,6 @@ namespace Kirurobo
/// </summary> /// </summary>
private Vector2 GetClientCursorPosition() private Vector2 GetClientCursorPosition()
{ {
// New Input System 在没有焦点时无法获取鼠标坐标,因此自行计算 // New Input System 在没有焦点时无法获取鼠标坐标,因此自行计算
Vector2 mousePos = UniWinCore.GetCursorPosition(); Vector2 mousePos = UniWinCore.GetCursorPosition();
Vector2 winPos = windowPosition; Vector2 winPos = windowPosition;
@@ -698,7 +714,7 @@ namespace Kirurobo
Vector2 unityPos = new Vector2( Vector2 unityPos = new Vector2(
(mousePos.x - winPos.x - clientRect.x) * Screen.width / clientRect.width, (mousePos.x - winPos.x - clientRect.x) * Screen.width / clientRect.width,
(mousePos.y - winPos.y - clientRect.y) * Screen.height / clientRect.height (mousePos.y - winPos.y - clientRect.y) * Screen.height / clientRect.height
); );
// // 调试用 // // 调试用
// // 与 Unity 获取的值进行比较 // // 与 Unity 获取的值进行比较
@@ -715,13 +731,13 @@ namespace Kirurobo
// 在编辑器中始终使用 Unity 功能获取鼠标坐标 // 在编辑器中始终使用 Unity 功能获取鼠标坐标
// Game 窗口可能不是唯一窗口,或 Scale 不同,无法简单计算 // Game 窗口可能不是唯一窗口,或 Scale 不同,无法简单计算
#if UNITY_EDITOR #if UNITY_EDITOR
#if ENABLE_LEGACY_INPUT_MANAGER #if ENABLE_LEGACY_INPUT_MANAGER
return Input.mousePosition; return Input.mousePosition;
#elif UNITY_EDITOR && ENABLE_INPUT_SYSTEM #elif UNITY_EDITOR && ENABLE_INPUT_SYSTEM
return Mouse.current.position.ReadValue(); return Mouse.current.position.ReadValue();
#else #else
return unityPos; return unityPos;
#endif #endif
#else #else
return unityPos; return unityPos;
#endif #endif
@@ -762,8 +778,8 @@ namespace Kirurobo
// 在屏幕外则视为透明 // 在屏幕外则视为透明
if ( if (
mousePos.x < 0 || mousePos.x >= w mousePos.x < 0 || mousePos.x >= w
|| mousePos.y < 0 || mousePos.y >= h || mousePos.y < 0 || mousePos.y >= h
) )
{ {
return false; return false;
} }
@@ -775,14 +791,14 @@ namespace Kirurobo
if (transparentType == TransparentType.ColorKey) return true; if (transparentType == TransparentType.ColorKey) return true;
// 根据指定坐标的绘制结果进行判断 // 根据指定坐标的绘制结果进行判断
try // 在 WaitForEndOfFrame 时机执行的话,不需要 try 应该也没问题 try // 在 WaitForEndOfFrame 时机执行的话,不需要 try 应该也没问题
{ {
// 参考 http://tsubakit1.hateblo.jp/entry/20131203/1386000440 // 参考 http://tsubakit1.hateblo.jp/entry/20131203/1386000440
colorPickerTexture.ReadPixels(new Rect(mousePos, Vector2.one), 0, 0); colorPickerTexture.ReadPixels(new Rect(mousePos, Vector2.one), 0, 0);
Color color = colorPickerTexture.GetPixels32()[0]; Color color = colorPickerTexture.GetPixels32()[0];
pickedColor = color; pickedColor = color;
return (color.a >= opacityThreshold); // alpha 达到阈值则视为不透明 return (color.a >= opacityThreshold); // alpha 达到阈值则视为不透明
} }
catch (System.Exception ex) catch (System.Exception ex)
{ {
@@ -838,7 +854,8 @@ namespace Kirurobo
onObject = true; onObject = true;
return; return;
} }
} else }
else
{ {
// 如果相机无效,则获取主相机 // 如果相机无效,则获取主相机
currentCamera = Camera.main; currentCamera = Camera.main;
@@ -878,6 +895,7 @@ namespace Kirurobo
{ {
SetTopmost(_isTopmost); SetTopmost(_isTopmost);
} }
SetZoomed(_isZoomed); SetZoomed(_isZoomed);
SetClickThrough(_isClickThrough); SetClickThrough(_isClickThrough);
SetAllowDrop(_allowDropFiles); SetAllowDrop(_allowDropFiles);
@@ -889,11 +907,11 @@ namespace Kirurobo
} }
else else
{ {
#if UNITY_EDITOR #if UNITY_EDITOR
// 在编辑器中,由于 Game 视图可能被关闭或停靠,如果发生变化则更改目标窗口 // 在编辑器中,由于 Game 视图可能被关闭或停靠,如果发生变化则更改目标窗口
// 如果活动窗口与当前目标相同,则不执行任何操作 // 如果活动窗口与当前目标相同,则不执行任何操作
_uniWinCore.AttachMyActiveWindow(); _uniWinCore.AttachMyActiveWindow();
#endif #endif
} }
} }
@@ -974,7 +992,8 @@ namespace Kirurobo
/// <param name="type"></param> /// <param name="type"></param>
public void SetTransparentType(TransparentType type) public void SetTransparentType(TransparentType type)
{ {
if (_uniWinCore != null) { if (_uniWinCore != null)
{
// 如果正在透明中,则先解除再重新透明 // 如果正在透明中,则先解除再重新透明
if (_isTransparent) if (_isTransparent)
{ {
@@ -1081,6 +1100,7 @@ namespace Kirurobo
{ {
return new Rect(position, size); return new Rect(position, size);
} }
return Rect.zero; return Rect.zero;
} }
@@ -1218,9 +1238,11 @@ namespace Kirurobo
[Obsolete] [Obsolete]
public int GetDebugInfo() public int GetDebugInfo()
{ {
if (_uniWinCore != null) { if (_uniWinCore != null)
{
return UniWinCore.GetDebugInfo(); return UniWinCore.GetDebugInfo();
} }
return 0; return 0;
} }
} }
+13 -5
View File
@@ -10,22 +10,23 @@ using System.Collections.Generic;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
#if ENABLE_LEGACY_INPUT_MANAGER #if ENABLE_LEGACY_INPUT_MANAGER
#elif ENABLE_INPUT_SYSTEM #elif ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
#endif #endif
namespace Kirurobo namespace Kirurobo
{ {
public class UniWindowMoveHandle : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler, IPointerUpHandler public class UniWindowMoveHandle : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler,
IPointerUpHandler
{ {
private UniWindowController _uniwinc; private UniWindowController _uniwinc;
/// <summary> /// <summary>
/// 窗口最大化时是否禁用移动 /// 窗口最大化时是否禁用移动
/// </summary> /// </summary>
[Tooltip("窗口已最大化(缩放)时禁用拖拽移动。")] [Tooltip("窗口已最大化(缩放)时禁用拖拽移动。")] public bool disableOnZoomed = true;
public bool disableOnZoomed = true;
/// <summary> /// <summary>
/// 拖动中则为 true /// 拖动中则为 true
@@ -34,6 +35,7 @@ namespace Kirurobo
{ {
get { return _isDragging; } get { return _isDragging; }
} }
private bool _isDragging = false; private bool _isDragging = false;
/// <summary> /// <summary>
@@ -66,7 +68,11 @@ namespace Kirurobo
void Start() void Start()
{ {
// 获取场景中的 UniWindowController // 获取场景中的 UniWindowController
#if UNITY_2022_1_OR_NEWER
_uniwinc = GameObject.FindAnyObjectByType<UniWindowController>(); _uniwinc = GameObject.FindAnyObjectByType<UniWindowController>();
#else
_uniwinc = GameObject.FindObjectOfType<UniWindowController>();
#endif
if (_uniwinc) _isHitTestEnabled = _uniwinc.isHitTestEnabled; if (_uniwinc) _isHitTestEnabled = _uniwinc.isHitTestEnabled;
//// 下面的代码似乎不需要,所以注释掉以免擅自更改 //// 下面的代码似乎不需要,所以注释掉以免擅自更改
@@ -132,6 +138,7 @@ namespace Kirurobo
{ {
_uniwinc.isHitTestEnabled = _isHitTestEnabled; _uniwinc.isHitTestEnabled = _isHitTestEnabled;
} }
_isDragging = false; _isDragging = false;
} }
@@ -158,7 +165,8 @@ namespace Kirurobo
// 如果鼠标按钮已松开,则结束拖动 // 如果鼠标按钮已松开,则结束拖动
var buttons = UniWindowController.GetMouseButtons(); var buttons = UniWindowController.GetMouseButtons();
if ((buttons & UniWindowController.MouseButton.Left) == UniWindowController.MouseButton.None) { if ((buttons & UniWindowController.MouseButton.Left) == UniWindowController.MouseButton.None)
{
EndDragging(); EndDragging();
return; return;
} }