【发布时间】:2019-08-27 01:29:41
【问题描述】:
我对在 Unity 中编写异步内容非常陌生。为异步操作创建进度条非常容易,但是在为异步任务创建进度条时,我什至不确定何时开始:(
public async void GetPhotoFromAndroidAndAddTexture(RawImage mainImage, string path)
{
await AddTextureToImage(mainImage, path);
}
public async Task<Texture2D> GetTextureFromAndroid(string path)
{
if(path != "" && path != " ")
{
#if UNITY_ANDROID
path = "file://" + path;
#endif
UnityWebRequest www = UnityWebRequestTexture.GetTexture(path);
var asyncOperation = www.SendWebRequest();
while(!asyncOperation.isDone)
{
await Task.Delay( 1000/30 );
}
return DownloadHandlerTexture.GetContent(www);
}
else
{
Debug.Log("The path is not correct!");
return null;
}
}
public async Task AddTextureToImage(RawImage mainImage, string path)
{
IProgress<int> progress;
Texture2D photoFromDevice = await GetTextureFromAndroid(path);
float textureH = photoFromDevice.height;
float textureW = photoFromDevice.width;
float aspectRat = textureH > textureW ? textureH/textureW : textureW/textureH;
mainImage.GetComponent<AspectRatioFitter>().aspectRatio = aspectRat;
mainImage.texture = photoFromDevice;
}
当来自 android 的照片被添加为纹理时,我想创建一个进度条。 提前感谢您的任何建议
【问题讨论】:
-
async 总是有多线程的问题......你为什么不使用协程(
IEnumerator),就像通常在examples 中显示的那样?
标签: unity3d asynchronous task progress-bar