Unity3d 不同设备之间 鼠标或者手势的判断

来源:互联网 发布:中银淘宝信用卡额度 编辑:程序博客网 时间:2024/06/08 15:06

//判断是否是触摸设备

using UnityEngine;

namespace Liulala.Util

{

public static class PlatformUtil

{

publicstatic bool IsTouchDevice

{

get

{

returnApplication.platform == RuntimePlatform.IPhonePlayer || 

                      Application.platform == RuntimePlatform.Android;

}

}

}

}

//手势和触摸的判断

using UnityEngine;

using Liulala.Util;


public interface IGameInput

{

bool IsClickDown {get; }


bool IsClickUp {get; }


bool IsClicking {get; }


bool HasTouch {get; }


Vector3 MousePosition {get; }

int TouchCount {get; }

}


publicclass DesktopGameInput : IGameInput

{

publicbool IsClickDown

{

get

{

returnInput.GetMouseButtonDown(0);

}

}


publicbool IsClickUp

{

get

{

returnInput.GetMouseButtonUp(0);

}

}


publicbool IsClicking

{

get

{

returnInput.GetMouseButton(0);

}

}


publicVector3 MousePosition

{

get {return Input.mousePosition; }

}


publicbool HasTouch { get {return true; } }

publicint TouchCount { get {return1; } }

}


publicclass SingleTouchGameInput : IGameInput

{

publicbool IsClickDown

{

get

{

returnInput.touchCount == 1 && Input.GetTouch(0).phase ==TouchPhase.Began;

}

}


publicbool IsClickUp

{

get

{

returnInput.touchCount == 1 && Input.GetTouch(0).phase ==TouchPhase.Ended;

}

}


publicbool IsClicking

{

get

{

returnInput.touchCount == 1 && Input.GetTouch(0).phase ==TouchPhase.Stationary;

}

}


publicVector3 MousePosition

{

get

{

if (Input.touchCount ==1)

{

returnInput.GetTouch(0).position;

}

else

{

returnInput.mousePosition;

}

}

}


publicbool HasTouch

{

get

{

returnInput.touchCount > 0;

}

}

publicint TouchCount

{

get

{

returnInput.touchCount;

}

}

}


public static class GameControl

{

publicstatic IGameInput Input

{  

get

{

if (_gameInput ==null)

{

Initialize();

}

return _gameInput; 

}  

}


privatestatic void Initialize()

{

if (PlatformUtil.IsTouchDevice)

{

_gameInput =new SingleTouchGameInput();

}

else

{

_gameInput =new DesktopGameInput();

}

}


privatestatic IGameInput _gameInput;

}


通过GameControl.Input. 调用即可