unity input中 mobile input到底是做什么用

Unity(7)
使用Unity做下列的功能实现:鼠标点击下去,出现抹布,抹布随着鼠标的移动而移动。当抹布擦拭神灯后,会出现灯神。
if (Input.GetMouseButtonDown(0))
dusterCloth = Instantiate(dusterClothPre) as GameO
dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.mousePosition);
dusterCloth.transform.parent = clothP
dusterCloth.transform.localScale = Vector3.
dusterCloth.transform.localEulerAngles = Vector3.
if (Input.GetMouseButton(0))
dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.mousePosition);
Ray ray = ca.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
if (hit.collider.name == &1& || hit.collider.name == &2&)
timeMax += Time.deltaT
if (timeMax & 1f)
timeMax = 0;
//触发按钮
Debug.Log(&Send Message Trigger&);
transform.parent.GetComponent&Sg_Aladdin&().SelectBtnMessage(hit.collider.gameObject);
if (Input.GetMouseButtonUp(0))
//删掉抹布
Destroy(dusterCloth);
timeMax = 0;
if (Input.touchCount & 0)
var touch = Input.GetTouch(0);
switch (touch.phase)
case TouchPhase.Began:
dusterCloth = Instantiate(dusterClothPre) as GameO
dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.GetTouch(0).position);
dusterCloth.transform.parent = clothP
dusterCloth.transform.localScale = Vector3.
dusterCloth.transform.localEulerAngles = Vector3.
case TouchPhase.Moved:
dusterCloth.transform.position = ca.ScreenToWorldPoint(Input.GetTouch(0).position);
Ray ray = ca.ScreenPointToRay(Input.GetTouch(0).position);
if (Physics.Raycast(ray, out hit))
uilabel.text = hit.collider.
if (hit.collider.name == &1& || hit.collider.name == &2&)
timeMax += Time.deltaT
if (timeMax & 1f)
timeMax = 0;
//触发按钮
case TouchPhase.Ended:
//删掉抹布
Destroy(dusterCloth);
timeMax = 0;
Notice : 界面摆放时,需要触发事件的UI置于最顶层。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:45289次
排名:千里之外
评论:17条
(2)(1)(1)(1)(2)(2)6804人阅读
Unity3D(5)
& &&这篇博文将简单的记录,如何用unity处理在移动设备上的触控操作。
& &&iOS和Android设备能够支持多点触控。在unity中你可以通过Input.touches属性集合访问在最近一帧中触摸在屏幕上的每一根手指的状态数据。简单的触控响应实现起很简单,不过一些复杂的触控响应或触控手势什么的,还是使用一些第三方的插件吧,当然你也可以自己封装。不管什么情况,了解决unity原生api还是非常必要的。&
& & 1、类:用来记录一个手指触摸在屏幕上的状态与位置的各种相关数据。这其它中只有两个属性是你要注意的,就是Touch.fingerId和Touch.tapCount。
& & & & & & & &: 一个Touch的标识。Input.touches数组中的同一个索引在两帧之前,指向的可不一定是同一个Touch。用来标识某个具体的touch一定要用fingerId,在分析手势时、或处理多点触控时,fingerId是非常重要的。
& & & & & & & &: 点击的总人数,这个属性可以用来模拟“双击”的效果。
& & & & & & & &这里先普及一个概念吧,一个touch的生命周期是:当一个手指接触到触屏时,产生一个Touch,并分配它一个fingerId,到这个手指离开触屏这个touch就有了。在它整个生命周期里,它的figerId是不会变的。死掉的touch所使用过fingerId当也会被之后的touch多次使用。ps:其实也不是手指一离开,这个touch就立马死掉。还有一种特殊的情况就是:在手指敲开的地方,在一个很短的时间内,一个手指又回到这个地方的话,这个touch没不会死掉,这个也是Touch.tapCount属于的由来吧。这个touch生命周期,是我自己理解的,实际情况是不是这样就不知道了,料想也不会差的太多。
& & 2、枚举:它列表描述了手指触摸的几种状态。对应Touch类中的phase属性。这是状态分别是:Began、Move、Stationary、Ended、Canceled。
& &&3、:一个Touch数组,代表着当前帧,所有手指在屏幕上的触碰状态与相关数据。(只读)
& &&4、: 触摸数量,相当于Input.touches.Length。(只读)
& &&5、:设置与指示当前系统(注意不是指设备哦!)是否启用多点触控。不过这个属性有点怪,我在电脑上测试给它赋false不会报错但完全是没有用的,它的值一值是true.
不过在我的安卓手机上测试是正常的!Ture表示支持多点触控(一般是5点);False表示单点触控。
& & 6、:安索引值获取一个Touch对象。
下面一个测试Demo
& & & & &这个Demo主要是让你的触摸可视化的显示在你的手机(或平板)屏幕上;
& & & & &并实时记录和显示了手指在屏幕上的状态和相关数据。
2、相关截图:
3、脚本就一个
* author: AnYuanLzh
using UnityE
using System.C
using System.Collections.G
public class MyTouch : MonoBehaviour
/// &summary&
/// 定义的一个手指类
/// &/summary&
class MyFinger
public int id = -1;
static private List&MyFinger& fingers = new List&MyFinger&();
/// &summary&
/// 手指容器
/// &/summary&
static public List&MyFinger& Fingers
if(fingers.Count==0)
for(int i=0; i&5; i++)
MyFinger mf = new MyFinger();
mf.id = -1;
fingers.Add(mf);
// 小圈圈:用来实时显示手指触摸的位置
GameObject[] marks = new GameObject[5];
public GameObject markPerfab =
// 粒子效果:来所显示手指手动的大概路径
ParticleSystem[] particles = new ParticleSystem[5];
public ParticleSystem particlePerfab =
// Use this for initialization
void Start ()
// init marks and particles
for(int i=0; i&MyFinger.Fingers.C i++)
GameObject mark = Instantiate(markPerfab, Vector3.zero, Quaternion.identity) as GameO
mark.transform.parent = this.
mark.SetActive(false);
marks[i] =
ParticleSystem particle = Instantiate(particlePerfab, Vector3.zero, Quaternion.identity) as ParticleS
particle.transform.parent = this.
particle.Pause();
particles[i] =
// Update is called once per frame
void Update ()
Touch[] touches = Input.
// 遍历所有的已经记录的手指
// --掦除已经不存在的手指
foreach(MyFinger mf in MyFinger.Fingers)
if(mf.id == -1)
bool stillExit =
foreach(Touch t in touches)
if(mf.id == t.fingerId)
stillExit =
if(stillExit == false)
mf.id = -1;
// 遍历当前的touches
// --并检查它们在是否已经记录在AllFinger中
// --是的话更新对应手指的状态,不是的放放加进去
foreach(Touch t in touches)
bool stillExit =
// 存在--更新对应的手指
foreach(MyFinger mf in MyFinger.Fingers)
if(t.fingerId == mf.id)
stillExit =
mf.touch =
// 不存在--添加新记录
if(!stillExit)
foreach(MyFinger mf in MyFinger.Fingers)
if(mf.id == -1)
mf.id = t.fingerId;
mf.touch =
// 记录完手指信息后,就是响应相应和状态记录了
for(int i=0; i& MyFinger.Fingers.C i++)
MyFinger mf = MyFinger.Fingers[i];
if(mf.id != -1)
if(mf.touch.phase == TouchPhase.Began)
marks[i].SetActive(true);
marks[i].transform.position = GetWorldPos(mf.touch.position);
particles[i].transform.position = GetWorldPos(mf.touch.position);
else if(mf.touch.phase == TouchPhase.Moved)
marks[i].transform.position = GetWorldPos(mf.touch.position);
if(!particles[i].isPlaying)
particles[i].loop =
particles[i].Play();
particles[i].transform.position = GetWorldPos(mf.touch.position);
else if(mf.touch.phase == TouchPhase.Ended)
marks[i].SetActive(false);
marks[i].transform.position = GetWorldPos(mf.touch.position);
particles[i].loop =
particles[i].Play();
particles[i].transform.position = GetWorldPos(mf.touch.position);
else if(mf.touch.phase == TouchPhase.Stationary)
if(particles[i].isPlaying)
particles[i].Pause();
particles[i].transform.position = GetWorldPos(mf.touch.position);
if(Input.GetKeyDown(KeyCode.Home) || Input.GetKeyDown(KeyCode.Escape))
Application.Quit();
if(Input.GetMouseButtonDown(0))
GameObject mark = Instantiate(markPerfab, Vector3.zero, Quaternion.identity) as GameO
mark.transform.parent = this.
mark.transform.position = GetWorldPos(Input.mousePosition);
ParticleSystem particle = Instantiate(particlePerfab, Vector3.zero, Quaternion.identity) as ParticleS
particle.transform.parent = this.
particle.transform.position = GetWorldPos(Input.mousePosition);
particle.loop =
particle.Play();
/// &summary&
/// 显示相关高度数据
/// &/summary&
void OnGUI()
GUILayout.Label(&支持的手指的数量:& + MyFinger.Fingers.Count);
GUILayout.BeginHorizontal(GUILayout.Width(Screen.width));
for(int i=0; i& MyFinger.Fingers.C i++)
GUILayout.BeginVertical();
MyFinger mf = MyFinger.Fingers[i];
GUILayout.Label(&手指& + i.ToString());
if(mf.id != -1)
GUILayout.Label(&Id: & + mf.id);
GUILayout.Label(&状态: & + mf.touch.phase.ToString());
GUILayout.Label(&没有发现!&);
GUILayout.EndVertical();
GUILayout.EndHorizontal();
public Vector3 GetWorldPos(Vector2 screenPos)
return Camera.main.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, Camera.main.nearClipPlane + 10));
4、相关下载(点下面的链接)
& & & & & &项目文件 和 发布好的apk,(不需要下载积分)。
& &注:这个apk对Android系统的要求是Android4.0及以上版本。
(如有转载请在文首注明出处,AnYuanLzh:)
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:29760次
排名:千里之外
原创:67篇
(5)(1)(19)(3)(1)(1)(2)(2)(28)(10)(3)Unity学习笔记之InputField&Toggle应用_jennyhigh_新浪博客
Unity学习笔记之InputField&Toggle应用
今天收获满满呀!O(∩_∩)O哈哈~好开心!!
​感谢雨谭老师对下来菜单的讲解,教程地址如下:
http://www.51zxw.net/show.aspx?id=45337&cid=529​
(虽然要给钱,还是很值得哒~~)​
先看下效果:
​功能实现:点击小图标出现答案列表,选中后答案出现在输入框内,在列表外点击关闭答案列表。
​实现步骤:
1.新建一个InputField和一个Toggle,再建一个答案列表,由多个Button组成;
2.将列表关联到Toggle上,实现点击就能出现;
​3.在Button上挂上脚本,只要选中就将文字输入InputField;
​短小精炼^_^牛!
4.最后一个功能​雨谭老师是点击背景图就直接将列表消失,但是有点小问题,Toggle没有还原。所以这里我小改动了一下,直接作用Toggle,让Toggle去控制列表的消失。
直接将这个脚本挂在背景Panel上,这里面有个小插曲,gameObject.SetActive不能放在if的括号里面进行判断,所以我使用了:isActiveAndEnabled(我是个天才^_^)
​5.当然和雨谭老师不一样的地方,我还需要设计选择判断功能,这个其实在上一篇已经说到了:
​挂在按钮上直接使用,轻松加愉快!!开心O(∩_∩)O~~
博客等级:
博客积分:0
博客访问:7,531
关注人气:0
荣誉徽章:多点触控时,下标是从0开始的,两个触控点下标就是0,1。
代码如下:
nt touchCount = 2; // 触摸帧的数量
if(touchCount == Input.touchCount()){
vector2 touchPosition1 = Input.GetTouch(0).
vector2 touchPosition2 = Input.GetTouch(1).
1.Input.touchCount 触摸随之增长,一秒50次增量。
2.Input.GetTouch(0).phase==TouchPhase.Moved 手指滑动中最后一帧滑动的状态是运动的。
3.TouchPhase& 触摸的几个状态。
4.Touch.deltaPosition 增量位置(Input.GetTouch(0).deltaPosition)最后一帧滑动的值,只返回xy轴坐标,也可用vector3(z轴为0),所以一般用vector2接收。
static var aa:int;
function Update () {
if(Input.touchCount&0)
print(Input.touchCount);
function OnGUI()
GUI.Label(Rect(34,34,34,34),"sdff");
touchCount指的是触摸帧的数量。要注意的是:touch事件 只能在模拟器或者真机上运行(已测试通过),大约一秒钟touch不放。touchCount+50次左右。2.Input.touches 触摸列表。
// Prints number of fingers touching the screen
//输出触摸在屏幕上的手指数量
function Update () {
var fingerCount = 0;
for (var touch : Touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
fingerCount++;
if (fingerCount & 0)
print ("User has " + fingerCount + " finger(s) touching the screen");
3.让cube随着touch 移动代码:
static var count:int; //定义touchCount数
var particle_:GameO//定义存放cube对象
var touchposition:Vector3; //存储移动三维坐标值
function Update () {
if(Input.touchCount&0)
count+=Input.touchC}
if((Input.touchCount&0&&Input.GetTouch(0).phase==TouchPhase.Moved)) //[color=Red]如果点击手指touch了
并且手指touch的状态为移动的[/color]
touchposition=Input.GetTouch(0).deltaP
//[color=Red]获取手指touch最后一帧移动的xy轴距离[/color]
particle_.transform.Translate(touchposition.x*0.01,touchposition.y*0.01,0);//[color=Red]移动这个距离[/color]
function OnGUI()
GUI.Label(Rect(10,10,100,30),"cishu:"+count.ToString());
GUI.Label(Rect(10,50,100,30),touchposition.ToString());
阅读(...) 评论()}

我要回帖

更多关于 unity input manager 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信