ItweenPath使用

来源:互联网 发布:怎么开淘宝网店货源 编辑:程序博客网 时间:2024/06/01 08:48

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 () {    }}
0 0
原创粉丝点击