動きを制御するInputクラスの説明
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
/// <summary>
/// 按键输入管理器
/// </summary>
public class InputKeysManager : SingletonPatternBase<InputKeysManager>
{
//表示是否启用本脚本的功能,监听玩家按键输入
public bool IsActive { get;private set;}
/// <summary>
/// 是否激活按键监听
///传入true,会激活按键监听,这样玩家通过按键输入管理器输入的操作才会生效
///传入false,禁用按键监听,这样玩家通过按键输入管理器输入的操作就会无效
/// </summary>
/// <param name="isActive"></param>
public void SetActive(bool isActive)
{
IsActive = isActive;
Debug.Log(isActive);
}
public InputKeysManager()
{
MonoManager.Instance.AddUpdateListener(CheckKeys);
}
void CheckKeys()
{
if (!IsActive)
{
return;
}
foreach (var item in Enum.GetValues(typeof(KeyCode)))
{
CheckKeysCode((KeyCode)item);
}
for (int i = 0; i <= 2; i++)
{
if (Input.GetMouseButtonDown(i))
{
EventCenterManager.Instance.DisPatch<int>(E_InputCommand.GetMouseButtonDown, i);
}
if (Input.GetMouseButtonUp(i))
{
EventCenterManager.Instance.DisPatch<int>(E_InputCommand.GetMouseButtonUp, i);
}
if (Input.GetMouseButton(i))
{
EventCenterManager.Instance.DisPatch<int>(E_InputCommand.GetMouseButton, i);
}
}
}
void CheckKeysCode(KeyCode key)
{
if (Input.GetKeyDown(key))
{
EventCenterManager.Instance.DisPatch<KeyCode>(E_InputCommand.GetKeyDown, key);
}
if (Input.GetKeyUp(key))
{
EventCenterManager.Instance.DisPatch<KeyCode>(E_InputCommand.GetKeyUp, key);
}
if (Input.GetKey(key))
{
EventCenterManager.Instance.DisPatch<KeyCode>(E_InputCommand.GetKey, key);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 玩家控制的英雄对象
/// </summary>
public class Hero : MonoBehaviour
{
//移动方向
Vector3 movementDirection;
//运动速度
public float movementSpeed = 5;
void Awake()
{
EventCenterManager.Instance.AddListener<KeyCode>(E_InputCommand.GetKey, CheckInputKeys);
EventCenterManager.Instance.AddListener<KeyCode>(E_InputCommand.GetKeyUp,CheckInputKeyUp);
}
void CheckInputKeys(KeyCode key)
{
switch (key)
{
case KeyCode.W:
movementDirection.Set(movementDirection.x,1,movementDirection.z);
break;
case KeyCode.S:
movementDirection.Set(movementDirection.x, -1, movementDirection.z);
break;
case KeyCode.A:
movementDirection.Set(-1, movementDirection.y, movementDirection.z);
break;
case KeyCode.D:
movementDirection.Set(1, movementDirection.y, movementDirection.z);
break;
}
}
void CheckInputKeyUp(KeyCode key)
{
switch (key)
{
case KeyCode.W:
case KeyCode.S:
movementDirection.Set(movementDirection.x, 0, movementDirection.z);
break;
case KeyCode.A:
case KeyCode.D:
movementDirection.Set(0, movementDirection.y, movementDirection.z);
break;
}
}
void OnDestroy()
{
EventCenterManager.Instance.RemoveListener<KeyCode>(E_InputCommand.GetKey, CheckInputKeys);
EventCenterManager.Instance.RemoveListener<KeyCode>(E_InputCommand.GetKeyUp, CheckInputKeyUp);
}
Update is called once per frame
//void Update()
//{
// //计算出运动量 -- 应该将运动写到FixUpdate中
// if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A))
// {
// movementDirection = new Vector3(-1, 1, 0); //左上方 (-1,1,0)
// }
// else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D))
// {
// movementDirection = new Vector3(1, 1, 0); //右上方 (-1,1,0)
// }
// else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A))
// {
// movementDirection = new Vector3(-1, -1, 0); //左下方 (-1,1,0)
// }
// else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D))
// {
// movementDirection = new Vector3(1, -1, 0); //右下方 (-1,1,0)
// }
// else if (Input.GetKey(KeyCode.W))
// {
// movementDirection = Vector3.up; //上方 (0,1,0)
// }
// else if(Input.GetKey(KeyCode.S))
// {
// movementDirection = Vector3.down; //下 (0,-1,0)
// }
// else if (Input.GetKey(KeyCode.A))
// {
// movementDirection = Vector3.left; //左 (-1,0,0)
// }
// else if (Input.GetKey(KeyCode.D))
// {
// movementDirection = Vector3.right; //右 (1,0,0)
// }
// else
// {
// movementDirection = Vector3.zero;
// }
//}
private void FixedUpdate()
{
transform.Translate(movementSpeed * movementDirection * Time.fixedDeltaTime);
}
}