您的位置:首页 > 其它

ItweenPath使用

2015-10-28 15:26 288 查看
ItweenPath可以为物体可视化绘制一条路径,一般由最多十个点组成的路径,路径可以直接在编辑器中拖动,也可以通过代码来动态进行设置。

新建一个场景,添加一个精灵,挂上ItweenPath的脚本



此时在scene中可以看到一条路径,可以拖动上面的点自定义一条路径出来如下图:



接下来添加一个测试脚本test.cs

脚本首先要获取到场景中的path路径,然后作为Itween中MoveTo里面的参数,执行移动

代码如下:

using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {

private Vector3[] path;
// Use this for initialization
void Start () {

path = iTweenPath.GetPath("path");
iTween.MoveTo(this.gameObject, iTween.Hash("path", path, "time", 5, "easetype", iTween.EaseType.spring));

}
// Update is called once per frame
void Update () {

}
}


运行,就可以看到图标按照路径开始移动。

接下来是自定义ItweenPath路径,我们可以设定路径中的某个点为代码可以控制的点。

首先,在场景中建造六个空物体,它们的位置设定为路径中经过的位置。



将这些物体的位置设定为路径中的点需要用到ItweenPath中的Set方法。

代码如下:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class test : MonoBehaviour {

private Vector3[] path;
private List<GameObject> go_list;
// Use this for initialization
void Start () {

go_list = new List<GameObject>(10);
for (int i = 0; i < 6; i++)
{
go_list.Add(GameObject.Find((i+1).ToString()));
}
path = iTweenPath.GetPath("path");
for (int j = 0; j < 6; j++)
{
path[j].Set(go_list[j].transform.position.x, go_list[j].transform.position.y, go_list[j].transform.position.z);

}
iTween.MoveTo(this.gameObject, iTween.Hash("path", path, "time", 5, "easetype", iTween.EaseType.spring));

}
// Update is called once per frame
void Update () {

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  path