add unity multithreading project
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Animations
|
||||
{
|
||||
public class AsyncTween : MonoBehaviour
|
||||
{
|
||||
private void Start()
|
||||
{
|
||||
Animate();
|
||||
}
|
||||
|
||||
private async UniTask Animate()
|
||||
{
|
||||
float time = 0.0f;
|
||||
|
||||
// move
|
||||
while (time < 1.0f)
|
||||
{
|
||||
transform.position = Vector3.Lerp(Vector3.zero, Vector3.forward * 5.0f, time);
|
||||
// await Task.Delay(100);
|
||||
await UniTask.DelayFrame(1);
|
||||
time += Time.deltaTime;
|
||||
}
|
||||
|
||||
// scale
|
||||
time = 0.0f;
|
||||
while (time < 1.0f)
|
||||
{
|
||||
transform.localScale = Vector3.Lerp(Vector3.one, Vector3.one * 2.0f, time);
|
||||
// await Task.Delay(100);
|
||||
await UniTask.DelayFrame(1);
|
||||
time += Time.deltaTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 264a3a466b7247cea8465e4b3fdcdd75
|
||||
timeCreated: 1751902541
|
||||
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Animations
|
||||
{
|
||||
public class CoRoutineTween : MonoBehaviour
|
||||
{
|
||||
private async void Start()
|
||||
{
|
||||
// StartCoroutine(Animate());
|
||||
await Animate();
|
||||
}
|
||||
|
||||
private IEnumerator Animate()
|
||||
{
|
||||
float time = 0.0f;
|
||||
|
||||
// move
|
||||
while (time < 1.0f)
|
||||
{
|
||||
transform.position = Vector3.Lerp(Vector3.zero, Vector3.forward * 5.0f, time);
|
||||
yield return null;
|
||||
time += Time.deltaTime;
|
||||
}
|
||||
|
||||
// scale
|
||||
time = 0.0f;
|
||||
while (time < 1.0f)
|
||||
{
|
||||
transform.localScale = Vector3.Lerp(Vector3.one, Vector3.one * 2.0f, time);
|
||||
yield return null;
|
||||
time += Time.deltaTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e35c5f2a0fdf454ea3fc6ef4543323da
|
||||
timeCreated: 1751901739
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using PrimeTween;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Animations
|
||||
{
|
||||
public class PrimeTween : MonoBehaviour
|
||||
{
|
||||
private void Start()
|
||||
{
|
||||
Tween.PositionZ(transform, 5.0f, 1.0f).Chain(
|
||||
Tween.Scale(transform, Vector3.one * 2.0f, 1.0f));
|
||||
|
||||
// await Tween.PositionZ(transform, 5.0f, 1.0f);
|
||||
// Tween.Scale(transform, Vector3.one * 2.0f, 1.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e4d4225d496a4f5d89404f0ec4f33c90
|
||||
timeCreated: 1751903903
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Threading;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Animations
|
||||
{
|
||||
public class ThreadAnimation : MonoBehaviour
|
||||
{
|
||||
private void Start()
|
||||
{
|
||||
// CAUTION: you can not access Unity Objects from a thread which is not the main thread, e.g. transforms
|
||||
// WARNING: never ever put the main thread to sleep
|
||||
// Thread thread = new Thread(() =>
|
||||
// {
|
||||
float time = 0.0f;
|
||||
|
||||
// move
|
||||
while (time < 1.0f)
|
||||
{
|
||||
transform.position = Vector3.Lerp(Vector3.zero, Vector3.forward * 5.0f, time);
|
||||
Thread.Sleep(100);
|
||||
time += 0.1f;
|
||||
}
|
||||
|
||||
// scale
|
||||
time = 0.0f;
|
||||
while (time < 1.0f)
|
||||
{
|
||||
transform.localScale = Vector3.Lerp(Vector3.one, Vector3.one * 2.0f, time);
|
||||
Thread.Sleep(100);
|
||||
time += 0.1f;
|
||||
}
|
||||
// });
|
||||
// thread.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d7132ac922e04f09a4bda87ad5e35722
|
||||
timeCreated: 1751901998
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class UpdateTween : MonoBehaviour
|
||||
{
|
||||
private bool _isMoving = true;
|
||||
private bool _isAnimating = true;
|
||||
private float _time = 0.0f;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_isAnimating)
|
||||
{
|
||||
if (_isMoving)
|
||||
{
|
||||
// move
|
||||
// transform.position += Vector3.forward * Time.deltaTime;
|
||||
// transform.position = Vector3.Lerp(Vector3.zero, Vector3.forward * 5.0f, Time.deltaTime);
|
||||
transform.position = Vector3.Lerp(Vector3.zero, Vector3.forward * 5.0f, _time);
|
||||
_time += Time.deltaTime;
|
||||
|
||||
if (_time >= 1.0f)
|
||||
{
|
||||
_time = 0.0f;
|
||||
_isMoving = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// scale
|
||||
transform.localScale = Vector3.Lerp(Vector3.one, Vector3.one * 2.0f, _time);
|
||||
_time += Time.deltaTime;
|
||||
|
||||
if (_time >= 1.0f)
|
||||
{
|
||||
_time = 0.0f;
|
||||
_isAnimating = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8140663adbc534c18853396e6a7924e2
|
||||
Reference in New Issue
Block a user