查看: 1227|回复: 0
打印 上一主题 下一主题

[经验分享] unity3d在2d游戏中同时拖动多个collider

[复制链接]

100

主题

3

听众

7683

积分

高级设计师

Rank: 6Rank: 6

纳金币
2378
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2015-3-2 00:08:21 |只看该作者 |倒序浏览

最近,又在弄unity3d,因为unity3d提供了官方的2d游戏设计工具,用下来还不错。

就比较而言,unity3d和国内比较流行的cocos2d比较而言,unity3d的商城更好用,或者说外部支持做得更好。


在2d游戏中同时拖动多个对象,找了很久没找到代码,只好自己写了。虽然看上去,会有bug,不过,性能比我能想到的其它方法要好。


[csharp] view plaincopy


  • /**
  • *
  • * 作者:红尘不到
  • * 日期:2015年2月13日
  • * 版本:1.0
  • * 说明:多个对象拖动
  • **/  
  • using UnityEngine;  
  • using System.Collections;  
  •   
  • /// <summary>  
  • /// 多个对象拖动  
  • /// </summary>  
  • public class MultipleDrag : MonoBehaviour  
  • {  
  •     /// <summary>  
  •     /// 触摸总数  
  •     /// </summary>  
  •     private int touchCount = -1;  
  •     /// <summary>  
  •     /// 触摸ID  
  •     /// </summary>  
  •     private int touchID = -1;  
  •   
  •     /// <summary>  
  •     /// 每帧更新  
  •     /// </summary>  
  •     private void Update()  
  •     {  
  •         if (Input.touchCount > 0)  
  •         {  
  •   
  •             if (touchCount != Input.touchCount)  
  •             {  
  •                 //当触摸总数改变,从新获得触摸ID  
  •                 touchID = -1;  
  •                 touchCount = Input.touchCount;  
  •                 for (int i = 0; i < touchCount; i++)  
  •                 {  
  •                     Vector3 wp = Camera.main.ScreenToWorldPoint(Input.touches.position);  
  •                     Vector2 touchPos = new Vector2(wp.x, wp.y);  
  •                     if (collider2D == Physics2D.OverlapPoint(touchPos))  
  •                     {  
  •                         touchID = Input.touches.fingerId;  
  •                         break;  
  •                     }  
  •                 }  
  •             }  
  •             else  
  •             {  
  •                 //当触摸总数没变,根据触摸ID定位  
  •                 if (touchID > -1)  
  •                 {  
  •                     for (int i = 0; i < Input.touchCount; i++)  
  •                     {  
  •                         if (Input.touches.fingerId == touchID)  
  •                         {  
  •                             Vector3 wp = Camera.main.ScreenToWorldPoint(Input.touches.position);  
  •                             transform.position = new Vector2(wp.x, wp.y);  
  •                             break;  
  •                         }  
  •                     }  
  •                 }  
  •             }  
  •         }  
  •         else  
  •         {  
  •             touchCount = -1;  
  •             touchID = -1;  
  •         }  
  •     }  
  • }  



另外,这个方法只能对collider对象起作用。如果同时拖动多个4.6 ui的对象,我也没想到好的办法。

如果是需要拖动3d界面下的,可以看看高通的这段程序,也许有帮助。https://developer.vuforia.com/resources/dev-guide/unity-drag-ar-object-screen-your-finger


分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-29 22:13 , Processed in 0.093740 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部