C# InputManager示例

来源:互联网 发布:mac系统清理工具 免费 编辑:程序博客网 时间:2024/06/05 10:39
using UnityEngine;using System.Collections;public class InputManager : MonoBehaviour {public bool useTouch = false;//Use touch based controlspublic LayerMask mask = -1;//Set input layer maskRay ray;//The hit rayRaycastHit hit;//The hit raycastTransform button;//The triggered button//Called at every framevoid Update () {if (useTouch)GetTouches();elseGetClicks();}//If playing with mousevoid GetClicks(){//If we pressed the mouseif(Input.GetMouseButtonDown(0)){//Cast a rayray = Camera.main.ScreenPointToRay(Input.mousePosition);//If the ray hit something in the set layerif (Physics.Raycast(ray, out hit, Mathf.Infinity, mask)){//Register it, and send it to the GUI managerbutton = hit.transform;GUIManager.Instance.ButtonDown(button);}//If the ray didn't hit a GUI objectelse{//Set the button to null, and move the sub upbutton = null;                PlayerManager.Instance.MoveUp();}}//If the click was releasedelse if (Input.GetMouseButtonUp(0)){//If there is no button registered previouselyif (button == null)//Move the sub down                PlayerManager.Instance.MoveDown();//If there is a button registeredelse//Send it to the GUI manager                GUIManager.Instance.ButtonUp(button);}//Used in testing to reset the status/*if (Input.GetKey(KeyCode.P)){SaveManager.CreateData();missionManager.ResetDataString();}*/}//If playing with touch screenvoid GetTouches(){//Loop through the touchesforeach (Touch touch in Input.touches) {//If a touch has happened            if (touch.phase == TouchPhase.Began && touch.phase != TouchPhase.Canceled){//Cast a rayray = Camera.main.ScreenPointToRay(touch.position);//If the ray hit something in the set layerif (Physics.Raycast(ray, out hit, Mathf.Infinity, mask)){//Register it, and send it to the GUI managerbutton = hit.transform;                    GUIManager.Instance.ButtonDown(button);}//If the ray didn't hit a GUI objectelse{//Set the button to null, and move the sub upbutton = null;                    PlayerManager.Instance.MoveUp();}}//If a touch has endedelse if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled){//If there is no button registered previouselyif (button == null)//Move the sub down                    PlayerManager.Instance.MoveDown();//If there is a button registeredelse//Send it to the GUI manager                    GUIManager.Instance.ButtonUp(button);}}}}

原创粉丝点击