-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
VRMInitializer.cs
73 lines (63 loc) · 3.27 KB
/
VRMInitializer.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
#nullable enable
using System.Linq;
using UnityEngine;
using UnityEditor;
using VRM;
namespace Esperecyan.UniVRMExtensions
{
/// <summary>
/// Hmanoidモデルを直接VRMプレハブにします。
/// </summary>
public class VRMInitializer
{
/// <summary>
/// プレハブアセットを上書きしてVRMプレハブにします。
/// </summary>
/// <param name="prefabPath">「Assets/」で始まるプレハブアセットのパス。</param>
/// <param name="prefabInstance"><see cref="PrefabUtility.LoadPrefabContents"/>で開いたプレハブインスタンス。
/// 指定されていなければ、「prefabPath」を<see cref="PrefabUtility.LoadPrefabContents"/>で開き、処理後、
/// <see cref="PrefabUtility.SaveAsPrefabAsset"/>、<see cref="PrefabUtility.UnloadPrefabContents"/>、
/// <see cref="PrefabUtility.SaveAssets"/>を実行します。</param>
public static void Initialize(string prefabPath, GameObject? prefabInstance = null)
{
var prefab = prefabInstance != null ? prefabInstance : PrefabUtility.LoadPrefabContents(prefabPath);
var animator = prefab.GetComponent<Animator>();
var metaObject = ScriptableObject.CreateInstance<VRMMetaObject>();
metaObject.name = "Meta";
var meta = prefab.AddComponent<VRMMeta>();
meta.Meta = AssetUtility.Save(prefabPath, metaObject);
var humanoidDescription = prefab.AddComponent<VRMHumanoidDescription>();
humanoidDescription.Avatar = animator.avatar;
var blendShapeProxy = prefab.AddComponent<VRMBlendShapeProxy>();
var blendShapeAvatar = ScriptableObject.CreateInstance<BlendShapeAvatar>();
blendShapeAvatar.name = "BlendShape";
blendShapeProxy.BlendShapeAvatar = AssetUtility.Save(prefabPath, blendShapeAvatar);
blendShapeProxy.BlendShapeAvatar.CreateDefaultPreset();
blendShapeAvatar.Clips
= blendShapeAvatar.Clips.Select(clip => AssetUtility.Save(prefabPath, clip)).ToList();
var firstPerson = prefab.AddComponent<VRMFirstPerson>();
firstPerson.SetDefault();
firstPerson.TraverseRenderers();
prefab.AddComponent<VRMLookAtHead>();
var lookAtBoneApplyer = prefab.AddComponent<VRMLookAtBoneApplyer>();
lookAtBoneApplyer.LeftEye = OffsetOnTransform.Create(animator.GetBoneTransform(HumanBodyBones.LeftEye));
lookAtBoneApplyer.RightEye = OffsetOnTransform.Create(animator.GetBoneTransform(HumanBodyBones.RightEye));
var secondary = prefab.transform.Find("secondary");
if (secondary == null)
{
secondary = new GameObject("secondary").transform;
secondary.SetParent(prefab.transform, false);
}
if (secondary.GetComponent<VRMSpringBone>() == null)
{
secondary.gameObject.AddComponent<VRMSpringBone>();
}
if (prefabInstance == null)
{
PrefabUtility.SaveAsPrefabAsset(prefab, prefabPath);
PrefabUtility.UnloadPrefabContents(prefab);
AssetDatabase.SaveAssets();
}
}
}
}