ChangDaoZhanGuan/ChangDaoProject/Assets/Scr/Cooler_tip.cs

56 lines
1.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Cooler_tip : MonoBehaviour
{
public Image uiImage; // 引用UI图片
public Image maskImage;
private RectTransform imageRectTransform; // 图片的RectTransform
public string targetTag = "cooler";
private void Start()
{
// 获取图片的RectTransform组件
imageRectTransform = uiImage.GetComponent<RectTransform>();
// 初始时隐藏图片
uiImage.gameObject.SetActive(false);
maskImage.gameObject.SetActive(false);
}
private void Update()
{
// 检测鼠标点击或触摸屏点击
if (Input.GetMouseButtonDown(0))
{
// 射线检测,判断是否点击在模型上
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.CompareTag(targetTag))
{
// 如果点击到具有特定标签的模型,执行你的操作
Debug.Log("Clicked on object with tag: " + targetTag);
// 点击到模型
ShowImage(hit.point);
}
}
}
}
private void ShowImage(Vector3 position)
{
// 显示图片并设置位置
uiImage.gameObject.SetActive(true);
maskImage.gameObject.SetActive(true);
//imageRectTransform.position = Camera.main.WorldToScreenPoint(position);
}
}