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; /// /// 教学模式提示,箭头 /// public class IndicatorArrowComponent : MonoBehaviour { public RectTransform indicatorArrow; //箭头 public GameObject teachingModePrompt; //提示消息 [SerializeField] private GameObject _tempGame; private CancellationTokenSource cts; public void Init() { Debug.Log(MotionEngine.GetModule().GetProcessMode()); if (MotionEngine.GetModule().GetProcessMode() == ProcessMode.Teaching) indicatorArrow.gameObject.SetActive(true); teachingModePrompt.SetActive(true); MotionEngine.GetModule().OnTeachingPromptsObjects += TeachingPromptsObjects; if (MotionEngine.GetModule().GetProcessMode() == ProcessMode.Teaching || MotionEngine.GetModule().GetProcessMode() == ProcessMode.Training)//LY除了教学模式都弹出提示 { MotionEngine.GetModule().OnStepProcessDescriptionMessage += OnSendMessagePrompt; } } /// /// 消息 /// /// private void OnSendMessagePrompt(string message) { if (MotionEngine.GetModule().GetProcessMode() != ProcessMode.Assessment) { teachingModePrompt.transform.Find("Text").GetComponent().text = message; // 取消上一个等待任务并启动新的任务 cts?.Cancel(); cts = new CancellationTokenSource(); ClearMessageAfterDelayAsync(3f, teachingModePrompt.transform.Find("Text").GetComponent(), 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) { } } /// /// 箭头指向得物体 /// /// 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; } } }