17_DoTween
2019-07-28 本文已影响0人
alphonseLin
委托,Lambda, LinQ串讲
- 什么是委托
- 委托类型是怎么声明出来的
- 泛型委托
- 我们必须自己创建委托类型吗?
- 泛型委托的类型参数推断
*方法与Lambda表达式之间的关系
*如何把一个lambda表达式赋值给一个委托类型变量
*如何把一个Lambda表达式喂给一个委托类型参数
LINQ
委托
using System;
using System.Linq;
namespace Combine{
class Program
{
static void Main{string[] args}{
MyDele dele1=new MyDele(Program.Add);
Student stu=new Student();
//dele1+=stu.SayHello;
dele1+=(new Student()).SayHello;
//dele1+=M1;
dele1();
int res=dele(100,200);
Console.WriteLine(res);
}
static void M1(){
Console.WriteLine("M1 is called!");
}
static int Add(int x, int y){
return x+y;
}
}
class Student{
public void SayHello(){
Console.WriteLine("Hellow, I'm a student");
}
}
delegate void MyDele();
}
泛型委托
- 只需要去刻画一下就好,不需要特意去指明内容
- 解决类膨胀问题
- 可以用var来缩短前面action的指定
···
using System;
using System.Linq;
namespace Combine
{
class Program{
static void Main(string[] args){
MyDele<int>deleAdd=new MyDele<int>(Add);
int res=deleAdd(100,200);
Console.WriteLine(res);
MyDele<double>deleMul=new MyDele<double>(Mul);
double mulRes=deleMul(3.0,4.0);
console.WriteLine(mulRes);
}
static int Add(int x, int y){
return x+y;
}
}
static double Mul(double x, double y){
return x*y;
}
delegate T MyDele<T>(T a, T b);
}
···
- 不需要自己再去声明委托类类型,目前已经包含了两大类委托,action(没有委托类类型的)+function(包含委托类类型的)
using System;
using System.Linq;
namespace Combine
{
class Program{
static void Main(string[] args){
Action action_1=new Action(M1);
action_1();
Action<string>action_2=new Action<string>(SayHello);
action_2("Tim");
Action<string,string> action_3=new Action<string,string>(SayHello_1);
action_3("Tim","Tom");
Action<string,int> action_4=new Action<string,int>(SayHello_2);
action_4("Tim",3);
//var action_4=new Action<string,int>(SayHello_2);
//action_4("Tim",3);
Func<int,int,int>func=new Func<int,int,int>(Add);
int res=func(100,200);
Console.WriteLine(res);
var func=new Func<double, double, double>(Mul);
double res=func(3.0,4.0);
Console.WriteLine(res);
}
static void Add(int x,int y){
return x+y;
}
static void M1(){
Console.WriteLine("M1 is Called");
}
static void SayHello(string name){
Console.WriteLine($"Hello,{name}!")
}
static void SayHello_1(string name1,string name2){
Console.WriteLine($"Hello,{name1}and{name2}!")
}
static void SayHello_2(string name,int round){
for (int i = 0; i < round; i++)
{
Console.WriteLine($"Hello,{name}!");
}
}
}
}
Lambda表达式
- 匿名函数
- inline函数
namespace Combine
{
class Program{
static void Main(string[] args){
//Lambda
//1. 匿名方法
//2. InLine方法,不用声明,直接调用
//老方法_1调用
int res=Add(100,200);
Func<int,int,int>func=new Func<int,int,int>((int a,int b)=>{return a+b;});
//写法照顾1 Func<int,int,int>func=new Func<int,int,int>((a,b)=>{return a+b;});
//写法照顾2 Func<int,int,int>func=(a,b)=>{return a+b;};
int res=func(100,200);
Console.WriteLine(res);
func=new Func<int,int,int>((int x,int y)=>{retun x*y});
//写法照顾1 func=new Func<int,int,int>((x,y)=>{retun x*y});
//写法照顾2 func=(x,y)=>{retun x*y};
res=func(3,4);
Console.WriteLine(res);
}
//老方法_1
static int Add(int a, int b){
return a+b;
}
}
}
泛型委托+Lambda表达式
namespace Combine
{
class Program{
static void Main(string[] args){
DoSomeCalc((a, b)=>{return a*b},100,200);//泛型委托的类型推断
}
static void DoSomeCalc<T>(Func<T,T,T>func,T x,T y){
T res=func(x,y);
Console.WriteLine(res);
}
}
}
LinQ
课程来源:
Timothy Liu
https://www.youtube.com/watch?v=g1EU9gnZKOg&t=2s
DoTween
1
- 解释:
补间动画:又叫做中间帧动画,渐变动画,只要建立起始和结束的画面,中间部分由软件自动生成,省去了中间动画制作的复杂过程,这正是Flash的迷人之处,补间动画是Flash中最常用的动画效果。 - 开发者链接:
DoTween - 通用格式
static DOTween.To(getter, setter, to, float duration)
getter:lambda表达式 ()=> myValue
setter:lambda表达式 (x)=> myValue = x
to:最终值
duration:补间时长
例如:
DOTween.To(()=> myVector, x=> myVector = x, new Vector3(3,4,8), 1);
DOTween.To(()=> myFloat, x=> myFloat = x, 52, 1);
- 测试时内容
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class GetStart : MonoBehaviour {
public Vector3 myValue = new Vector3(0, 0, 0);
public Transform cubeTransform;
public RectTransform taskPanelTransform;
// Use this for initialization
void Start () {
//对变量做动画,通过插值的方式来去修改一个值的变化
DOTween.To(() => myValue, x => myValue = x, new Vector3(0, 0, 0), 2);
}
// Update is called once per frame
void Update () {
//cubeTransform.position = myValue;
//taskPanelTransform.position = myValue;
taskPanelTransform.localPosition = myValue;
}
}
内容测试
2
- 通过Do的方式来移动,比上述方式快
public class myButton : MonoBehaviour {
public RectTransform panelTransform;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void onClick()
{
//让panel transform从当前位置,动画到(0,0,0)的位置,时间为1s,修改局部坐标
//panelTransform.DOMove(new Vector3(0, 0, 0), 1);
panelTransform.DOLocalMove(new Vector3(0, 0, 0), 0.3f);
}
}
3
- 视频的前放与后放
- 视频的set. AutoKill
public RectTransform panelTransform;
private bool isIn = false;
void Start()
{
//让panel transform从当前位置,动画到(0,0,0)的位置,时间为1s,修改局部坐标
//panelTransform.DOMove(new Vector3(0, 0, 0), 1);
//默认动画完成会被销毁
Tweener tweener = panelTransform.DOLocalMove(new Vector3(0, 0, 0), 0.5f);
//Tweener,保存这个动画的信息,每次调用do类型方法,都会创建一个Tweener,这个对象是doTween来管理的
//把自动销毁功能设置为false
tweener.SetAutoKill(false);
tweener.Pause();
}
public void onClick()
{
if (isIn == false)
{
panelTransform.DOPlayForward();//前放
isIn = true;
}
else
{
//让panel离开屏幕
panelTransform.DOPlayBackwards();//倒放
isIn = false;
}
}
4. 动画属性
public class MyPanel : MonoBehaviour {
// Use this for initialization
void Start () {
Tweener tweener = transform.DOLocalMoveX(0, 2);
tweener.SetEase(Ease.InBack);//控制动画曲线
//tweener.SetLoops(2);//重复几次
tweener.OnComplete(OnTweenComplete);//动画结束事件
}
// Update is called once per frame
void Update () {
}
void OnTweenComplete()
{
Debug.Log("动画播放完成了");
}
代码内容
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
using System;
using System.Threading.Tasks;
public class Test001 : MonoBehaviour {
public Gradient _gradient;
public AnimationCurve _curve;
async void Start()
{
//移动
//transform.DOMove(Vector3.one, 2);
//transform.DOLocalMoveX(1, 2);
//transform.DOLocalMove();
//旋转
//transform.DORotate(new Vector3(0,90), 2);
//transform.DOLocalRotate()
//transform.DORotateQuaternion(new Quaternion(0.1f, 0.1f, 0.1f, 0.1f), 2);
//transform.DOLookAt(Vector3.one, 2);//看向1,1,1点
//缩放
//transform.DOScale(Vector3.one * 2, 2);
//Punch
//Punch[0]:带有方向 力的大小
//Punch[1]:持续时间
//Punch[2]:震动次数(频率)
//Pucnh[3]:弹来弹去
//transform.DOPunchPosition(new Vector3(0, 1, 0), 2, 2, 0.5f);
//transform.DOPunchRotation(new Vector3(0, 90, 0), 2, 2, 0.5f);
//transform.DOPunchScale(new Vector3(0, 1, 0) * 2, 2, 2, 0.5f);
//shake
//transform.DOShakePosition(2, Vector3.one, 10, 0,false,true);
//transform.DOShakeRotation();
//transform.DOShakeScale();
//Blend
//混合,因为dotween只会执行最新的一行,同时是一个增量移动
//transform.DOBlendableMoveBy(Vector3.one, 2);
//transform.DOBlendableMoveBy(-Vector3.one*2, 2);
//transform.DOBlendablePunchRotation();
//material
//Material material = GetComponent<MeshRenderer>().material;
//material.SetColor();
//material.DOColor(Color.red, "_Color",2);
//material.DOColor(Color.red, 2);
//material.alpha
//Material material = GetComponent<MeshRenderer>().material;
//material.DOColor(Color.clear, "_TintColor", 2);
//material.DOFade(0, "_TintColor",2);
//material.Gradient
//Material material = GetComponent<MeshRenderer>().material;
//material.DOGradientColor(_gradient, 2);
//material.mapping
//Material material = GetComponent<MeshRenderer>().material;
//material.DOOffset()
//material.DOVector(Color.clear,"_Color",2);
//material.blend
//Material material = GetComponent<MeshRenderer>().material;
//material.DOBlendableColor(Color.red, 2);
//material.DOBlendableColor(Color.green, 2);
//Camera
//Camera[0]:宽/高的比值
//Camera camera = GetComponent<Camera>();
//camera.DOAspect(0.5f,2);
//camera.DOColor(Color.red, 2);
//camera.DONearClipPlane(6.0f, 2);
//camera.DOFieldOfView(20, 2);
//camera.DOOrthoSize(0.8f, 2);
//camera.DOPixelRect(new Rect(0, 0, 512, 384), 2);
//camera.DORect(new Rect(0, 0, 0.5f, 0.5f), 2);
//camera.DOShakePosition(2,10,20);
//Text
//Text text = GetComponent<Text>();
//text.DOColor();
//text.DOFade();
//text.DOBlendableColor();
//text.DOText("sadasdsaaaaaaaaasdasdawdaswfaf",5).SetEase(Ease.Linear);
////Sequence队列, 先添加的先执行
//Sequence quence = DOTween.Sequence();
//quence.Append(transform.DOMove(Vector3.one, 2));//0-2
//quence.Join(transform.DOScale(Vector3.one * 2, 2));
//quence.AppendCallback(AppendCallBack);
//quence.AppendInterval(1);//2-3
//quence.Append(transform.DOMove(new Vector3(1, 0, 0), 2));//3-5
//quence.Join(transform.DOScale(Vector3.one, 2));
////quence.Insert(6, transform.DOMove(-Vector3.one, 2));//会覆盖
////队列预添加, 后添加的会先执行, 需要注意的就是执行顺序的问题
//quence.PrependInterval(1);
//quence.Prepend(transform.DOMove(-Vector3.one * 2, 2));
//quence.PrependCallback(PrependCallback);
////队列部分的回调函数,可以用来卡时机
//quence.InsertCallback(5, InsertCallBack);//按时间插入
//TweenParams para = new TweenParams();
//para.SetLoops();
//transform.DOMove(Vector3.one, 2).SetAs(para);
//transform.DOMove(Vector3.one, 2).SetLoops(-1, LoopType.Yoyo);
//transform.DOMove(Vector3.one, 2).SetLoops(-1, LoopType.Incremental).SetAutoKill(true);//不kill掉会缓存
//from方法补间动画
//transform.DOMove(Vector3.one, 2).From(true);//不kill掉会缓存
//transform.DOMove(Vector3.one, 2).SetDelay(3);//延迟
//transform.DOMove(Vector3.one, 10).SetSpeedBased();//让物体运动以速度为基准, DoMove[0]: 目标点(矢量)DoMove[1]:速度大小
//缓存调用,可以节省性能
//transform.DOMove(Vector3.one, 10).SetId("ID01");
//DOTween.Play("ID01");
//transform.DOMove(Vector3.one, 10).SetRecyclable(true);//设置是否可回收
//transform.DOMove(Vector3.one, 2).SetRelative(true);//是否有相关性,增量运动
//transform.DOMove(Vector3.one, 2).SetUpdate(UpdateType.Manual,true);//可设置帧函数类型
//DOTween.ManualUpdate();
//transform.DOMove(Vector3.one, 2).SetUpdate(UpdateType.Normal,true);//是否忽略unity的timescale影响
//运动曲线
//transform.DOMove(Vector3.one, 2).SetEase(Ease.InBack);//运动曲线的变化
//transform.DOMove(Vector3.one, 2).SetEase(Ease.Flash,10,-1);//运动速度的变化
//结合unity曲线,做运动曲线
//transform.DOMove(Vector3.one, 2).SetEase(EaseFun);
//回调函数
//transform.DOMove(Vector3.one, 2).OnComplete(()=> { Debug.Log("OnComplete"); });
//transform.DOMove(Vector3.one, 2).OnKill(() => { Debug.Log("OnKill"); });
//transform.DOMove(Vector3.one, 2).OnPlay(() => { Debug.Log("OnPlay"); });
//transform.DOMove(Vector3.one, 2).OnPause(() => { Debug.Log("OnPause"); });
//transform.DOMove(Vector3.one, 2).OnStart(() => { Debug.Log("OnStart"); });
//transform.DOMove(Vector3.one, 2).OnStepComplete(() => { Debug.Log("OnStepComplete"); });//每个周期调用一次
//transform.DOMove(Vector3.one, 2).OnUpdate(() => { Debug.Log("OnUpdate"); });
////restart, rewind, doflip, dobackwards
//transform.DOMove(Vector3.one, 2).OnRewind(() => { Debug.Log("OnRewind"); });
//动画的控制方法
//transform.DOMove(Vector3.one, 2);
transform.DOMove(Vector3.one, 2);
await Task.Delay(TimeSpan.FromSeconds(1));
//transform.DOPause();
//transform.DOPlay();
//transform.DORestart();
//transform.DORewind();//倒播
//transform.DOSmoothRewind();
//transform.DOKill();
//transform.DOFlip();//目标点和起始点翻转
//transform.DOGoto(1,true);//按视角跳转到时间点
transform.DOTogglePause();
//transform.DOPlayBackwards();
await Task.Delay(TimeSpan.FromSeconds(0.5f));
//transform.DOPlayForward();
transform.DOTogglePause();//特定环境中调用就好
//获取数据的方法:类方法(静态方法)和实例方法
var list1=DOTween.PausedTweens();
var list2=DOTween.PlayingTweens();
var list3 = DOTween.TweensById("ID",true);
var list4 = DOTween.TweensByTarget(transform,true);//直接拿对象
}
private float EaseFun(float time, float duration, float overshootOrAmplitude, float period)
{
return (time / duration+1);
}
private void InsertCallBack()
{
Debug.Log("InsertCallBack");
}
private void AppendCallBack()
{
Debug.Log("AppendCallBack");
}
private void PrependCallback()
{
Debug.Log("PrependCallback");
}
}