Unity开发Android程序,拖拽放大缩小物体
2014-10-31 13:42
288 查看
Vector2 m_screenpos = new Vector2(); // 记录手指触屏的位置
void Start () {
Input.multiTouchEnabled = true; // 允许多点触控
}
void Update () {
#if !UNITY_EDITOR && ( UNITY_IOS || UNITY_ANDROID )
MobileInput();
#else
DesktopInput();
#endif
}
// 桌面系统鼠标操作
void DesktopInput()
{
// 记录鼠标左键的移动距离
float mx = Input.GetAxis("Mouse X");
float my = Input.GetAxis("Mouse Y");
if ( mx!= 0 || my !=0 )
{
//松开鼠标左键
if (Input.GetMouseButton(0))
{
Camera.main.transform.Translate(new Vector3(mx*Time.deltaTime, my * Time.deltaTime, 0));
}
}
}
// 移动平台触屏操作
void MobileInput()
{
if (Input.touchCount <= 0)
return;
// 1个手指触摸屏幕
if (Input.touchCount == 1)
{
if (Input.touches[0].phase == TouchPhase.Began)
{
// 记录手指触屏的位置
m_screenpos = Input.touches[0].position;
}
// 手指移动
else if (Input.touches[0].phase == TouchPhase.Moved)
{
// 移动摄像机
Camera.main.transform.Translate(new Vector3(Input.touches[0].deltaPosition.x * Time.deltaTime, Input.touches[0].deltaPosition.y * Time.deltaTime, 0));
}
// 手指离开屏幕 判断移动方向
if (Input.touches[0].phase == TouchPhase.Ended &&
Input.touches[0].phase != TouchPhase.Canceled)
{
Vector2 pos = Input.touches[0].position;
// 手指水平移动
if (Mathf.Abs(m_screenpos.x - pos.x) > Mathf.Abs(m_screenpos.y - pos.y))
{
if (m_screenpos.x > pos.x){
//手指向左划动
}
else{
//手指向右划动
}
}
else // 手指垂直移动
{
if (m_screenpos.y > pos.y){
//手指向下划动
}
else{
//手指向上划动
}
}
}
}
else if ( Input.touchCount >1 )
{
// 记录两个手指的位置
Vector2 finger1 = new Vector2();
Vector2 finger2 = new Vector2();
// 记录两个手指的移动
Vector2 mov1 = new Vector2();
Vector2 mov2 = new Vector2();
for (int i=0; i<2; i++ )
{
Touch touch = Input.touches[i];
if (touch.phase == TouchPhase.Ended )
break;
if ( touch.phase == TouchPhase.Moved )
{
float mov = 0;
if (i == 0)
{
finger1 = touch.position;
mov1 = touch.deltaPosition;
}
else
{
finger2 = touch.position;
mov2 = touch.deltaPosition;
if (finger1.x > finger2.x)
{
mov = mov1.x;
}
else
{
mov = mov2.x;
}
if (finger1.y > finger2.y)
{
mov+= mov1.y;
}
else
{
mov+= mov2.y;
}
Camera.main.transform.Translate(0, 0, mov * Time.deltaTime);
}
}
}
}
}
void Start () {
Input.multiTouchEnabled = true; // 允许多点触控
}
void Update () {
#if !UNITY_EDITOR && ( UNITY_IOS || UNITY_ANDROID )
MobileInput();
#else
DesktopInput();
#endif
}
// 桌面系统鼠标操作
void DesktopInput()
{
// 记录鼠标左键的移动距离
float mx = Input.GetAxis("Mouse X");
float my = Input.GetAxis("Mouse Y");
if ( mx!= 0 || my !=0 )
{
//松开鼠标左键
if (Input.GetMouseButton(0))
{
Camera.main.transform.Translate(new Vector3(mx*Time.deltaTime, my * Time.deltaTime, 0));
}
}
}
// 移动平台触屏操作
void MobileInput()
{
if (Input.touchCount <= 0)
return;
// 1个手指触摸屏幕
if (Input.touchCount == 1)
{
if (Input.touches[0].phase == TouchPhase.Began)
{
// 记录手指触屏的位置
m_screenpos = Input.touches[0].position;
}
// 手指移动
else if (Input.touches[0].phase == TouchPhase.Moved)
{
// 移动摄像机
Camera.main.transform.Translate(new Vector3(Input.touches[0].deltaPosition.x * Time.deltaTime, Input.touches[0].deltaPosition.y * Time.deltaTime, 0));
}
// 手指离开屏幕 判断移动方向
if (Input.touches[0].phase == TouchPhase.Ended &&
Input.touches[0].phase != TouchPhase.Canceled)
{
Vector2 pos = Input.touches[0].position;
// 手指水平移动
if (Mathf.Abs(m_screenpos.x - pos.x) > Mathf.Abs(m_screenpos.y - pos.y))
{
if (m_screenpos.x > pos.x){
//手指向左划动
}
else{
//手指向右划动
}
}
else // 手指垂直移动
{
if (m_screenpos.y > pos.y){
//手指向下划动
}
else{
//手指向上划动
}
}
}
}
else if ( Input.touchCount >1 )
{
// 记录两个手指的位置
Vector2 finger1 = new Vector2();
Vector2 finger2 = new Vector2();
// 记录两个手指的移动
Vector2 mov1 = new Vector2();
Vector2 mov2 = new Vector2();
for (int i=0; i<2; i++ )
{
Touch touch = Input.touches[i];
if (touch.phase == TouchPhase.Ended )
break;
if ( touch.phase == TouchPhase.Moved )
{
float mov = 0;
if (i == 0)
{
finger1 = touch.position;
mov1 = touch.deltaPosition;
}
else
{
finger2 = touch.position;
mov2 = touch.deltaPosition;
if (finger1.x > finger2.x)
{
mov = mov1.x;
}
else
{
mov = mov2.x;
}
if (finger1.y > finger2.y)
{
mov+= mov1.y;
}
else
{
mov+= mov2.y;
}
Camera.main.transform.Translate(0, 0, mov * Time.deltaTime);
}
}
}
}
}
相关文章推荐
- unity 鼠标拖拽物体旋转,滚轮放大缩小(一)
- Android开发--Matrix(一)--实现图片的动态放大缩小
- Bitmap位图渲染与操作(图片移动,放大,缩小,旋转,镜像发转)--android游戏开发
- Android开发--Matrix(一)--实现图片的动态放大缩小
- Android手机开发:图片的放大和缩小显示ImageView
- unity 实现了鼠标滚动放大和缩小物体暨拉近拉远相机的效果
- Android 图片拖拽、放大缩小的自定义控件
- android+unity游戏开发之求物体间的距离
- Android开发_拖动效果,旋转效果,两点放大缩小的实现
- Android开发之GridView的Item放大缩小
- Android 图片拖拽、放大缩小的自定义控件
- unity中通过收拾实现物体旋转和放大缩小
- Android 图片拖拽、放大缩小的自定义控件
- Android开发之GridView的Item放大缩小
- android+unity游戏开发之求物体间的距离
- Android引路蜂地图开发示例:放大、缩小
- Android手机开发:图片的放大和缩小显示ImageView
- unity手势控制、放大缩小、拖拽、滑动。单击
- 菜鸟的android开发笔记 之 第二弹 第一个程序HelloAndroid
- 翻译:Android程序开发入门