ar_tourism_flutter_unity/unity/VRProject2/Assets/zhl/zhlScripts/upSpeed.cs
2025-05-14 17:04:13 +08:00

214 lines
6.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System;
public class upSpeed : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public GameObject header;
public ScreenRecording screenRecording;
public CaptureScreenshotMgr captureScreenshotMgr;
public float pressStartTime;
private const float shortPressDuration = 0.5f; // 定义短按的时间阈值
public bool isLongPressTriggered = false; // 标记长按是否已触发
public bool isPress = false;
public float pressDuration;
public string duanANdChang;
public bool isZhiXing;
// Start is called before the first frame update
private void Awake()
{
try
{
// RuntimeLog.Instance?.AddLog("\n=== 初始化 upSpeed ===");
// 获取按钮组件
this.gameObject.GetComponent<Button>();
// 初始化 header
if (header == null)
{
header = GameObject.Find("Header");
// RuntimeLog.Instance?.AddLog(header != null
// ? "找到 Header 对象"
// : "警告: 未找到 Header 对象");
}
// 初始化管理器引用
if (captureScreenshotMgr == null)
{
captureScreenshotMgr = FindObjectOfType<CaptureScreenshotMgr>();
if (captureScreenshotMgr != null)
{
// 传递 header 引用给 CaptureScreenshotMgr
captureScreenshotMgr.header = header;
// RuntimeLog.Instance?.AddLog("已找到并设置 CaptureScreenshotMgr");
}
else
{
// RuntimeLog.Instance?.AddLog("警告: 未找到 CaptureScreenshotMgr 组件");
}
}
if (screenRecording == null)
{
screenRecording = FindObjectOfType<ScreenRecording>();
if (screenRecording != null)
{
// RuntimeLog.Instance?.AddLog("已找到 ScreenRecording");
}
else
{
// RuntimeLog.Instance?.AddLog("警告: 未找到 ScreenRecording 组件");
}
}
}
catch (Exception ex)
{
// RuntimeLog.Instance?.AddLog($"upSpeed Awake 错误: {ex.Message}");
Debug.LogError($"upSpeed Awake 错误: {ex.Message}");
}
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("按下");
isZhiXing=false;
pressStartTime = Time.time; // 记录按下的时间
isLongPressTriggered = false; // 重置长按触发标记
isPress = true;
//StartCoroutine(CheckLongPress());
}
public void Update()
{
if (isPress)
{
float times = Time.time - pressStartTime;
if (times < shortPressDuration)
{
duanANdChang="短按";
//header.SetActive(false);
// captureScreenshotMgr.CaptureScreenshot("",()=>
// {
// header.SetActive(true);
// });
}
else
{
duanANdChang="长按";
if(!isZhiXing)
{
HandleLongPress();
isZhiXing=true;
}
}
}
}
public void OnPointerUp(PointerEventData eventData)
{
isPress = false;
// 抬起时的逻辑
if (duanANdChang=="短按")
{
HandleShortPressUp();
}
else if(duanANdChang=="长按")
{
HandleLongPressUp();
}
}
private void HandleShortPressUp()
{
try
{
// RuntimeLog.Instance?.AddLog("\n=== 处理短按抬起事件 ===");
if (captureScreenshotMgr == null)
{
// RuntimeLog.Instance?.AddLog("错误: captureScreenshotMgr 为空");
return;
}
if (header == null)
{
// RuntimeLog.Instance?.AddLog("警告: header 对象为空,尝试重新查找");
header = GameObject.Find("Header");
if (header != null)
{
captureScreenshotMgr.header = header;
}
}
if (header != null)
{
header.SetActive(false);
// RuntimeLog.Instance?.AddLog("Header 已隐藏");
}
captureScreenshotMgr.CaptureScreenshot();
// RuntimeLog.Instance?.AddLog("截图请求已发送");
}
catch (Exception ex)
{
// RuntimeLog.Instance?.AddLog($"HandleShortPressUp 错误: {ex.Message}");
Debug.LogError($"HandleShortPressUp 错误: {ex.Message}");
}
}
private void HandleLongPressUp()
{
try
{
Debug.Log("长按抬起时的逻辑");
if (screenRecording != null)
{
screenRecording.StopScreen();
}
else
{
Debug.LogError("screenRecording 为空");
}
}
catch (Exception ex)
{
Debug.LogError($"HandleLongPressUp 错误: {ex.Message}");
}
}
private void HandleShortPress()
{
// 短按的操作逻辑
Debug.Log("短按的操作逻辑");
// 例如:截图
captureScreenshotMgr.CaptureScreenshot();
}
private void HandleLongPress()
{
try
{
Debug.Log("长按的操作逻辑");
if (screenRecording != null)
{
screenRecording.StartScreen();
}
else
{
Debug.LogError("screenRecording 为空");
}
}
catch (Exception ex)
{
Debug.LogError($"HandleLongPress 错误: {ex.Message}");
}
}
}