1
This commit is contained in:
47
Src/Unity/Scripts/Physics/PhysicsUtils.cs
Normal file
47
Src/Unity/Scripts/Physics/PhysicsUtils.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.Physics
|
||||
{
|
||||
public static class PhysicsUtils
|
||||
{
|
||||
public static void FixCollidersBound(params Transform[] transforms)
|
||||
{
|
||||
foreach (var transform in transforms)
|
||||
{
|
||||
// 获取或创建 BoxCollider 组件
|
||||
var boxCollider = transform.GetComponent<BoxCollider>();
|
||||
if (boxCollider == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 获取所有可见的 MeshRenderer 组件
|
||||
var meshRenderers = transform.GetComponentsInChildren<MeshRenderer>();
|
||||
|
||||
if (meshRenderers.Length == 0)
|
||||
{
|
||||
Debug.LogWarning("No MeshRenderer components found in children.");
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化包围盒,以第一个 MeshRenderer 的包围盒为基准
|
||||
Bounds bounds = new Bounds(transform.transform.InverseTransformPoint(meshRenderers[0].bounds.center),
|
||||
transform.transform.InverseTransformVector(meshRenderers[0].bounds.size));
|
||||
|
||||
// 遍历所有 MeshRenderer,合并包围盒
|
||||
for (int i = 1; i < meshRenderers.Length; i++)
|
||||
{
|
||||
Bounds localBounds = meshRenderers[i].bounds;
|
||||
Vector3 localCenter = transform.transform.InverseTransformPoint(localBounds.center);
|
||||
Vector3 localSize = transform.transform.InverseTransformVector(localBounds.size);
|
||||
|
||||
bounds.Encapsulate(new Bounds(localCenter, localSize));
|
||||
}
|
||||
|
||||
// 设置 BoxCollider 的中心和大小
|
||||
boxCollider.center = bounds.center;
|
||||
boxCollider.size = bounds.size;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Src/Unity/Scripts/Physics/PhysicsUtils.cs.meta
Normal file
3
Src/Unity/Scripts/Physics/PhysicsUtils.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 796081504c0e47ce93358ef36ba3adb9
|
||||
timeCreated: 1735276998
|
@@ -10,6 +10,8 @@ namespace BITKit.UX
|
||||
{
|
||||
public class UXToolTips:IDisposable
|
||||
{
|
||||
public static VisualElement Hovering;
|
||||
|
||||
private readonly IUXService _uxService;
|
||||
private readonly IMainTicker _ticker;
|
||||
|
||||
@@ -40,7 +42,12 @@ namespace BITKit.UX
|
||||
|
||||
|
||||
}
|
||||
if(_label is null || _rootVisualElement is null)return;
|
||||
|
||||
if (_label is null || _rootVisualElement is null)
|
||||
{
|
||||
Hovering = null;
|
||||
return;
|
||||
}
|
||||
var tooltip = CurrentToolTip(_rootVisualElement.panel);
|
||||
if (tooltip != "")
|
||||
{
|
||||
@@ -71,12 +78,18 @@ namespace BITKit.UX
|
||||
{
|
||||
// https://docs.unity3d.com/2022.2/Documentation/Manual/UIE-faq-event-and-input-system.html
|
||||
|
||||
if (!EventSystem.current.IsPointerOverGameObject()) return "";
|
||||
if (!EventSystem.current.IsPointerOverGameObject())
|
||||
{
|
||||
Hovering = null;
|
||||
return "";
|
||||
}
|
||||
|
||||
var screenPosition = Mouse.current.position.ReadValue();
|
||||
screenPosition.y = Screen.height - screenPosition.y;
|
||||
|
||||
VisualElement ve = panel.Pick(RuntimePanelUtils.ScreenToPanel(panel, screenPosition));
|
||||
|
||||
Hovering = ve;
|
||||
|
||||
return ve == null ? "" : ve.tooltip;
|
||||
}
|
||||
|
Reference in New Issue
Block a user