一、下载并试玩
首先去Asset Store下载该插件,过程不多赘述,下载完并导入。
可以先来体验一下,你就明白这个插件好不好玩了,点击那个Examples.unity,然后就可以试玩了。。
二、尝试自己用一下
创建一个场景,然后把TouchManager及Cursors预制体拖到Scene窗口下,点击运行:
来看一下输出:初始化Unity触摸输入;初始化Unity鼠标输入;没有找到触摸层,给main camera加上标准层。
恩。再来看一下这两个预制体上用到的代码····可以,很牛批,下一题:
继续往场景里加一个cube,然后给cube加上Transformer、Transform Gesture这两个组件,Transformer负责监听TransformGesture的事件并把响应的事件应用到GameObject上:
你会发现,鼠标可以拖动那个cube动。
三、研究插件给的其中一个Demo
由于时间关系,只研究一下Colors这个Demo。
说一下这个游戏怎么玩儿的,就是点击鼠标移动圆圈,圆圈吃圆圈。想要实现双指放大也容易,就是按住ALT键,在圆圈上整两个鼠标定点就行了。
首先来看Camera挂载的代码,很明显就是hit只检测2D物体了,既然游戏里的圆圈是可以被拖动并融合的,所以肯定就是这些圆圈了。
我们来看圆圈,发现Scene这个空物体上挂载了Colors.cs,它用到了Circle预制体,我们来看看这个预制体
发现这个预制体用了两个Collider,一个半径是0.1,一个半径是0.5,并且半径为0.5的是Is Trigger的(下图这两个红色的圆圈),然后有2D刚体组件,并且挂载了Circle.cs,然后还有例行公事的Transform Gesture和Transformer(可以不用管这两个是干啥的,只要记住凡是场景内的物体想要有触摸的效果,就得加上这两个组件)。
我们可以看到Transform Gesture上面有Translation、Rotation、Scaling,意思就是用户可以移动、缩放和旋转圆。Transformer监听TransformGesture事件。
好了,我们来看看Colors.cs文件,其实很短,就41行代码····
/*
* @author Valentin Simonov / http://va.lent.in/
*/
using UnityEngine;
using System.Collections.Generic;
namespace TouchScript.Examples.Colors
{
/// <exclude />
public class Colors : MonoBehaviour
{
public Transform Prefab;
public int Total = 10;
private List<Color> colors = new List<Color>()
{
Color.blue,
Color.cyan, //蓝绿
Color.gray,
Color.green,
Color.magenta, //洋红
Color.red,
Color.white,
Color.yellow,
Color.black
};
void Start()
{
var container = transform.Find("Container"); //找到名字为Container的物体
for (var i = 0; i < Total; i++)
{
var obj = Instantiate(Prefab) as Transform; //预制体实例化
obj.SetParent(container); //设置该circle的父是container
obj.localPosition = new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), 0); //设置该circle的位置
obj.GetComponent<Renderer>().material.color = colors[Random.Range(0, colors.Count)]; //设置该circle的颜色
}
}
}
}
我们再来看Circle.cs,也很短,就53行代码....
/*
* @author Valentin Simonov / http://va.lent.in/
*/
using UnityEngine;
using TouchScript.Gestures;
using TouchScript.Gestures.TransformGestures;
namespace TouchScript.Examples.Colors
{
/// <exclude />
public class Circle : MonoBehaviour
{
private bool isDestroyed = false;
public Color Kill()
{
isDestroyed = true;
GetComponent<TransformGesture>().Cancel(true, true);
GetComponent<TransformGesture>().Cancel(true, true);
var color = GetComponent<Renderer>().sharedMaterial.color;
Destroy(gameObject);
return color;
}
private void OnTriggerEnter2D(Collider2D other) //当两个Circle碰撞到一起时
{
if (isDestroyed) return;
var gesture = GetComponent<TransformGesture>(); //获取TransformGesture组件
if (gesture.State != Gesture.GestureState.Changed && gesture.State != Gesture.GestureState.Began) return;
var otherCircle = other.GetComponent<Circle>();
if (!otherCircle) return;
var otherColor = otherCircle.Kill(); //获取另外一个Circle的颜色,并销毁另外一个Circle
var scale =
Mathf.Sqrt(otherCircle.transform.localScale.x*otherCircle.transform.localScale.x +
transform.localScale.x*transform.localScale.x); //获取新的半径
var color = Color.Lerp(GetComponent<Renderer>().sharedMaterial.color, otherColor, .5f); //获取新的颜色
var obj = Instantiate(gameObject) as GameObject; //新创建一个物体
obj.transform.SetParent(transform.parent); //继承原Circle的父
obj.transform.localPosition = transform.localPosition; //继承原Circle的位置
obj.transform.localRotation = transform.localRotation; //继承原Circle的旋转角
obj.transform.localScale = new Vector3(scale, 1, scale); //得到新的大小
obj.GetComponent<Renderer>().sharedMaterial.color = color; //显示新的颜色
Kill(); //销毁原Circle及TransformerGesture的“关注”
}
}
}
GetComponent<TransformGesture>().Cancel(true, true); 这里表示指针返回到系统,此时取消的指针会被新的(更大的)圆圈及其TransformGesture捕获,因此用户可以继续移动并调整新圆圈的大小。
OK,似乎没什么问题了。就酱。
参考博客:https://www.jianshu.com/p/4e9f0d65f896?from=singlemessage