当前位置:   article > 正文

Unity插件之DoTween案例分析_dotween 案例

dotween 案例

案例分析

01.Basics

Code:

  1. using UnityEngine;
  2. using System.Collections;
  3. using DG.Tweening;
  4. public class Basics : MonoBehaviour
  5. {
  6. public Transform redCube, greenCube, blueCube, purpleCube;
  7. IEnumerator Start()
  8. {
  9. // Start after one second delay (to ignore Unity hiccups(打嗝) when activating Play mode in Editor)
  10. yield return new WaitForSeconds(1);
  11. // Let's move the red cube TO 0,4,0 in 2 seconds
  12. redCube.DOMove(new Vector3(0,4,0), 2);
  13. // Let's move the green cube FROM 0,4,0 in 2 seconds
  14. greenCube.DOMove(new Vector3(0,4,0), 2).From();
  15. // Let's move the blue cube BY 0,4,0 in 2 seconds 相对位置
  16. blueCube.DOMove(new Vector3(0,4,0), 2).SetRelative();
  17. // Let's move the purple cube BY 6,0,0 in 2 seconds
  18. // and also change its color to yellow.
  19. // To change its color, we'll have to use its material as a target (instead than its transform).
  20. purpleCube.DOMove(new Vector3(6,0,0), 2).SetRelative();
  21. // Also, let's set the color tween to loop infinitely forward and backwards
  22. //在紫色和黄色之间yoyo球
  23. purpleCube.GetComponent<Renderer>().material.DOColor(Color.yellow, 2).SetLoops(-1, LoopType.Yoyo);
  24. }
  25. }

02.Follow

Code:

  1. DragTarget .cs
  2. using UnityEngine;
  3. using System.Collections;
  4. public class DragTarget : MonoBehaviour
  5. {
  6. Transform t;
  7. Camera mainCam;
  8. void Start()
  9. {
  10. t = this.transform;
  11. mainCam = Camera.main;
  12. }
  13. void OnMouseDrag()
  14. {
  15. Vector2 mousePos = Input.mousePosition;
  16. //以相机为起点到物体的位置
  17. float distance = mainCam.WorldToScreenPoint(t.position).z;
  18. //屏幕坐标到世界坐标
  19. t.position = mainCam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, distance));
  20. }
  21. }
  1. Follow.cs
  2. using UnityEngine;
  3. using System.Collections;
  4. using DG.Tweening;
  5. public class Follow : MonoBehaviour
  6. {
  7. public Transform target; // Target to follow
  8. Vector3 targetLastPos;
  9. Tweener tween;
  10. void Start()
  11. {
  12. // First create the "move to target" tween and store it as a Tweener.
  13. // In this case I'm also setting autoKill to FALSE so the tween can go on forever
  14. // (otherwise it will stop executing if it reaches the target)
  15. tween = transform.DOMove(target.position, 2).SetAutoKill(false);
  16. // Store the target's last position, so it can be used to know if it changes
  17. // (to prevent changing the tween if nothing actually changes)
  18. targetLastPos = target.position;
  19. }
  20. void Update()
  21. {
  22. // Use an Update routine to change the tween's endValue each frame
  23. // so that it updates to the target's position if that changed
  24. if (targetLastPos == target.position) return;
  25. // Add a Restart in the end, so that if the tween was completed it will play again
  26. tween.ChangeEndValue(target.position, true).Restart();
  27. targetLastPos = target.position;
  28. }
  29. }

03.Materials

