90 lines
2.7 KiB
C#

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
namespace UniHumanoid
{
[CustomEditor(typeof(HumanPoseClip))]
public class HumanPoseClipEditor : Editor
{
private Animator m_animator;
private HumanPoseHandler m_poseHandler;
private Animator m_lastAnimator;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
var clip = target as HumanPoseClip;
if (clip == null) return;
EditorGUILayout.Space();
EditorGUILayout.LabelField("Pose Application", EditorStyles.boldLabel);
m_animator = EditorGUILayout.ObjectField("Target Animator", m_animator, typeof(Animator), true) as Animator;
// Recreate pose handler if animator changed
if (m_animator != m_lastAnimator)
{
m_poseHandler = null;
m_lastAnimator = m_animator;
}
if (m_animator != null && m_animator.isHuman)
{
if (GUILayout.Button("Apply Pose"))
{
ApplyPose(clip);
}
if (GUILayout.Button("Capture Current Pose"))
{
CapturePose(clip);
}
}
else if (m_animator != null)
{
EditorGUILayout.HelpBox("Selected animator must be humanoid!", MessageType.Warning);
}
}
private void ApplyPose(HumanPoseClip clip)
{
if (m_animator == null || !m_animator.isHuman) return;
// Create pose handler if needed
if (m_poseHandler == null)
{
m_poseHandler = new HumanPoseHandler(m_animator.avatar, m_animator.transform);
}
var pose = clip.GetPose();
m_poseHandler.SetHumanPose(ref pose);
// Record undo
Undo.RegisterFullObjectHierarchyUndo(m_animator.gameObject, "Apply Pose");
// Ensure the changes are visible in the scene view
EditorUtility.SetDirty(m_animator.gameObject);
SceneView.RepaintAll();
}
private void CapturePose(HumanPoseClip clip)
{
if (m_animator == null || !m_animator.isHuman) return;
// Create pose handler if needed
if (m_poseHandler == null)
{
m_poseHandler = new HumanPoseHandler(m_animator.avatar, m_animator.transform);
}
var pose = new HumanPose();
m_poseHandler.GetHumanPose(ref pose);
Undo.RecordObject(clip, "Capture Pose");
clip.ApplyPose(ref pose);
EditorUtility.SetDirty(clip);
}
}
}