Frame/Assets/Scripts/MVVM/Core/Thread/ThreadJobTest.cs

65 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections;
using System.Threading;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
namespace AssemblyCSharp
{
public class ThreadJobTest:MonoBehaviour
{
public Image greenRect;
public Image pinkRect;
ThreadJob job;
void Awake()
{
job = new ThreadJob(
() => {
Debug.Log("Work Thread :" + Thread.CurrentThread.ManagedThreadId + " work!");
//heavy job
for (int i = 0; i < 12345; i++)
{
for (int j = 0; j < 12345; j++)
{
var z = i + j;
}
}
},
() => {
Debug.Log("Down");
});
}
void Start(){
Debug.Log("Main Thread :"+Thread.CurrentThread.ManagedThreadId+" work!");
StartCoroutine (Move());
}
IEnumerator Move()
{
pinkRect.transform.DOLocalMoveX(250, 1.0f);
yield return new WaitForSeconds(1);
pinkRect.transform.DOLocalMoveY(-150, 2);
yield return new WaitForSeconds(2);
//AI操作陷入深思在异步线程执行,GreenRect不会卡顿
job.Start();
yield return StartCoroutine (job.WaitFor());
pinkRect.transform.DOLocalMoveY(150, 2);
}
void Update()
{
greenRect.transform.Rotate(Vector3.left, Time.deltaTime * 180);
}
}
}