diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 25f231224..c5b64334e 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -10,10 +10,6 @@ using UniJSON; #if UNITY_EDITOR using UnityEditor; #endif -#if ((NET_4_6 || NET_STANDARD_2_0) && UNITY_2017_1_OR_NEWER) -using System.Threading.Tasks; -#endif - namespace UniGLTF { @@ -351,44 +347,6 @@ namespace UniGLTF } } } - // for (int i = 0; i < GLTF.meshes.Count; ++i) - // { - // var mesh = GLTF.meshes[i]; - // try - // { - // for (int j = 0; j < mesh.primitives.Count; ++j) - // { - // var primitive = mesh.primitives[j]; - // for (int k = 0; k < primitive.targets.Count; ++k) - // { - // var extraName = parsed["meshes"][i]["primitives"][j]["targets"][k]["extra"]["name"].Value.GetString(); - // //Debug.LogFormat("restore morphName: {0}", extraName); - // throw new NotImplementedException(); - // // primitive.extras.targetNames.Add(extraName); - // } - // } - // } - // catch (Exception) - // { - // // do nothing - // } - // } -#if false - for (int i = 0; i < GLTF.nodes.Count; ++i) - { - var node = GLTF.nodes[i]; - try - { - var extra = parsed["nodes"][i]["extra"]["skinRootBone"].AsInt; - //Debug.LogFormat("restore extra: {0}", extra); - //node.extras.skinRootBone = extra; - } - catch (Exception) - { - // do nothing - } - } -#endif } #endregion @@ -396,106 +354,7 @@ namespace UniGLTF public bool EnableLoadBalancing; - /// - /// ReadAllBytes, Parse, Create GameObject - /// - /// allbytes - public void Load(string path) - { - var bytes = File.ReadAllBytes(path); - Load(path, bytes); - } - - /// - /// Parse, Create GameObject - /// - /// gltf or glb path - /// allbytes - public void Load(string path, byte[] bytes) - { - Parse(path, bytes); - Load(); - Root.name = Path.GetFileNameWithoutExtension(path); - } - - /// - /// Build unity objects from parsed gltf - /// - public void Load() - { - var schedulable = LoadAsync(); - schedulable.ExecuteAll(); - } - - [Obsolete("Action to Action")] - public IEnumerator LoadCoroutine(Action onLoaded, Action onError = null) - { - return LoadCoroutine(() => onLoaded(Unit.Default), onError); - } - - public IEnumerator LoadCoroutine(Action onError = null) - { - return LoadCoroutine(() => { }, onError); - } - - public IEnumerator LoadCoroutine(Action onLoaded, Action onError = null) - { - if (onLoaded == null) - { - onLoaded = () => { }; - } - - if (onError == null) - { - onError = Debug.LogError; - } - - var schedulable = LoadAsync(); - foreach (var x in schedulable.GetRoot().Traverse()) - { - while (true) - { - var status = x.Execute(); - if (status != ExecutionStatus.Continue) - { - break; - } - yield return null; - } - } - - onLoaded(); - } - - [Obsolete("Action to Action")] - public void LoadAsync(Action onLoaded, Action onError = null) - { - LoadAsync(() => onLoaded(Unit.Default), onError); - } - - public void LoadAsync(Action onLoaded, Action onError = null) - { - if (onError == null) - { - onError = Debug.LogError; - } - - LoadAsync() - .Subscribe(Scheduler.MainThread, - _ => onLoaded(), - onError - ); - } - -#if ((NET_4_6 || NET_STANDARD_2_0) && UNITY_2017_1_OR_NEWER && !UNITY_WEBGL) - public async Task LoadAsyncTask() - { - await LoadAsync().ToTask(); - return Root; - } -#endif - - protected virtual Schedulable LoadAsync() + public virtual Schedulable LoadAsync() { return Schedulable.Create() @@ -667,7 +526,6 @@ namespace UniGLTF public GameObject Root; public List Nodes = new List(); - public List Meshes = new List(); public void ShowMeshes() { @@ -844,12 +702,6 @@ namespace UniGLTF } } - [Obsolete("Use ExtractImages(prefabPath)")] - public void ExtranctImages(UnityPath prefabPath) - { - ExtractImages(prefabPath); - } - /// /// Extract images from glb or gltf out of Assets folder. /// diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextExtensions.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextExtensions.cs new file mode 100644 index 000000000..c7526278e --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextExtensions.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections; +using System.IO; +using UnityEngine; +using DepthFirstScheduler; +using System.Threading.Tasks; + +namespace UniGLTF +{ + public static class ImporterContextExtensions + { + /// + /// ReadAllBytes, Parse, Create GameObject + /// + /// allbytes + public static void Load(this ImporterContext self, string path) + { + var bytes = File.ReadAllBytes(path); + self.Load(path, bytes); + } + + /// + /// Parse, Create GameObject + /// + /// gltf or glb path + /// allbytes + public static void Load(this ImporterContext self, string path, byte[] bytes) + { + self.Parse(path, bytes); + self.Load(); + self.Root.name = Path.GetFileNameWithoutExtension(path); + } + + /// + /// Build unity objects from parsed gltf + /// + public static void Load(this ImporterContext self) + { + var schedulable = self.LoadAsync(); + schedulable.ExecuteAll(); + } + + public static IEnumerator LoadCoroutine(this ImporterContext self, Action onError = null) + { + return self.LoadCoroutine(() => { }, onError); + } + + public static IEnumerator LoadCoroutine(this ImporterContext self, Action onLoaded, Action onError = null) + { + if (onLoaded == null) + { + onLoaded = () => { }; + } + + if (onError == null) + { + onError = Debug.LogError; + } + + var schedulable = self.LoadAsync(); + foreach (var x in schedulable.GetRoot().Traverse()) + { + while (true) + { + var status = x.Execute(); + if (status != ExecutionStatus.Continue) + { + break; + } + yield return null; + } + } + + onLoaded(); + } + + public static void LoadAsync(this ImporterContext self, Action onLoaded, Action onError = null) + { + if (onError == null) + { + onError = Debug.LogError; + } + + self.LoadAsync() + .Subscribe(Scheduler.MainThread, + _ => onLoaded(), + onError + ); + } + +#if ((NET_4_6 || NET_STANDARD_2_0) && UNITY_2017_1_OR_NEWER && !UNITY_WEBGL) + public static async Task LoadAsyncTask(this ImporterContext self) + { + await self.LoadAsync().ToTask(); + return self.Root; + } +#endif + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextExtensions.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextExtensions.cs.meta new file mode 100644 index 000000000..1fab29de6 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c4f44b8c475a43a469d9d57786857eaa +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM.Samples/Scripts/VRMRuntimeLoader.cs b/Assets/VRM.Samples/Scripts/VRMRuntimeLoader.cs index 4b6d85804..e31c3ab0e 100644 --- a/Assets/VRM.Samples/Scripts/VRMRuntimeLoader.cs +++ b/Assets/VRM.Samples/Scripts/VRMRuntimeLoader.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Runtime.InteropServices; +using UniGLTF; using UnityEngine; diff --git a/Assets/VRM.Samples/Scripts/ViewerUI.cs b/Assets/VRM.Samples/Scripts/ViewerUI.cs index 9c1e71288..72e0137bb 100644 --- a/Assets/VRM.Samples/Scripts/ViewerUI.cs +++ b/Assets/VRM.Samples/Scripts/ViewerUI.cs @@ -2,6 +2,7 @@ using System.IO; using System.Linq; using System.Threading.Tasks; +using UniGLTF; using UniHumanoid; using UnityEngine; using UnityEngine.UI; diff --git a/Assets/VRM10.Samples/Runtime/ViewerUI.cs b/Assets/VRM10.Samples/Runtime/ViewerUI.cs index 96406053f..b23a42874 100644 --- a/Assets/VRM10.Samples/Runtime/ViewerUI.cs +++ b/Assets/VRM10.Samples/Runtime/ViewerUI.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Linq; +using UniGLTF; using UniHumanoid; using UnityEngine; using UnityEngine.UI;