HumanPoseTransferEditor.cs
5.18 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
using System;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace UniHumanoid
{
[CustomEditor(typeof(HumanPoseTransfer))]
public class HumanPoseTransferEditor : Editor
{
//HumanPoseTransfer m_target;
SerializedProperty m_avatarProp;
SerializedProperty m_typeProp;
SerializedProperty m_clipProp;
SerializedProperty m_transferProp;
static string[] SOURCE_TYPES = ((HumanPoseTransfer.HumanPoseTransferSourceType[])
Enum.GetValues(typeof(HumanPoseTransfer.HumanPoseTransferSourceType)))
.Select(x => x.ToString())
.ToArray();
private void OnEnable()
{
//m_target = (HumanPoseTransfer)target;
m_typeProp = serializedObject.FindProperty("SourceType");
m_clipProp = serializedObject.FindProperty("PoseClip");
m_avatarProp = serializedObject.FindProperty("Avatar");
m_transferProp = serializedObject.FindProperty("Source");
}
public override void OnInspectorGUI()
{
//base.OnInspectorGUI();
serializedObject.Update();
EditorGUILayout.PropertyField(m_avatarProp);
/*
m_typeProp.intValue =
GUILayout.Toolbar(m_typeProp.intValue, SOURCE_TYPES);
*/
m_typeProp.intValue =
EditorGUILayout.Popup("SourceType", m_typeProp.intValue, SOURCE_TYPES);
switch ((HumanPoseTransfer.HumanPoseTransferSourceType)m_typeProp.intValue)
{
case HumanPoseTransfer.HumanPoseTransferSourceType.None:
serializedObject.ApplyModifiedProperties();
break;
case HumanPoseTransfer.HumanPoseTransferSourceType.HumanPoseClip:
PoseClipInspector();
break;
case HumanPoseTransfer.HumanPoseTransferSourceType.HumanPoseTransfer:
PoseHandler();
break;
}
GUILayout.Space(20);
// CreatePose
if (GUILayout.Button("Pose to HumanPoseClip"))
{
var path = EditorUtility.SaveFilePanel(
"Save humanpose",
Application.dataPath,
string.Format("{0}.pose.asset", serializedObject.targetObject.name),
"asset");
var assetPath = ToAssetPath(path);
if (!string.IsNullOrEmpty(path))
{
var pose = ((HumanPoseTransfer)serializedObject.targetObject).CreatePose();
var clip = ScriptableObject.CreateInstance<HumanPoseClip>();
clip.ApplyPose(ref pose);
AssetDatabase.CreateAsset(clip, assetPath);
Selection.activeObject = clip;
}
}
// CreatePose
if (GUILayout.Button("Pose to AnimationClip"))
{
var path = EditorUtility.SaveFilePanel(
"Save animnationClip",
Application.dataPath,
string.Format("{0}.pose.anim", serializedObject.targetObject.name),
"anim");
var assetPath = ToAssetPath(path);
if (!string.IsNullOrEmpty(path))
{
var pose = ((HumanPoseTransfer)serializedObject.targetObject).CreatePose();
var clip = AnimationClipUtility.CreateAnimationClipFromHumanPose(pose);
AssetDatabase.CreateAsset(clip, assetPath);
Selection.activeObject = clip;
}
}
}
public static string ToAssetPath(string src)
{
src = src.Replace("\\", "/");
var basePath = Path.GetFullPath(Application.dataPath + "/..").Replace("\\", "/");
if (!src.StartsWith(basePath))
{
return null;
}
return src.Substring(basePath.Length + 1);
}
void PoseClipInspector()
{
var old = (HumanPoseClip)m_clipProp.objectReferenceValue;
EditorGUILayout.PropertyField(m_clipProp);
serializedObject.ApplyModifiedProperties();
var _target = (HumanPoseTransfer)target;
if (_target.PoseClip != old)
{
//Debug.Log("clip != old");
if (_target.PoseClip != null)
{
var pose = _target.PoseClip.GetPose();
_target.SetPose(pose);
}
}
#if false
if (_target.PoseClip != null)
{
if (GUILayout.Button("Apply PoseClip"))
{
Debug.Log("apply");
var pose = default(HumanPose);
_target.PoseClip.GetPose(out pose);
_target.SetPose(pose);
}
}
#endif
}
void PoseHandler()
{
EditorGUILayout.PropertyField(m_transferProp);
serializedObject.ApplyModifiedProperties();
}
}
}