Code: 

  1. Materials.cs
  2. using UnityEngine;
  3. using DG.Tweening;
  4. public class Materials : MonoBehaviour
  5. {
  6. public GameObject target;
  7. public Color toColor;
  8. Tween colorTween, emissionTween, offsetTween;
  9. void Start()
  10. {
  11. // NOTE: all tweens will be created in a paused state, so they can be toggled via the UI
  12. // Store the material, since we will tween that
  13. Material mat = target.GetComponent<Renderer>().material;
  14. // COLOR
  15. colorTween = mat.DOColor(toColor, 1).SetLoops(-1, LoopType.Yoyo).Pause();
  16. // EMISSION
  17. // Note that the float value you see in Unity's inspector, next to the emission's color,
  18. // doesn't really exist in the shader (it's generated by Unity's inspector and applied to the material's color),
  19. // se we have to tween the full _EmissionColor.
  20. emissionTween = mat.DOColor(new Color(0, 0, 0, 0), "_EmissionColor", 1).SetLoops(-1, LoopType.Yoyo).Pause();
  21. // OFFSET
  22. // In this case we set the loop to Incremental and the ease to Linear, because it's cooler
  23. offsetTween = mat.DOOffset(new Vector2(1, 1), 1).SetEase(Ease.Linear).SetLoops(-1, LoopType.Incremental).Pause();
  24. }
  25. // Toggle methods (called by UI events)
  26. public void ToggleColor()
  27. {
  28. colorTween.TogglePause();
  29. }
  30. public void ToggleEmission()
  31. {
  32. emissionTween.TogglePause();
  33. }
  34. public void ToggleOffset()
  35. {
  36. offsetTween.TogglePause();
  37. }
  38. }

04.Paths:过上车功能

Code:

  1. //这是插件里面的枚举类型。
  2. public enum PathType
  3. {
  4. //
  5. // 摘要:
  6. // Linear, composed of straight segments between each waypoint
  7. Linear = 0,
  8. //
  9. // 摘要:
  10. // Curved path (which uses Catmull-Rom curves)
  11. CatmullRom = 1
  12. }
  1. using UnityEngine;
  2. using System.Collections;
  3. using DG.Tweening;
  4. public class Paths : MonoBehaviour
  5. {
  6. public Transform target;
  7. public PathType pathType = PathType.CatmullRom;
  8. public Vector3[] waypoints = new[] {
  9. new Vector3(4, 2, 6),
  10. new Vector3(8, 6, 14),
  11. new Vector3(4, 6, 14),
  12. new Vector3(0, 6, 6),
  13. new Vector3(-3, 0, 0)
  14. };
  15. void Start()
  16. {
  17. // Create a path tween using the given pathType, Linear or CatmullRom (curved).
  18. // Use SetOptions to close the path 设为true,则最后一点和第一点重合
  19. // and SetLookAt to make the target orient to the path itself
  20. //上一句为使目标朝向路径本身
  21. Tween t = target.DOPath(waypoints, 4, pathType)
  22. .SetOptions(true)
  23. .SetLookAt(0.001f);
  24. // Then set the ease to Linear and use infinite loops
  25. t.SetEase(Ease.Linear).SetLoops(-1);
  26. }
  27. }

05.Sequences:悠悠球

  1. using UnityEngine;
  2. using System.Collections;
  3. using DG.Tweening;
  4. public class Sequences : MonoBehaviour
  5. {
  6. public Transform cube;
  7. public float duration = 4;
  8. IEnumerator Start()
  9. {
  10. // Start after one second delay (to ignore Unity hiccups when activating Play mode in Editor)
  11. yield return new WaitForSeconds(1);
  12. // Create a new Sequence.
  13. // We will set it so that the whole duration is 6
  14. Sequence s = DOTween.Sequence();
  15. // Add an horizontal relative move tween that will last the whole Sequence's duration
  16. s.Append(cube.DOMoveX(6, duration).SetRelative().SetEase(Ease.InOutQuad));
  17. // Insert a rotation tween which will last half the duration
  18. // and will loop forward and backward twice
  19. s.Insert(0, cube.DORotate(new Vector3(0, 45, 0), duration / 2).SetEase(Ease.InQuad).SetLoops(2, LoopType.Yoyo));
  20. // Add a color tween that will start at half the duration and last until the end
  21. s.Insert(duration / 2, cube.GetComponent<Renderer>().material.DOColor(Color.yellow, duration / 2));
  22. // Set the whole Sequence to loop infinitely forward and backwards
  23. s.SetLoops(-1, LoopType.Yoyo);
  24. }
  25. }

