本文主要是介绍异步编程小案例-FlayCoins,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
介绍
使用Dotween和UniTask 结合的方式实现金币飞入指定位置,这中间会用到UniTask和Dotween相关的知识点。
准备工作
- 最需要准备的无非就是导入 UniTask 我在上一篇已经说过了,这里还需要导入 Dotween,直接使用免费版本的即可。免费版本的Dotween可以直接在Unity Asset Store 里面下载导入。
- 场景的搭建也很简单结构如下图
- 制作一个简单的预制体金币的
脚本编写
这里有有一个脚本啊直接挂载在CoinCollection 上面去。
using Cysharp.Threading.Tasks;
using DG.Tweening;
using System;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;
// 注意如果没有添加好 宏 这里是无法在Dotween上使用UniTask的public class UniTaskTargetMove : MonoBehaviour
{public GameObject coin;public Transform coinParent;[SerializeField,Tooltip("金币生成的位置")] private Transform _locationPosition;[SerializeField,Tooltip("金币最终要飞到的位置")] private Transform _endPosition;[SerializeField] private Text _coinText;[Header("金币随机生成的范围")][SerializeField] private int _minX;[SerializeField] private int _maxX;[SerializeField] private int _minY;[SerializeField] private int _maxY;[SerializeField,Tooltip("金币生成的数量")] private int _coinAmount;[SerializeField,Tooltip("金币移动的时间")] private float _duration;private List<GameObject> coins = new List<GameObject>();private Tween tweenCoins;private int coinCount;CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();private void Start(){// CollectCoins();}[Button("cioinCollections")]private async void CollectCoins() {cancellationTokenSource.Cancel();SetCoins(0);for (int i = coins.Count-1; i >= 0; i--) {Destroy(coins[i]);}coins.Clear();List<UniTask> uniTasks = new List<UniTask>();for (int i = 0; i < _coinAmount; i++){var tempCoinPos = Instantiate(coin, coinParent);var xPosition = _locationPosition.position.x + UnityEngine.Random.Range(_minX, _maxX);var yPosition = _locationPosition.position.y + UnityEngine.Random.Range(_minY, _maxY);tempCoinPos.transform.position = new Vector3(xPosition, yPosition);uniTasks.Add (tempCoinPos.transform.DOPunchPosition(new Vector3(0,30,0),UnityEngine.Random.Range(0,1f)).SetEase(Ease.InOutElastic).ToUniTask());coins.Add(tempCoinPos);await UniTask.Delay(TimeSpan.FromSeconds(0.01f));}//await UniTask.Delay(TimeSpan.FromSeconds(1f));await UniTask.WhenAll(uniTasks);await moveCoinsTask();}private async UniTask moveCoinsTask() {List<UniTask> movecoinTaskList = new List<UniTask>();for (int i = coins.Count-1; i >= 0; i--){movecoinTaskList.Add(MoveCoinTask(coins[i]));await UniTask.Delay(TimeSpan.FromSeconds(0.05f)); // 等待0.05f}}/// <summary>/// 更新金币的数量/// </summary>/// <param name="value"></param>private void SetCoins(int value) {coinCount = value;_coinText.text = coinCount.ToString();}/// <summary>/// 移动金币/// </summary>/// <param name="coinInstance"></param>/// <returns></returns>private async UniTask MoveCoinTask(GameObject coinInstance) {await coinInstance.transform.DOMove(_endPosition.position, _duration).SetEase(Ease.InBack).ToUniTask();var temp = coinInstance;coins.Remove(coinInstance);Destroy(temp);await ReactToCollectionCoin();SetCoins(coinCount + 1);}/// <summary>/// 当金币到达endPosition后面的时候会 变大变小/// </summary>/// <returns></returns>private async UniTask ReactToCollectionCoin() {if (tweenCoins == null) {tweenCoins = _endPosition.DOPunchScale(new Vector3(1.5f, 1.5f, 1.5f), 0.1f).SetEase(Ease.InOutElastic);await tweenCoins.ToUniTask();tweenCoins = null;}}
}
这个效果用到了很多UniTask和DoTween相关的知识,这个也主要是为了更好的学习如何使用UniTask。
这篇关于异步编程小案例-FlayCoins的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!