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

unity3D学习之ITween例子测试-UI菜鸟笔记1

2017-08-08 20:41 411 查看
尝试着模仿ITween官网上的例子,写的一个交互UI:

1、先创建一个Prefab格子:



2、创建脚本:

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

public class TextDemo : MonoBehaviour {

//调用ITween插件测试例子

public GameObject box;
public float Size = 9;
private GameObject prefa;

// Use this for initialization
void Start () {
//创建方格
for (int i = 0; i < Size; i++)
{
for (int j = 1; j < Size; j++)
{
//创建预制体并转为gameobject方便变色
GameObject boxer = Instantiate(box, new Vector3(i, 0, j), Quaternion.identity);
if ((i+j)%2 == 0)
{
//偶数变黑色
iTween.ColorTo(boxer, Color.black, .5f);
}
}
}
}

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

//射线检测
RaycastHit hitinfo;

//返回一条射线从摄像机通过一个屏幕点(屏幕位置转射线)
//通过屏幕获取空间位置
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

//当鼠标的射线碰到box预制体时触发,返回真
//out返回Raycast中的RaycastHit(直接返回不用赋值,ref则要)
bool iscollider = Physics.Raycast(ray, out hitinfo);
if (iscollider)
{
if (hitinfo.collider.tag == "Box")
{
ChangeColorAndMoveup(hitinfo.collider.gameObject);
}
}else if(prefa != null)
{
reseting();
}

}

//改变颜色和位置
void ChangeColorAndMoveup(GameObject bos)
{
//上一个box恢复为原来的状态(其实是强行改回原来的设定)
if (prefa !=null && prefa != bos)
{
reseting();
}
//变为绿色和上移
if (bos.transform.position.y ==0)
{
iTween.ColorTo(bos, Color.green, .01f);
iTween.MoveTo(bos, new Vector3(bos.transform.position.x, .5f, bos.transform.position.z), .01f);
prefa = bos;
}

}

void reseting()
{
if ((prefa.transform.position.x + prefa.transform.position.z) % 2 == 0)
{
iTween.ColorTo(prefa, Color.black, 2f);
}
else
{
iTween.ColorTo(prefa, Color.white, 2f);
}
iTween.MoveTo(prefa, new Vector3(prefa.transform.position.x, 0, prefa.transform.position.z), 2f);
}
}


3、把脚本绑在MainCamera上:



4、最后实现的效果:



5、下面是自己用DoTween瞎搞的一个练习:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class MoveDemo : MonoBehaviour {

//DoTween扩展性强
public Transform whitecube, blackcube, greencube;
public Vector3[] theway = new[] {
new Vector3(2,2,0),
new Vector3(6,2,2),
new Vector3(8,8,20),
new Vector3(0,3,3),
};

// Use this for initialization
void Start () {
whitecube.DOMove(new Vector3(3, 0, 0), 2f);
blackcube.DOMove(new Vector3(3, 4, 0), 2f).From(false);

//setlookat使物体面向前进的方向
//setEase设置物体的动画曲线(枚举类型)
//setloops循环
//setoptions使循环无缝连接
greencube.DOPath(theway, 4, PathType.CatmullRom).SetLookAt(.00001f).SetEase(Ease.Linear).SetLoops(-1).SetOptions(true);
Material mat = greencube.GetComponent<Renderer>().material;

//_EmissionColor发光颜色
mat.DOColor(new Color(1, 2, 2, 2), "_EmissionColor", 1).SetLoops(-1, LoopType.Yoyo);
//改变material中的offset属性
mat.DOOffset(new Vector2(1, 1), 1).SetLoops(-1, LoopType.Incremental);
//颜色透明有效果时有作用(控制颜色透明度)
//mat.DOFade(0, 2);
}

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

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