Files
Webview_SgConfig_Unity_IOS/Assets/Scripts/FGUIClickDetection.cs
T

140 lines
5.0 KiB
C#
Raw Normal View History

2026-07-15 16:19:07 +08:00
using System.Collections.Generic;
using FairyGUI;
using ScrewsMaster;
using UnityEngine;
public class FGUIClickDetection : MonoBehaviour
{
void Update()
{
if (Input.GetMouseButtonDown(0)) // 检测鼠标左键或手指点击
{
// 获取鼠标在 Unity 屏幕坐标系中的位置
Vector2 screenPos = Input.mousePosition;
// 处理坐标原点差异,将屏幕坐标的 Y 值翻转
screenPos.y = Screen.height - screenPos.y;
// 计算屏幕与 FairyGUI UI 的宽高比例
float scaleX = Screen.width / GRoot.inst.width;
float scaleY = Screen.height / GRoot.inst.height;
// 将屏幕坐标缩放到 FairyGUI 的 UI 坐标系
Vector2 fguiScreenPos = new Vector2(screenPos.x / scaleX, screenPos.y / scaleY);
// 最后将转换后的坐标传递给 FairyGUI 的本地坐标系
// Vector2 fguiPos = GRoot.inst.GlobalToLocal(fguiScreenPos);
// WebviewManager.Instance.HandleUIElements(fguiScreenPos);
// GetInteractiveObjectAtScreenPosition(screenPosition);
// 将屏幕坐标转换为FGUI坐标
// Vector2 localPos = GRoot.inst.GlobalToLocal(screenPosition);
// 使用HitTest获取点击位置下的GObject
// 如果检测到对象
// if (hitObject != null)
// {
// // 判断类型并触发相应操作
// if (hitObject is GButton button)
// {
// // 触发按钮点击事件
// button.onClick.Call();
// }
// else if (hitObject is GTextInput textInput)
// {
// // 触发文本输入框的焦点
// textInput.RequestFocus();
// }
// else if (hitObject is GSlider slider)
// {
// // 可以添加滑动条的响应逻辑
// Debug.Log("Slider detected!");
// }
// // 继续根据其他类型处理
// }
}
}
public GObject GetInteractiveObjectAtScreenPosition(Vector2 screenPosition)
{
// 转换屏幕坐标为FGUI坐标
Vector2 localPos = GRoot.inst.GlobalToLocal(screenPosition);
Debug.LogError($"开始检查 坐标 {localPos}");
// 创建一个列表以存储潜在的可交互对象
List<GObject> interactiveObjects = new List<GObject>();
// 遍历GRoot下的所有子对象
FindInteractiveObjects(GRoot.inst, localPos, interactiveObjects);
// 遍历找到的可交互对象,判断是否被遮挡
foreach (GObject obj in interactiveObjects)
{
Debug.LogError($"可交互对象 {obj.name}");
// 判断是否被其他对象遮挡
if (!IsObjectObstructed(obj, localPos))
{
Debug.LogError($"结果是:{obj.name}");
return obj; // 返回第一个没有被遮挡的对象
}
}
return null; // 没有找到可交互的对象
}
private void FindInteractiveObjects(GComponent parent, Vector2 position, List<GObject> interactiveObjects)
{
Debug.LogError($"当前父节点 {parent.name}");
// 遍历当前GObject的所有子对象
foreach (GObject child in parent.GetChildren())
{
if (child.visible && child.touchable)
{
// 检查坐标是否在子对象的边界内
Rect rect = new Rect(child.x, child.y, child.width, child.height);
if (rect.Contains(position))
{
interactiveObjects.Add(child);
}
}
// 递归查找子组件
if (child is GComponent component)
{
FindInteractiveObjects(component, position, interactiveObjects);
}
}
}
private bool IsObjectObstructed(GObject target, Vector2 position)
{
// 遍历所有子对象,检查目标对象是否被遮挡
foreach (GObject gObject in GRoot.inst.GetChildren())
{
// 排除当前目标对象
if (gObject != target && gObject.visible && gObject.touchable)
{
Rect rect = new Rect(gObject.x, gObject.y, gObject.width, gObject.height);
if (rect.Contains(position))
{
return true; // 被遮挡
}
}
// 递归检查子组件
if (gObject is GComponent component)
{
foreach (GObject child in component.GetChildren())
{
Rect rect = new Rect(child.x, child.y, child.width, child.height);
if (rect.Contains(position))
{
return true; // 被遮挡
}
}
}
}
return false; // 没有被遮挡
}
}