-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomGltfImport.cs
104 lines (88 loc) · 3.06 KB
/
CustomGltfImport.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// SPDX-FileCopyrightText: 2023 Unity Technologies and the glTFast authors
// SPDX-License-Identifier: Apache-2.0
#if NEWTONSOFT_JSON
namespace GLTFast.Documentation.Examples
{
#region CustomGltfImport
using GLTFast;
using Addons;
using System;
using UnityEngine;
using GltfImport = GLTFast.Newtonsoft.GltfImport;
class CustomGltfImport : MonoBehaviour
{
// Path to the gltf asset to be imported
public string Uri;
async void Start()
{
try
{
ImportAddonRegistry.RegisterImportAddon(new MyAddon());
var gltfImport = new GltfImport();
await gltfImport.Load(Uri);
await gltfImport.InstantiateMainSceneAsync(transform);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
public class MyAddon : ImportAddon<MyAddonInstance> { }
public class MyAddonInstance : ImportAddonInstance
{
GltfImport m_GltfImport;
public override void Dispose() { }
public override void Inject(GltfImportBase gltfImport)
{
var newtonsoftGltfImport = gltfImport as GltfImport;
if (newtonsoftGltfImport == null)
return;
m_GltfImport = newtonsoftGltfImport;
newtonsoftGltfImport.AddImportAddonInstance(this);
}
public override void Inject(IInstantiator instantiator)
{
var goInstantiator = instantiator as GameObjectInstantiator;
if (goInstantiator == null)
return;
var _ = new MyInstantiatorAddon(m_GltfImport, goInstantiator);
}
public override bool SupportsGltfExtension(string extensionName)
{
return false;
}
}
}
class MyInstantiatorAddon
{
GltfImport m_GltfImport;
GameObjectInstantiator m_Instantiator;
public MyInstantiatorAddon(GltfImport gltfImport, GameObjectInstantiator instantiator)
{
m_GltfImport = gltfImport;
m_Instantiator = instantiator;
m_Instantiator.NodeCreated += OnNodeCreated;
m_Instantiator.EndSceneCompleted += () =>
{
m_Instantiator.NodeCreated -= OnNodeCreated;
};
}
void OnNodeCreated(uint nodeIndex, GameObject gameObject)
{
// De-serialize glTF JSON
var gltf = m_GltfImport.GetSourceRoot();
var node = gltf.Nodes[(int)nodeIndex] as GLTFast.Newtonsoft.Schema.Node;
var extras = node?.extras;
if (extras == null)
return;
// Access values in the extras property
if (extras.TryGetValue("some-extra-key", out string extraValue))
{
var component = gameObject.AddComponent<ExtraData>();
component.someExtraKey = extraValue;
}
}
}
#endregion
}
#endif