From 6f3d3e3f2b5823b0e57056f17b8e3d0a2cecbcf5 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 12 Feb 2021 21:48:47 +0900 Subject: [PATCH] fix deadlock --- .../Runtime/Extensions/TaskExtensions.cs | 21 +++++++++++++++++++ .../Runtime/Extensions/TaskExtensions.cs.meta | 11 ++++++++++ .../MaterialFacotry/MaterialFactory.cs | 1 + .../TextureLoader/GltfTextureLoader.cs | 10 ++++----- .../UnityWebRequestTextureLoader.cs | 12 ++--------- 5 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 Assets/UniGLTF/Runtime/Extensions/TaskExtensions.cs create mode 100644 Assets/UniGLTF/Runtime/Extensions/TaskExtensions.cs.meta diff --git a/Assets/UniGLTF/Runtime/Extensions/TaskExtensions.cs b/Assets/UniGLTF/Runtime/Extensions/TaskExtensions.cs new file mode 100644 index 000000000..4326925c3 --- /dev/null +++ b/Assets/UniGLTF/Runtime/Extensions/TaskExtensions.cs @@ -0,0 +1,21 @@ +using System.Collections; +using System.Threading.Tasks; + +namespace UniGLTF +{ + public static class TaskExtensions + { + public static IEnumerator AsIEnumerator(this Task task) + { + while (!task.IsCompleted) + { + yield return null; + } + + if (task.IsFaulted) + { + throw task.Exception; + } + } + } +} diff --git a/Assets/UniGLTF/Runtime/Extensions/TaskExtensions.cs.meta b/Assets/UniGLTF/Runtime/Extensions/TaskExtensions.cs.meta new file mode 100644 index 000000000..b42a81728 --- /dev/null +++ b/Assets/UniGLTF/Runtime/Extensions/TaskExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 54481e2d4da20ec4fa15376518f2e938 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs index 060143ef7..2ceb79a20 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs @@ -145,6 +145,7 @@ namespace UniGLTF if (m_gltf.materials == null || m_gltf.materials.Count == 0) { var task = CreateMaterialAsync(m_gltf, 0, GetTextureAsync); + while (!task.IsCompleted) { yield return null; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs index 50eb5c3de..b557fb49c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/GltfTextureLoader.cs @@ -27,12 +27,10 @@ namespace UniGLTF public static async Task LoadTextureAsync(glTF gltf, IStorage storage, int index) { string m_textureName = default; - var imageBytes = await Task.Run(() => - { - var imageIndex = gltf.GetImageIndexFromTextureIndex(index); - var segments = gltf.GetImageBytes(storage, imageIndex, out m_textureName); - return ToArray(segments); - }); + + var imageIndex = gltf.GetImageIndexFromTextureIndex(index); + var segments = gltf.GetImageBytes(storage, imageIndex, out m_textureName); + var imageBytes = ToArray(segments); // // texture from image(png etc) bytes diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs index 226565715..347fa074e 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/TextureLoader/UnityWebRequestTextureLoader.cs @@ -55,16 +55,8 @@ namespace UniGLTF public IEnumerator ProcessOnMainThread(glTF gltf, IStorage storage, bool isLinear, glTFTextureSampler sampler) { - ArraySegment bytes = default; - var task = Task.Run(() => - { - var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex); - bytes = gltf.GetImageBytes(storage, imageIndex, out m_textureName); - }); - while (!task.IsCompleted) - { - yield return null; - } + var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex); + var bytes = gltf.GetImageBytes(storage, imageIndex, out m_textureName); // tmp file var tmp = Path.GetTempFileName();