From c4edb9b06aa3087e382c813c730519f718830e70 Mon Sep 17 00:00:00 2001 From: Kochoyume <78918084+kochounoyume@users.noreply.github.com> Date: Wed, 3 Jul 2024 00:36:49 +0900 Subject: [PATCH] Remove EventSystem field and fix constant string --- .../Runtime/AnimHashConstants.cs | 48 +++++++++---------- .../Runtime/MobilePlayerController.cs | 12 ++--- .../PlayerControl/Runtime/PlayerController.cs | 34 ++++++------- 3 files changed, 45 insertions(+), 49 deletions(-) diff --git a/Packages/PlayerControl/Runtime/AnimHashConstants.cs b/Packages/PlayerControl/Runtime/AnimHashConstants.cs index 6036a0d..5c0aa05 100644 --- a/Packages/PlayerControl/Runtime/AnimHashConstants.cs +++ b/Packages/PlayerControl/Runtime/AnimHashConstants.cs @@ -11,32 +11,32 @@ public sealed class AnimHashConstants /// /// "Speed" animation hash. /// - public readonly int SpeedAnim; + public readonly int Speed; /// /// "IsGround" animation hash. /// - public readonly int GroundAnim; + public readonly int IsGround; /// /// "JumpStart" animation hash. /// - public readonly int JumpStartAnim; + public readonly int JumpStart; /// /// "DoubleJump" animation hash. /// - public readonly int DoubleJumpAnim; + public readonly int DoubleJump; /// /// "Forward" animation hash. /// - public readonly int ForwardAnim; + public readonly int Forward; /// /// "SideStep" animation hash. /// - public readonly int SideStepAnim; + public readonly int SideStep; // Ensure that this class is not generated before Awake. public AnimHashConstants(in Object obj) @@ -45,41 +45,41 @@ public AnimHashConstants(in Object obj) { throw new System.ArgumentNullException(nameof(obj)); } - SpeedAnim = Animator.StringToHash("Speed"); - GroundAnim = Animator.StringToHash("IsGround"); - JumpStartAnim = Animator.StringToHash("JumpStart"); - DoubleJumpAnim = Animator.StringToHash("DoubleJump"); - ForwardAnim = Animator.StringToHash("Forward"); - SideStepAnim = Animator.StringToHash("SideStep"); + Speed = Animator.StringToHash(nameof(Speed)); + IsGround = Animator.StringToHash(nameof(IsGround)); + JumpStart = Animator.StringToHash(nameof(JumpStart)); + DoubleJump = Animator.StringToHash(nameof(DoubleJump)); + Forward = Animator.StringToHash(nameof(Forward)); + SideStep = Animator.StringToHash(nameof(SideStep)); } public override string ToString() { StringBuilder builder = new StringBuilder(nameof(AnimHashConstants)); builder.Append(" {"); - builder.Append(nameof(SpeedAnim)); + builder.Append(nameof(Speed)); builder.Append(" = "); - builder.Append(SpeedAnim); + builder.Append(Speed); builder.Append(", "); - builder.Append(nameof(GroundAnim)); + builder.Append(nameof(IsGround)); builder.Append(" = "); - builder.Append(GroundAnim); + builder.Append(IsGround); builder.Append(", "); - builder.Append(nameof(JumpStartAnim)); + builder.Append(nameof(JumpStart)); builder.Append(" = "); - builder.Append(JumpStartAnim); + builder.Append(JumpStart); builder.Append(", "); - builder.Append(nameof(DoubleJumpAnim)); + builder.Append(nameof(DoubleJump)); builder.Append(" = "); - builder.Append(DoubleJumpAnim); + builder.Append(DoubleJump); builder.Append(", "); - builder.Append(nameof(ForwardAnim)); + builder.Append(nameof(Forward)); builder.Append(" = "); - builder.Append(ForwardAnim); + builder.Append(Forward); builder.Append(", "); - builder.Append(nameof(SideStepAnim)); + builder.Append(nameof(SideStep)); builder.Append(" = "); - builder.Append(SideStepAnim); + builder.Append(SideStep); builder.Append(" }"); return builder.ToString(); } diff --git a/Packages/PlayerControl/Runtime/MobilePlayerController.cs b/Packages/PlayerControl/Runtime/MobilePlayerController.cs index 920613f..4490e6d 100644 --- a/Packages/PlayerControl/Runtime/MobilePlayerController.cs +++ b/Packages/PlayerControl/Runtime/MobilePlayerController.cs @@ -46,22 +46,22 @@ protected override void Start() uiView.Joystick.OnValueChanged += value => { const InputActionPhase phase = InputActionPhase.Performed; - base.OnActionTriggered(new CallbackContext(MoveAction, phase, value)); + base.OnActionTriggered(new CallbackContext(Move, phase, value)); }; uiView.SprintButton.OnStart += () => { const InputActionPhase phase = InputActionPhase.Performed; - base.OnActionTriggered(new CallbackContext(SprintAction, phase)); + base.OnActionTriggered(new CallbackContext(Sprint, phase)); }; uiView.SprintButton.OnRelease += () => { const InputActionPhase phase = InputActionPhase.Canceled; - base.OnActionTriggered(new CallbackContext(SprintAction, phase)); + base.OnActionTriggered(new CallbackContext(Sprint, phase)); }; uiView.JumpButton.onClick.AddListener(() => { const InputActionPhase phase = InputActionPhase.Started; - base.OnActionTriggered(new CallbackContext(JumpAction, phase)); + base.OnActionTriggered(new CallbackContext(Jump, phase)); }); } @@ -99,12 +99,12 @@ protected override void Update() { delta = processor.Process(delta, null); } - base.OnActionTriggered(new CallbackContext(LookAction, InputActionPhase.Performed, delta)); + base.OnActionTriggered(new CallbackContext(Look, InputActionPhase.Performed, delta)); } protected override void OnActionTriggered(in CallbackContext context) { - if (context.CompareActionName(LookAction)) + if (context.CompareActionName(Look)) { #if UNITY_EDITOR if (!isDebug) diff --git a/Packages/PlayerControl/Runtime/PlayerController.cs b/Packages/PlayerControl/Runtime/PlayerController.cs index a5a9a34..149a1b7 100644 --- a/Packages/PlayerControl/Runtime/PlayerController.cs +++ b/Packages/PlayerControl/Runtime/PlayerController.cs @@ -41,8 +41,6 @@ public class PlayerController : MonoBehaviour private new ITransform transform; - private EventSystem eventSystem; - private AnimHashConstants constants; private PointerEventData pointerEventData; @@ -61,8 +59,6 @@ public class PlayerController : MonoBehaviour protected ref readonly ITransform Transform => ref transform; - protected ref readonly EventSystem EventSystem => ref eventSystem; - protected ref readonly AnimHashConstants Constants => ref constants; protected bool IsDoubleJump @@ -108,22 +104,22 @@ protected Quaternion WorldRotation /// /// "Move" action name. /// - public const string MoveAction = "Move"; + public const string Move = nameof(Move); /// /// "Sprint" action name. /// - public const string SprintAction = "Sprint"; + public const string Sprint = nameof(Sprint); /// /// "Jump" action name. /// - public const string JumpAction = "Jump"; + public const string Jump = nameof(Jump); /// /// "Look" action name. /// - public const string LookAction = "Look"; + public const string Look = nameof(Look); /// /// The damper time for the move animation. @@ -133,7 +129,6 @@ protected Quaternion WorldRotation protected virtual void Start() { transform = GetComponent(); - eventSystem = EventSystem.current; constants = new AnimHashConstants(this); PlayerInput.onActionTriggered += context => OnActionTriggered(context); JumpControl.OnJump.AddListener(OnJump); @@ -141,40 +136,40 @@ protected virtual void Start() protected virtual void Update() { - Animator.SetFloat(constants.SpeedAnim, CurrentSpeed); - Animator.SetBool(constants.GroundAnim, IsOnGround); + Animator.SetFloat(constants.Speed, CurrentSpeed); + Animator.SetBool(constants.IsGround, IsOnGround); Vector3 currentDirection = LocalDirection; float deltaTime = Time.deltaTime; - Animator.SetFloat(constants.ForwardAnim, currentDirection.z, MoveDampTime, deltaTime); - Animator.SetFloat(constants.SideStepAnim, currentDirection.x, MoveDampTime, deltaTime); + Animator.SetFloat(constants.Forward, currentDirection.z, MoveDampTime, deltaTime); + Animator.SetFloat(constants.SideStep, currentDirection.x, MoveDampTime, deltaTime); } protected virtual void OnActionTriggered(in CallbackContext context) { switch (context.ActionName) { - case MoveAction when context.Phase is InputActionPhase.Performed or InputActionPhase.Canceled: + case Move when context.Phase is InputActionPhase.Performed or InputActionPhase.Canceled: MoveControl.Move(context.Value); break; - case SprintAction when context.Phase is InputActionPhase.Performed: + case Sprint when context.Phase is InputActionPhase.Performed: const float sprintHoldSpeed = 4.0f; MoveControl.MoveSpeed = sprintHoldSpeed; break; - case SprintAction when context.Phase is InputActionPhase.Canceled: + case Sprint when context.Phase is InputActionPhase.Canceled: const float sprintReleasedSpeed = 1.2f; MoveControl.MoveSpeed = sprintReleasedSpeed; break; - case JumpAction when context.Phase is InputActionPhase.Started: + case Jump when context.Phase is InputActionPhase.Started: JumpControl.Jump(); break; - case LookAction when context.Phase is InputActionPhase.Performed: + case Look when context.Phase is InputActionPhase.Performed: CameraControl.RotateCamera(context.Value); break; } } - protected virtual void OnJump() => Animator.Play(IsDoubleJump ? constants.DoubleJumpAnim : constants.JumpStartAnim); + protected virtual void OnJump() => Animator.Play(IsDoubleJump ? constants.DoubleJump : constants.JumpStart); /// /// Check if the pointer is hitting UI. @@ -184,6 +179,7 @@ protected bool IsPointerHittingUI() { Pointer device = playerInput.GetDevice(); if (device == null) return false; + EventSystem eventSystem = EventSystem.current; pointerEventData ??= new PointerEventData(eventSystem); pointerEventData.position = device.position.ReadValue(); List results = new List();