fix(tutorial): complete click targets from UI events

This commit is contained in:
2026-08-30 21:25:32 +08:00
parent 846d9c8a69
commit 11bb7415ee
4 changed files with 76 additions and 2 deletions
+4
View File
@@ -1,5 +1,9 @@
# Changelog
## 0.1.4
- `ClickTarget` 对 UI Button 和显式 `ShrinkTutorialAnchor` 改为点击事件驱动,不再依赖轮询鼠标按下、抬起的帧时序;非 UI 目标保留原有指针检测。
## 0.1.3
- 修复 `ClickTarget` 在目标按钮点击后立即打开、关闭或替换 UI 时误判未命中的问题;现在按下时确认原目标,松开时按按下时的目标区域完成步骤。
+62
View File
@@ -43,6 +43,8 @@ namespace ShrinkTutorial
private ShrinkTutorialMaskShape _clickTargetArmedShape;
private int _clickTargetArmedFrame;
private bool _clickTargetArmedWithTouch;
private Button _clickTargetButton;
private ShrinkTutorialAnchor _clickTargetAnchor;
private float _stepAutoTimer;
private string _receivedCustomEvent = string.Empty;
@@ -214,6 +216,7 @@ namespace ShrinkTutorial
_stepCompleted = false;
_stepCompletionQueued = false;
_clickTargetArmed = false;
UnbindClickTargetEvents();
_stepAutoTimer = tutorial.steps[stepIndex].autoDelay;
_receivedCustomEvent = string.Empty;
@@ -237,6 +240,8 @@ namespace ShrinkTutorial
if (targetTransform)
LogDebug($"Resolved target for step {stepIndex}: {GetTransformPath(targetTransform)}");
BindClickTargetEvents(step, targetTransform);
EmitSignal(step.enterSignal);
_dialog.SetSkipVisible(ResolveSettings().showSkipButton);
@@ -249,6 +254,7 @@ namespace ShrinkTutorial
yield return null;
}
UnbindClickTargetEvents();
EmitSignal(step.exitSignal);
_dialog.Hide();
@@ -364,6 +370,9 @@ namespace ShrinkTutorial
private void TickClickTargetCompletion(ShrinkTutorialStep step, Transform targetTransform)
{
if (_clickTargetButton || _clickTargetAnchor)
return;
if (!_clickTargetArmed &&
targetTransform &&
ShrinkTutorialTargetUtility.TryBuildScreenRect(targetTransform, out var targetRect) &&
@@ -427,6 +436,58 @@ namespace ShrinkTutorial
QueueStepCompletion();
}
private void BindClickTargetEvents(ShrinkTutorialStep step, Transform targetTransform)
{
if (step.completeCondition != ShrinkTutorialCompleteCondition.ClickTarget || !targetTransform)
return;
_clickTargetButton = targetTransform.GetComponent<Button>() ??
targetTransform.GetComponentInParent<Button>() ??
targetTransform.GetComponentInChildren<Button>(true);
if (_clickTargetButton)
{
_clickTargetButton.onClick.AddListener(HandleClickTargetEvent);
LogDebug($"Bound ClickTarget button event: {GetTransformPath(_clickTargetButton.transform)}");
return;
}
_clickTargetAnchor = targetTransform.GetComponent<ShrinkTutorialAnchor>() ??
targetTransform.GetComponentInParent<ShrinkTutorialAnchor>() ??
targetTransform.GetComponentInChildren<ShrinkTutorialAnchor>(true);
if (_clickTargetAnchor)
{
_clickTargetAnchor.Clicked += HandleClickTargetPointerEvent;
LogDebug($"Bound ClickTarget anchor event: {GetTransformPath(_clickTargetAnchor.transform)}");
}
}
private void HandleClickTargetEvent()
{
LogDebug($"ClickTarget button event received, tutorial={_currentTutorial?.tutorialId}, step={_currentStepIndex}");
QueueStepCompletion();
}
private void HandleClickTargetPointerEvent(PointerEventData eventData)
{
if (eventData != null && eventData.button != PointerEventData.InputButton.Left)
return;
LogDebug($"ClickTarget pointer event received, tutorial={_currentTutorial?.tutorialId}, step={_currentStepIndex}");
QueueStepCompletion();
}
private void UnbindClickTargetEvents()
{
if (_clickTargetButton)
_clickTargetButton.onClick.RemoveListener(HandleClickTargetEvent);
if (_clickTargetAnchor)
_clickTargetAnchor.Clicked -= HandleClickTargetPointerEvent;
_clickTargetButton = null;
_clickTargetAnchor = null;
}
private static bool IsPointInsideTarget(Rect targetRect, ShrinkTutorialMaskShape maskShape, Vector2 screenPosition)
{
return maskShape == ShrinkTutorialMaskShape.Circle
@@ -577,6 +638,7 @@ namespace ShrinkTutorial
_runner = null;
_stepCompletionQueued = false;
_clickTargetArmed = false;
UnbindClickTargetEvents();
TryStartQueuedTutorial();
}
+9 -1
View File
@@ -1,12 +1,20 @@
using System;
using UnityEngine;
using UnityEngine.EventSystems;
namespace ShrinkTutorial
{
public class ShrinkTutorialAnchor : MonoBehaviour
public class ShrinkTutorialAnchor : MonoBehaviour, IPointerClickHandler
{
[SerializeField] private string anchorId = string.Empty;
public string AnchorId => anchorId;
public event Action<PointerEventData> Clicked;
public void OnPointerClick(PointerEventData eventData)
{
Clicked?.Invoke(eventData);
}
public void SetAnchorId(string value)
{
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "com.cneicy.shrink-tutorial",
"version": "0.1.3",
"version": "0.1.4",
"displayName": "ShrinkTutorial",
"description": "面向 Unity 的互动式新手引导系统,提供数据驱动教程、遮罩挖洞、动态锚点、步骤条件与运行时调度。",
"unity": "2022.3",