189 lines
6.1 KiB
C#
189 lines
6.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class OnclickRemains : MonoBehaviour, IPointerDownHandler
|
|
{
|
|
private Vector3 offset;
|
|
private float Zpos = -9f; // 控制物体的 z 轴位置
|
|
private delegate void Style();
|
|
private Style style;
|
|
//public float rayDistance = 2f; // 射线的最大距离
|
|
public Vector3 boxSize = new Vector3(1f, 1f, 1f); // 盒子的尺寸
|
|
public float detectionDistance = 2f; // 盒子检测的距离
|
|
|
|
private static bool OpenRepeat = false;//打开重复点击
|
|
private static WaitForSeconds t = new WaitForSeconds(1);
|
|
private static List<OnclickRemains> Remains;
|
|
public List<float> distances;
|
|
private int CurrentScreen;//当前所在场景index
|
|
|
|
private Coroutine coroutine;
|
|
private void OnEnable()
|
|
{
|
|
if (Remains == null)
|
|
{
|
|
Remains = new List<OnclickRemains>();
|
|
}
|
|
Remains.Add(this);
|
|
style = Move;
|
|
StartOpen(true);
|
|
|
|
if (SceneTransitionManager.Instance.CurrentTrackingPointIndex < 3)
|
|
{
|
|
Zpos = distances[SceneTransitionManager.Instance.CurrentTrackingPointIndex];
|
|
CurrentScreen = SceneTransitionManager.Instance.CurrentTrackingPointIndex;
|
|
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
|
|
switch (CurrentScreen)
|
|
{
|
|
case 0:
|
|
spriteRenderer.sortingLayerName = "Slime";
|
|
spriteRenderer.sortingOrder = 1200;
|
|
break;
|
|
case 1:
|
|
spriteRenderer.sortingLayerName = "伐木场";
|
|
spriteRenderer.sortingOrder = 30;
|
|
break;
|
|
case 2:
|
|
spriteRenderer.sortingLayerName = "日神";
|
|
spriteRenderer.sortingOrder = 5;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
private void OnDestroy()
|
|
{
|
|
if (coroutine != null)
|
|
{
|
|
StopCoroutine(coroutine);
|
|
}
|
|
Remains.Remove(this);
|
|
}
|
|
private void Update()
|
|
{
|
|
|
|
style?.Invoke();
|
|
}
|
|
|
|
|
|
public static void OpenClickRepeat()
|
|
{
|
|
OpenRepeat = true;
|
|
if (Remains != null)
|
|
{
|
|
for (int i = 0; i < Remains.Count; i++)
|
|
{
|
|
Remains[i].StartOpen(true);
|
|
}
|
|
}
|
|
}
|
|
public void StartOpen(bool open)
|
|
{
|
|
if (open)
|
|
{
|
|
if (coroutine == null && OpenRepeat)
|
|
{
|
|
Debug.LogWarning("开启了");
|
|
coroutine = StartCoroutine(RepeaetClick());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (coroutine != null)
|
|
{
|
|
StopCoroutine(coroutine);
|
|
coroutine = null;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
IEnumerator RepeaetClick()
|
|
{
|
|
while (true)
|
|
{
|
|
ClickEvent();
|
|
yield return t;
|
|
}
|
|
}
|
|
|
|
private void Move()
|
|
{
|
|
// 获取当前鼠标的位置并转换为世界坐标,保持物体的 Zpos
|
|
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(transform.position).z));
|
|
// 更新物体位置为鼠标位置加上偏移量,确保 Z 轴不变
|
|
transform.position = new Vector3(mousePosition.x + offset.x, mousePosition.y + offset.y, Zpos);
|
|
Debug.LogWarning("---" + transform.position);
|
|
if (Input.GetMouseButtonUp(0) || SceneTransitionManager.Instance.CurrentTrackingPointIndex != CurrentScreen)
|
|
{
|
|
StartOpen(true);
|
|
style = OnclickEvent;
|
|
}
|
|
}
|
|
private void OnclickEvent()
|
|
{
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
Debug.LogWarning("鼠标左键点击");
|
|
ClickEvent();
|
|
}
|
|
|
|
}
|
|
private void ClickEvent()
|
|
{
|
|
// 盒子的中心点位于物体自身的位置,并在物体的前方一定距离处
|
|
Vector3 boxCenter = transform.position + transform.forward * detectionDistance;
|
|
|
|
// 绘制可视化线条用于调试(代替 Gizmos 绘制)
|
|
Debug.DrawLine(transform.position, boxCenter, Color.green, 2.0f); // 画一条线指向检测位置
|
|
|
|
// 获取在盒子范围内的所有碰撞体
|
|
Collider[] hitColliders = Physics.OverlapBox(boxCenter, boxSize / 2, transform.rotation);
|
|
|
|
// 遍历所有碰撞体
|
|
foreach (Collider hitCollider in hitColliders)
|
|
{
|
|
Debug.Log("Hit object: " + hitCollider.gameObject.name);
|
|
GameObject obj = hitCollider.gameObject;
|
|
|
|
// 检查物体是否实现了 IPointerDownHandler 接口
|
|
if (obj.name != name && obj.TryGetComponent(out IPointerDownHandler pointerDown))
|
|
{
|
|
// 创建 PointerEventData 并传递点击事件
|
|
PointerEventData pointerEventData = new PointerEventData(EventSystem.current)
|
|
{
|
|
position = Vector2.one // 可根据需要设置点击位置
|
|
};
|
|
|
|
Debug.LogWarning("开启了点击事件");
|
|
pointerDown.OnPointerDown(pointerEventData);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// 绘制盒子用于调试
|
|
private void OnDrawGizmos()
|
|
{
|
|
// 在编辑器中显示盒子的检测区域
|
|
Vector3 boxCenter = transform.position + transform.forward * detectionDistance;
|
|
Gizmos.color = Color.red;
|
|
Gizmos.DrawWireCube(boxCenter, boxSize);
|
|
}
|
|
// 当鼠标点击时触发
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
StartOpen(false);
|
|
style = Move;
|
|
// 获取鼠标点击位置并将其转换为世界坐标,保持物体的 Zpos
|
|
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(transform.position).z));
|
|
|
|
// 计算物体位置与鼠标位置的偏移量
|
|
offset = transform.position - mousePosition;
|
|
}
|
|
}
|