06.UGUI,UI特效

  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using DG.Tweening;
  5. public class UGUI : MonoBehaviour
  6. {
  7. public Image dotweenLogo, circleOutline;
  8. public Text text, relativeText, scrambledText;
  9. public Slider slider;
  10. void Start()
  11. {
  12. // All tweens are created in a paused state (by chaining to them a final Pause()),
  13. // so that the UI Play button can activate them when pressed.
  14. // Also, the ones that don't loop infinitely have the AutoKill property set to FALSE,
  15. // so they won't be destroyed when complete and can be resued by the RESTART button
  16. // Animate the fade out of DOTween's logo
  17. dotweenLogo.DOFade(0, 1.5f).SetAutoKill(false).Pause();
  18. // Animate the circle outline's color and fillAmount
  19. circleOutline.DOColor(RandomColor(), 1.5f).SetEase(Ease.Linear).Pause();
  20. circleOutline.DOFillAmount(0, 1.5f).SetEase(Ease.Linear).SetLoops(-1, LoopType.Yoyo)
  21. .OnStepComplete(()=> {
  22. circleOutline.fillClockwise = !circleOutline.fillClockwise;
  23. circleOutline.DOColor(RandomColor(), 1.5f).SetEase(Ease.Linear);
  24. })
  25. .Pause();
  26. // Animate the first text...
  27. text.DOText("This text will replace the existing one", 2).SetEase(Ease.Linear).SetAutoKill(false).Pause();
  28. // Animate the second (relative) text...
  29. relativeText.DOText(" - This text will be added to the existing one", 2).SetRelative().SetEase(Ease.Linear).SetAutoKill(false).Pause();
  30. // Animate the third (scrambled) text...
  31. scrambledText.DOText("This text will appear from scrambled chars", 2, true, ScrambleMode.All).SetEase(Ease.Linear).SetAutoKill(false).Pause();
  32. // Animate the slider
  33. slider.DOValue(1, 1.5f).SetEase(Ease.InOutQuad).SetLoops(-1, LoopType.Yoyo).Pause();
  34. }
  35. // Called by PLAY button OnClick event. Starts all tweens
  36. public void StartTweens()
  37. {
  38. DOTween.PlayAll();
  39. // 开始全部
  40. }
  41. // Called by RESTART button OnClick event. Restarts all tweens
  42. public void RestartTweens()
  43. {
  44. DOTween.RestartAll();
  45. // 重启全部
  46. }
  47. // Returns a random color
  48. Color RandomColor()
  49. {
  50. return new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 1);
  51. }
  52. }

 

 

 

==》》添加:拖动物体的C#方法:

Drag:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Drag : MonoBehaviour {
  5. Transform t;
  6. Camera mainCam;
  7. Vector3 offset;
  8. void Start () {
  9. t = this.transform;
  10. mainCam = Camera.main;
  11. }
  12. private void OnMouseDown()
  13. {
  14. Vector2 mousPos = Input.mousePosition;
  15. float distance = mainCam.WorldToScreenPoint(t.transform.position).z;
  16. Vector3 worldPos = mainCam.ScreenToWorldPoint(new Vector3(mousPos.x, mousPos.y, distance));
  17. offset = t.position - worldPos;
  18. }
  19. private void OnMouseDrag()
  20. {
  21. Vector2 mousPos = Input.mousePosition;
  22. float distance = mainCam.WorldToScreenPoint(t.transform.position).z;
  23. t.position = mainCam.ScreenToWorldPoint(new Vector3(mousPos.x, mousPos.y, distance)) + offset;
  24. }
  25. }

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/125906?site
推荐阅读
相关标签
  

闽ICP备14008679号