YanCheng_Metrology/Assets/Scripts/Tsq/ToolsCameraComponent.cs

125 lines
3.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEditor.UIElements;
using UnityEngine.UI;
namespace DefaultNamespace
{
public class ToolsCameraComponent : MonoBehaviour
{
private IRaycastable _raycastComponent;
private IUIFollow _uiFollow;
public RectTransform ui;
private void CheckToolModel()
{
if (EventSystem.current.IsPointerOverGameObject())
{
return;
}
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 3))
{
//bool raycast = Physics.Raycast(ray, out hit);
GameObject hitObject = hit.collider.gameObject;
//if (raycast)
//{
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
{
ui.gameObject.SetActive(false);
}
}
void Update()
{
// 调用射线功能
CheckToolModel();
}
}
}
//{
// [SerializeField]
// private Camera mainCamera; // 主摄像机
// [SerializeField]
// private GameObject uiPanel; // UI Panel负责显示物品信息
// [SerializeField]
// private TMP_Text itemNameText;
// private GameObject lastHoveredObject; // 记录上一次悬停的物体
// void Start()
// {
// uiPanel.SetActive(false);
// }
// void Update()
// {
// Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
// RaycastHit hit;
// bool raycast = Physics.Raycast(ray, out hit);
// if (raycast)
// {
// GameObject hitObject = hit.collider.gameObject;
// if (hitObject.CompareTag("Tools"))
// {
// if (hitObject != lastHoveredObject)
// {
// Debug.Log(hitObject.name);
// HandleHoverEnter(hitObject);
// lastHoveredObject = hitObject;
// }
// uiPanel.transform.position = Input.mousePosition;
// }
// }
// else
// {
// if (lastHoveredObject!=null)
// {
// HandleHoverExit();
// }
// }
// }
// void HandleHoverEnter(GameObject hoverObject)
// {
// itemNameText.text = hoverObject.name;
// uiPanel.SetActive(true);
// }
// void HandleHoverExit()
// {
// uiPanel.SetActive(false);
// lastHoveredObject = null;
// }
//}
//}