VRMAssetWriter.cs
6.74 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#if false
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UniGLTF;
using UnityEditor;
using UnityEngine;
namespace VRM
{
public static class VRMAssetWriter
{
interface ISubAssetWriter
{
void WriteIfHas(GameObject go);
}
class SubAssetWriter<T>: ISubAssetWriter where T : UnityEngine.Object
{
HashSet<T> m_set = new HashSet<T>();
String m_assetPath;
public delegate IEnumerable<T> GetterFunc(GameObject go);
GetterFunc m_getter;
public SubAssetWriter(string assetPath, GetterFunc getter)
{
m_assetPath = assetPath;
m_getter = getter;
}
public void WriteIfHas(GameObject go)
{
foreach(var o in m_getter(go))
{
if (!m_set.Contains(o))
{
AssetDatabase.AddObjectToAsset(o, m_assetPath);
m_set.Add(o);
}
}
}
}
static IEnumerable<Mesh> GetMeshs(GameObject go)
{
var skinnedMesh = go.GetComponent<SkinnedMeshRenderer>();
if (skinnedMesh != null)
{
yield return skinnedMesh.sharedMesh;
}
var filter = go.GetComponent<MeshFilter>();
if (filter != null)
{
yield return filter.sharedMesh;
}
}
static IEnumerable<Material> GetMaterials(GameObject go)
{
var renderer = go.GetComponent<Renderer>();
if (renderer != null)
{
return renderer.sharedMaterials;
}
else
{
return Enumerable.Empty<Material>();
}
}
static IEnumerable<Texture2D> GetTextures(GameObject go)
{
foreach (var m in GetMaterials(go))
{
foreach(Texture2D x in m.GetTextures())
{
if (x != null)
{
yield return x;
}
}
}
}
static IEnumerable<Avatar> GetAvatars(GameObject go)
{
var animator = go.GetComponent<Animator>();
if(animator!=null && animator.avatar != null)
{
yield return animator.avatar;
}
}
static IEnumerable<BlendShapeClip> GetBlendShapeClips(GameObject go)
{
var proxy = go.GetComponent<VRMBlendShapeProxy>();
if (proxy != null && proxy.BlendShapeAvatar != null)
{
return proxy.BlendShapeAvatar.Clips;
}
else
{
return Enumerable.Empty<BlendShapeClip>();
}
}
static IEnumerable<BlendShapeAvatar> GetBlendShapeAvatars(GameObject go)
{
var proxy = go.GetComponent<VRMBlendShapeProxy>();
if (proxy != null && proxy.BlendShapeAvatar != null)
{
yield return proxy.BlendShapeAvatar;
}
}
static IEnumerable<UniHumanoid.AvatarDescription> GetAvatarDecriptions(GameObject go)
{
var humanoid = go.GetComponent<VRMHumanoidDescription>();
if (humanoid!=null && humanoid.Description != null)
{
yield return humanoid.Description;
}
else
{
var animator = go.GetComponent<Animator>();
if(animator!=null && animator.avatar)
{
var description= UniHumanoid.AvatarDescription.CreateFrom(animator.avatar);
if (description != null)
{
description.name = "AvatarDescription";
yield return description;
}
}
}
}
static IEnumerable<Texture2D> GetThumbnails(GameObject go)
{
var meta = go.GetComponent<VRMMetaInformation>();
if (meta != null && meta.Thumbnail != null)
{
yield return meta.Thumbnail;
}
}
static IEnumerable<UnityEngine.Object> GetSubAssets(String prefabPath)
{
return AssetDatabase.LoadAllAssetsAtPath(prefabPath);
}
public static void SaveAsPrefab(GameObject root, String path)
{
var prefabPath = path.ToUnityRelativePath();
Debug.LogFormat("SaveAsPrefab: {0}", prefabPath);
// clear subassets
if (File.Exists(prefabPath))
{
//Debug.LogFormat("Exist: {0}", m_prefabPath);
// clear subassets
foreach (var x in GetSubAssets(prefabPath))
{
if (x is Transform
|| x is GameObject)
{
continue;
}
GameObject.DestroyImmediate(x, true);
}
}
// add subassets
var writers = new ISubAssetWriter[]{
new SubAssetWriter<Texture2D>(prefabPath, GetTextures),
new SubAssetWriter<Material>(prefabPath, GetMaterials),
new SubAssetWriter<Mesh>(prefabPath, GetMeshs),
new SubAssetWriter<Avatar>(prefabPath, GetAvatars),
// VRM Objects
new SubAssetWriter<BlendShapeClip>(prefabPath, GetBlendShapeClips),
new SubAssetWriter<BlendShapeAvatar>(prefabPath, GetBlendShapeAvatars),
new SubAssetWriter<UniHumanoid.AvatarDescription>(prefabPath, GetAvatarDecriptions),
new SubAssetWriter<Texture2D>(prefabPath, GetThumbnails),
};
foreach (var x in root.transform.Traverse())
{
foreach (var writer in writers)
{
writer.WriteIfHas(x.gameObject);
}
}
///
/// create prefab, after subasset AssetDatabase.AddObjectToAsset
///
if (File.Exists(prefabPath))
{
//Debug.LogFormat("ReplacePrefab: {0}", m_prefabPath);
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
PrefabUtility.ReplacePrefab(root, prefab, ReplacePrefabOptions.ConnectToPrefab);
}
else
{
//Debug.LogFormat("CreatePrefab: {0}", m_prefabPath);
PrefabUtility.CreatePrefab(prefabPath, root, ReplacePrefabOptions.ConnectToPrefab);
}
}
}
}
#endif