ImporterContextExtensions.cs

This commit is contained in:
ousttrue 2021-02-16 19:38:22 +09:00
parent 0f513f3341
commit 2546535118
6 changed files with 114 additions and 149 deletions

View File

@ -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;
/// <summary>
/// ReadAllBytes, Parse, Create GameObject
/// </summary>
/// <param name="path">allbytes</param>
public void Load(string path)
{
var bytes = File.ReadAllBytes(path);
Load(path, bytes);
}
/// <summary>
/// Parse, Create GameObject
/// </summary>
/// <param name="path">gltf or glb path</param>
/// <param name="bytes">allbytes</param>
public void Load(string path, byte[] bytes)
{
Parse(path, bytes);
Load();
Root.name = Path.GetFileNameWithoutExtension(path);
}
/// <summary>
/// Build unity objects from parsed gltf
/// </summary>
public void Load()
{
var schedulable = LoadAsync();
schedulable.ExecuteAll();
}
[Obsolete("Action<Unit> to Action")]
public IEnumerator LoadCoroutine(Action<Unit> onLoaded, Action<Exception> onError = null)
{
return LoadCoroutine(() => onLoaded(Unit.Default), onError);
}
public IEnumerator LoadCoroutine(Action<Exception> onError = null)
{
return LoadCoroutine(() => { }, onError);
}
public IEnumerator LoadCoroutine(Action onLoaded, Action<Exception> 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<Unit> to Action")]
public void LoadAsync(Action<Unit> onLoaded, Action<Exception> onError = null)
{
LoadAsync(() => onLoaded(Unit.Default), onError);
}
public void LoadAsync(Action onLoaded, Action<Exception> 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<GameObject> LoadAsyncTask()
{
await LoadAsync().ToTask();
return Root;
}
#endif
protected virtual Schedulable<Unit> LoadAsync()
public virtual Schedulable<Unit> LoadAsync()
{
return
Schedulable.Create()
@ -667,7 +526,6 @@ namespace UniGLTF
public GameObject Root;
public List<Transform> Nodes = new List<Transform>();
public List<MeshWithMaterials> Meshes = new List<MeshWithMaterials>();
public void ShowMeshes()
{
@ -844,12 +702,6 @@ namespace UniGLTF
}
}
[Obsolete("Use ExtractImages(prefabPath)")]
public void ExtranctImages(UnityPath prefabPath)
{
ExtractImages(prefabPath);
}
/// <summary>
/// Extract images from glb or gltf out of Assets folder.
/// </summary>

View File

@ -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
{
/// <summary>
/// ReadAllBytes, Parse, Create GameObject
/// </summary>
/// <param name="path">allbytes</param>
public static void Load(this ImporterContext self, string path)
{
var bytes = File.ReadAllBytes(path);
self.Load(path, bytes);
}
/// <summary>
/// Parse, Create GameObject
/// </summary>
/// <param name="path">gltf or glb path</param>
/// <param name="bytes">allbytes</param>
public static void Load(this ImporterContext self, string path, byte[] bytes)
{
self.Parse(path, bytes);
self.Load();
self.Root.name = Path.GetFileNameWithoutExtension(path);
}
/// <summary>
/// Build unity objects from parsed gltf
/// </summary>
public static void Load(this ImporterContext self)
{
var schedulable = self.LoadAsync();
schedulable.ExecuteAll();
}
public static IEnumerator LoadCoroutine(this ImporterContext self, Action<Exception> onError = null)
{
return self.LoadCoroutine(() => { }, onError);
}
public static IEnumerator LoadCoroutine(this ImporterContext self, Action onLoaded, Action<Exception> 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<Exception> 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<GameObject> LoadAsyncTask(this ImporterContext self)
{
await self.LoadAsync().ToTask();
return self.Root;
}
#endif
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c4f44b8c475a43a469d9d57786857eaa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2,6 +2,7 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using UniGLTF;
using UnityEngine;

View File

@ -2,6 +2,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using UniGLTF;
using UniHumanoid;
using UnityEngine;
using UnityEngine.UI;

View File

@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Linq;
using UniGLTF;
using UniHumanoid;
using UnityEngine;
using UnityEngine.UI;