ShanxiKnowledgeBase/SXElectricityInformationAcq.../Assets/Scripts/IndicatorArrowComponent.cs

92 lines
3.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using DefaultNamespace;
using DefaultNamespace.ProcessMode;
using MotionFramework;
using TMPro;
using UnityEngine;
/// <summary>
/// 教学模式提示,箭头
/// </summary>
public class IndicatorArrowComponent : MonoBehaviour
{
public RectTransform indicatorArrow; //箭头
public GameObject teachingModePrompt; //提示消息
[SerializeField] private GameObject _tempGame;
private CancellationTokenSource cts;
public void Init()
{
Debug.Log(MotionEngine.GetModule<DataConfigManager>().GetProcessMode());
if (MotionEngine.GetModule<DataConfigManager>().GetProcessMode() == ProcessMode.Teaching)
indicatorArrow.gameObject.SetActive(true);
teachingModePrompt.SetActive(true);
MotionEngine.GetModule<AnimationProcessManager>().OnTeachingPromptsObjects += TeachingPromptsObjects;
MotionEngine.GetModule<AnimationProcessManager>().OnStepProcessDescriptionMessage += OnSendMessagePrompt;
}
/// <summary>
/// 消息
/// </summary>
/// <param name="message"></param>
private void OnSendMessagePrompt(string message)
{
teachingModePrompt.transform.Find("Text").GetComponent<TMP_Text>().text = message;
// 取消上一个等待任务并启动新的任务
cts?.Cancel();
cts = new CancellationTokenSource();
ClearMessageAfterDelayAsync(3f, teachingModePrompt.transform.Find("Text").GetComponent<TMP_Text>(), cts.Token).Forget();
}
private async UniTaskVoid ClearMessageAfterDelayAsync(float delay, TMP_Text textComponent, CancellationToken token)
{
try
{
await UniTask.Delay((int)(delay * 1000), cancellationToken: token);
textComponent.text = string.Empty;
}
catch (OperationCanceledException)
{
// 任务被取消时的处理(可选)
}
}
/// <summary>
/// 箭头指向得物体
/// </summary>
/// <param name="gameobj"></param>
private void TeachingPromptsObjects(GameObject gameobj)
{
_tempGame = gameobj;
}
void Update()
{
if (_tempGame != null)
{
Vector3 screenPos = Camera.main.WorldToScreenPoint(_tempGame.transform.position);
if (screenPos.z > 0 && screenPos.x > 0 && screenPos.x < Screen.width && screenPos.y > 0 && screenPos.y < Screen.height)
{
indicatorArrow.position = screenPos;
}
else
{
Vector3 offScreenPos = indicatorArrow.position;
offScreenPos.y = 9999;
indicatorArrow.position = offScreenPos;
}
}
else
{
Vector3 offScreenPos = indicatorArrow.position;
offScreenPos.y = 9999;
indicatorArrow.position = offScreenPos;
}
}
}