您的位置:首页 > 移动开发 > Unity3D

unity 插件iTween 官方例子学习心得

2014-06-10 15:40 204 查看
在学习unity的时候会用到一些强大的插件,iTween就是一个,它是一个动画库,它的应用非常广泛,学习它推荐可以看一下官方给的例子很是经典,应该包括了它里面的所有方法的用法,还有能产生的效果,很强大,之所以有这个插件,作者是奔着以最小的投入产生最大的产出,不说了,直接上例子,现在我们来学习第一个例子Flythrough:

1。创建场景物体,三个Cube、一个plane作为地面,两个pointlight调整摄像机的位置使得视图恰可以斜视场景。如图



给三个Cube上不一样颜色的材质吧看着舒服一点。



2、控制摄像机运动

新建一个C#脚本文件命名“cameracontroller”

<pre name="code" class="csharp">using UnityEngine;
using System.Collections;

public class cameraController : MonoBehaviour {
public Transform [] movepath;  //摄像机移动路径节点
public Transform [] targetpath;//摄像机投射方向路径节点
public Transform  looktarget;//摄像机投射点
public float value;

private float  redposition=0.16f;
private float  blueposition=0.53f;
private float greenposition=1.0f;

//gui style;
public Font font;
private GUIStyle style=new GUIStyle ();

// Use this for initialization
void Start () {
style.font=font;

}
void OnGUI()
{
value =GUI.VerticalSlider (new Rect (Screen.width -20,20,15,Screen .height -40),value ,1,0); //垂直滑动条,控制摄像机移动
iTween .PutOnPath (gameObject ,movepath ,value );//iTween方法,在movepath节点构成的路径上根据value的值把gameobject移动到相应的位置
iTween .PutOnPath (looktarget ,targetpath ,value );
transform .LookAt (iTween .PointOnPath (targetpath,value ));//iTween.pointonpath是根据value的值把looktarget移动到相应的位置,然后摄像机投射向那个位置

if(GUI.Button (new Rect (5,Screen .height -25,50,20),"red"))
{
SlideTo(redposition );

}
if(GUI.Button (new Rect (60,Screen .height -25,50,20),"blue"))
{
SlideTo(blueposition );

}
if(GUI.Button (new Rect (115,Screen .height -25,50,20),"green"))
{
SlideTo(greenposition );

}
}
void OnDrawGizmos()
{
iTween .DrawPath (movepath ,Color.magenta );
iTween .DrawPath (targetpath ,Color .cyan );
Gizmos .color =Color.black;
Gizmos .DrawLine (transform .position ,looktarget .position );

}
void SlideTo(float position)
{
iTween .Stop (gameObject );//销毁gameobject上的所有iTween
//插值方法
iTween.ValueTo (gameObject,iTween.Hash ("from",value ,"to",position ,"time",2,"easetype",iTween.EaseType.easeInOutCubic,"onupdate","Slidevalue"));

}
void Slidevalue(float p)

{
value=p;
}
// Update is called once per frame
void Update () {

}
}



脚本的作用就是根据所给的值让摄像机移动到相应的位置,代码很简单

因为定义了摄像机移动的路径和投射点的路径节点所以在Hierarchy面板中新建空物体作为节点,如图:



然后把相应的节点拖动到脚本的相应位置:



那就看一下咱的阶段性效果吧;


曲线位置编辑是比较好时间的,不知道哪位大神能给个简单的方法,可以省点时间。

3.给每个节点分别加个可视化的线条球以便与编辑;

新建C#脚本,命名“targetpath”

using UnityEngine;
using System.Collections;

public class targetpath : MonoBehaviour {

void OnDrawGizmos()
{
Gizmos .color =Color.cyan ;
Gizmos .DrawSphere(transform .position ,0.25f);

}
}
//

Gizmos 辅助线框

Gizmos are used to give visual debugging or setup aids in the scene view.

Gizmos是用于在场景视图可视化调试或辅助设置。

不懂得话看文档啊,其实我也是用到了才看的,我是菜鸟啦,
到此基本完工了,自己刚刚写blog可能有很多地方说的不够详实,原谅一下菜鸟啦。

看一下效果如何:



这是我的学习笔记了,代码对照着官方的文档很容易就能读懂的,也达到了iTween的初衷,以最小的投入得到最大的产出。行了,在学习中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: