显示名称

This commit is contained in:
taosuqi 2024-08-12 10:03:41 +08:00
parent 311226f057
commit 164b99913f
1 changed files with 29 additions and 27 deletions

View File

@ -1,8 +1,9 @@
using Framework.Scripts.Runtime.Engine.Engine.Camera.UIMoveTarget;
using Framework.Scripts.Runtime.Engine.Engine.Camera;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEditor.UIElements;
using UnityEngine.UI;
namespace DefaultNamespace
{
@ -15,34 +16,32 @@ namespace DefaultNamespace
public RectTransform ui;
private void Awake()
private void CheckToolModel()
{
_raycastComponent = new CameraRaycastComponent();
_uiFollow = new CameraFollowComponent();
}
void OnEnable()
{
_raycastComponent.OnRaycastHit += HandleRaycastHit;
}
// 处理射线命中事件并打印物体名称
void HandleRaycastHit(RaycastHit hit)
{
if (!EventSystem.current.IsPointerOverGameObject())
if (EventSystem.current.IsPointerOverGameObject())
{
if (hit.collider.CompareTag("Tools"))
return;
}
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 2))
{
bool raycast = Physics.Raycast(ray, out hit);
GameObject hitObject = hit.collider.gameObject;
//ToolBase tb = hit.transform.GetComponent<ToolBase>();
if (raycast)
{
Debug.Log("Raycast hit: " + hit.collider.name);
_uiFollow.FollowObject(hit.collider.transform, ui, Camera.main);
ui.gameObject.SetActive(true);
ui.transform.Find("Text").GetComponent<TMP_Text>().text = hit.transform.name;
}
else
{
ui.gameObject.SetActive(false);
if (hitObject.CompareTag("Tools"))
{
ui.gameObject.SetActive(true);
ui.GetComponentInChildren<TextMeshProUGUI>().text = hit.collider.gameObject.name;
ui.transform.position = Input.mousePosition + new Vector3(10, 10, 0);
}
else
{
ui.gameObject.SetActive(false);
}
}
}
else
@ -52,10 +51,13 @@ namespace DefaultNamespace
}
void Update()
{
// 调用射线功能
_raycastComponent?.Raycast();
CheckToolModel();
}
}
}