91 lines
1.8 KiB
C#
91 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace BoneTool.Script.Runtime
|
|
{
|
|
[RequireComponent(typeof(BoneVisualiser))]
|
|
public class BonePoseLib : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
public class ArmaturePose
|
|
{
|
|
public string Name = "New Pose";
|
|
|
|
public List<BonePose> BonePoses;
|
|
}
|
|
|
|
[Serializable]
|
|
public struct BonePose
|
|
{
|
|
public Vector3 SavedPos;
|
|
|
|
public Vector3 SavedRot;
|
|
|
|
public Vector3 SavedSca;
|
|
|
|
public Transform TargetTransform;
|
|
|
|
public BonePose(Transform targetTransform)
|
|
{
|
|
this = default(BonePose);
|
|
TargetTransform = targetTransform;
|
|
SavedPos = TargetTransform.localPosition;
|
|
SavedRot = TargetTransform.localEulerAngles;
|
|
SavedSca = TargetTransform.localScale;
|
|
}
|
|
|
|
public void Apply()
|
|
{
|
|
TargetTransform.localPosition = SavedPos;
|
|
TargetTransform.localEulerAngles = SavedRot;
|
|
TargetTransform.localScale = SavedSca;
|
|
}
|
|
}
|
|
|
|
public List<ArmaturePose> Poses;
|
|
|
|
public void AddPose(ArmaturePose pose)
|
|
{
|
|
Poses.Add(pose);
|
|
}
|
|
|
|
public void RemovePose(string poseName)
|
|
{
|
|
for (int i = 0; i < Poses.Count; i++)
|
|
{
|
|
if (Poses[i].Name == poseName)
|
|
{
|
|
Poses.RemoveAt(i);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetPose(int index)
|
|
{
|
|
ArmaturePose armaturePose = Poses[index];
|
|
armaturePose.BonePoses = new List<BonePose>();
|
|
Transform[] childNodes = GetComponent<BoneVisualiser>().GetChildNodes();
|
|
foreach (Transform targetTransform in childNodes)
|
|
{
|
|
armaturePose.BonePoses.Add(new BonePose(targetTransform));
|
|
}
|
|
}
|
|
|
|
public void ApplyPose(int index)
|
|
{
|
|
ArmaturePose armaturePose = Poses[index];
|
|
if (armaturePose.BonePoses == null)
|
|
{
|
|
MonoBehaviour.print("Empty pose");
|
|
return;
|
|
}
|
|
foreach (BonePose bonePose in armaturePose.BonePoses)
|
|
{
|
|
bonePose.Apply();
|
|
}
|
|
}
|
|
}
|
|
}
|