feat(Gaze): 实现注视相关提示
This commit is contained in:
@@ -9,11 +9,44 @@ namespace Script.Gameplay.Player
|
||||
{
|
||||
public class PlayerConnectController : MonoBehaviour
|
||||
{
|
||||
public bool IsEnableConnecting = true; // 是否启用连接功能
|
||||
private bool isEnableConnecting = false;
|
||||
public bool IsEnableConnecting
|
||||
{
|
||||
get => isEnableConnecting;
|
||||
set
|
||||
{
|
||||
isEnableConnecting = value;
|
||||
if (isEnableConnecting == false)
|
||||
{
|
||||
// 重置当前目标
|
||||
CurrentTarget = null;
|
||||
previousGazedTarget = null;
|
||||
outTarget = null;
|
||||
inputTarget = null;
|
||||
}
|
||||
}
|
||||
} // 是否启用连接功能
|
||||
[SerializeField] private FirstPersonRaycaster raycaster; // 第一人称射线检测器
|
||||
[SerializeField] private float maxConnectDistance = 10f; // 最大连接距离
|
||||
|
||||
private IConnectable currentTarget; // 当前注视的可连接对象
|
||||
private IConnectable currentTarget;
|
||||
public IConnectable CurrentTarget
|
||||
{
|
||||
get => currentTarget;
|
||||
set
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
currentTarget = value;
|
||||
OnGazeEnter?.Invoke(currentTarget.GetGameObject());
|
||||
}
|
||||
else
|
||||
{
|
||||
OnGazeExit?.Invoke(null);
|
||||
}
|
||||
}
|
||||
} // 当前注视的可连接对象
|
||||
|
||||
private IConnectable previousGazedTarget; // 上一次注视的 IConnectable(用于触发进入/离开)
|
||||
private InputManager inputManager;
|
||||
|
||||
@@ -21,14 +54,16 @@ namespace Script.Gameplay.Player
|
||||
private IConnectable inputTarget;
|
||||
public event Action<IConnectable> OnSetPointA;
|
||||
public event Action<IConnectable> OnSetPointB;
|
||||
public event Action<GameObject> OnGazeEnter;
|
||||
public event Action<GameObject> OnGazeExit;
|
||||
|
||||
void Start()
|
||||
{
|
||||
inputManager = InputManager.Instance;
|
||||
inputManager.Input.Player.SetOutput.performed += ctx => SetPointA(currentTarget);
|
||||
inputManager.Input.Player.SetInput.performed += ctx => SetPointB(currentTarget);
|
||||
inputManager.Input.Player.SetOutput.performed += ctx => SetPointA(CurrentTarget);
|
||||
inputManager.Input.Player.SetInput.performed += ctx => SetPointB(CurrentTarget);
|
||||
inputManager.Input.Player.Connect.performed += ctx => CreateConnection();
|
||||
inputManager.Input.Player.CutLine.performed += ctx => CutConnectLine(currentTarget);
|
||||
inputManager.Input.Player.CutLine.performed += ctx => CutConnectLine(CurrentTarget);
|
||||
|
||||
if (raycaster == null)
|
||||
raycaster = GetComponent<FirstPersonRaycaster>() ?? GetComponentInChildren<FirstPersonRaycaster>();
|
||||
@@ -48,6 +83,7 @@ namespace Script.Gameplay.Player
|
||||
void DetectConnectable()
|
||||
{
|
||||
if (raycaster == null) return;
|
||||
if(!IsEnableConnecting) return;
|
||||
|
||||
GameObject lookAtObj = raycaster.CurrentLookAtObject;
|
||||
IConnectable hitConnectable = lookAtObj != null ? lookAtObj.GetComponent<IConnectable>() : null;
|
||||
@@ -68,13 +104,13 @@ namespace Script.Gameplay.Player
|
||||
previousGazedTarget = hitConnectable;
|
||||
}
|
||||
|
||||
currentTarget = hitConnectable;
|
||||
CurrentTarget = hitConnectable;
|
||||
}
|
||||
|
||||
|
||||
public IConnectable GetCurrentTarget()
|
||||
{
|
||||
return currentTarget;
|
||||
return CurrentTarget;
|
||||
}
|
||||
|
||||
public void SetPointA(IConnectable target)
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using UnityEngine;
|
||||
using Script.Gameplay.Interface;
|
||||
using System;
|
||||
using Core;
|
||||
using Script.Gameplay.Input;
|
||||
|
||||
namespace Script.Gameplay.Player
|
||||
@@ -10,8 +11,29 @@ namespace Script.Gameplay.Player
|
||||
[SerializeField] private FirstPersonRaycaster raycaster; // 新增:第一人称射线检测器
|
||||
[SerializeField] private bool isEnablePlayerInteraction = true; // 是否启用玩家交互功能
|
||||
|
||||
private IInteractable currentTarget; // 被射线命中的当前可交互对象(用于按键交互)
|
||||
private IInteractable currentTarget;
|
||||
private IInteractable CurrentTarget
|
||||
{
|
||||
get => currentTarget;
|
||||
set
|
||||
{
|
||||
currentTarget = value;
|
||||
if (currentTarget != null)
|
||||
{
|
||||
// 可以在这里添加一些逻辑,比如显示交互提示UI
|
||||
OnGazeEnter?.Invoke(this.gameObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 可以在这里隐藏交互提示UI
|
||||
OnGazeExit?.Invoke(this.gameObject);
|
||||
}
|
||||
}
|
||||
} // 被射线命中的当前可交互对象(用于按键交互)
|
||||
private IInteractable previousGazedTarget; // 上一次注视的对象(用于注视进入/离开事件)
|
||||
|
||||
public event Action<GameObject> OnGazeEnter;
|
||||
public event Action<GameObject> OnGazeExit;
|
||||
|
||||
void Start()
|
||||
{
|
||||
@@ -25,11 +47,13 @@ namespace Script.Gameplay.Player
|
||||
var input = InputManager.Instance.Input;
|
||||
input.Player.Interact.performed += ctx =>
|
||||
{
|
||||
if (currentTarget != null)
|
||||
if (CurrentTarget != null)
|
||||
{
|
||||
currentTarget.Interact(this.gameObject);
|
||||
CurrentTarget.Interact(this.gameObject);
|
||||
}
|
||||
};
|
||||
|
||||
ControllerLocator.Instance.Register(this);
|
||||
}
|
||||
|
||||
void Update()
|
||||
@@ -63,7 +87,7 @@ namespace Script.Gameplay.Player
|
||||
previousGazedTarget = hitInteractable;
|
||||
}
|
||||
|
||||
currentTarget = hitInteractable;
|
||||
CurrentTarget = hitInteractable;
|
||||
}
|
||||
|
||||
public void SetPlayerInteractionEnabled(bool isEnabled)
|
||||
@@ -73,7 +97,7 @@ namespace Script.Gameplay.Player
|
||||
{
|
||||
previousGazedTarget.OnGazeExit(this.gameObject);
|
||||
previousGazedTarget = null;
|
||||
currentTarget = null;
|
||||
CurrentTarget = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -56,6 +56,7 @@ namespace Script.Gameplay.Player
|
||||
;
|
||||
|
||||
ControllerLocator.Instance.Register(this);
|
||||
OnEnterWatchMode?.Invoke(currentMode);
|
||||
}
|
||||
|
||||
// 实现按Tap键实现在WatchMode 3个模式切换,并通过SwitchWatchMode设置正确的相机模式
|
||||
|
58
Assets/Script/Gameplay/UI/PlayerGazeReminder.cs
Normal file
58
Assets/Script/Gameplay/UI/PlayerGazeReminder.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using Core;
|
||||
using UnityEngine;
|
||||
using Script.Gameplay.Player;
|
||||
using TMPro;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class PlayerGazeReminder : UIBase
|
||||
{
|
||||
[SerializeField] private TMP_Text reminderFirstText;
|
||||
[SerializeField] private TMP_Text reminderSecondText;
|
||||
private PlayerInteractorController playerInteractorController;
|
||||
private PlayerConnectController playerConnectController;
|
||||
|
||||
protected override void Awake()
|
||||
{
|
||||
base.Awake();
|
||||
reminderFirstText.text = "";
|
||||
reminderSecondText.text = "";
|
||||
ControllerLocator.Instance.TryGetWait<PlayerInteractorController>(OnGetInteractorController);
|
||||
ControllerLocator.Instance.TryGetWait<PlayerConnectController>(OnGetConnectController);
|
||||
}
|
||||
|
||||
private void OnGetInteractorController(PlayerInteractorController controller)
|
||||
{
|
||||
playerInteractorController = controller;
|
||||
playerInteractorController.OnGazeEnter += HandleInteractGazeEnter;
|
||||
playerInteractorController.OnGazeExit += HandleInteractGazeExit;
|
||||
}
|
||||
|
||||
private void HandleInteractGazeEnter(GameObject obj)
|
||||
{
|
||||
reminderFirstText.text = "Press [F] to interact";
|
||||
}
|
||||
|
||||
private void HandleInteractGazeExit(GameObject obj)
|
||||
{
|
||||
reminderFirstText.text = "";
|
||||
}
|
||||
|
||||
private void OnGetConnectController(PlayerConnectController controller)
|
||||
{
|
||||
playerConnectController = controller;
|
||||
playerConnectController.OnGazeEnter += HandleConnectGazeEnter;
|
||||
playerConnectController.OnGazeExit += HandleConnectGazeExit;
|
||||
}
|
||||
|
||||
private void HandleConnectGazeEnter(GameObject obj)
|
||||
{
|
||||
reminderFirstText.text = "Connectable";
|
||||
}
|
||||
|
||||
private void HandleConnectGazeExit(GameObject obj)
|
||||
{
|
||||
reminderFirstText.text = "";
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Script/Gameplay/UI/PlayerGazeReminder.cs.meta
Normal file
3
Assets/Script/Gameplay/UI/PlayerGazeReminder.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c8ca1a3e5bb84ffdb0f2e03f50e4a077
|
||||
timeCreated: 1761008641
|
Reference in New Issue
Block a user