generated from raccoon/vrchat-project-template
Added scripts stub
This commit is contained in:
58
Assets/hwm/Scripts/HWMSetup.cs
Normal file
58
Assets/hwm/Scripts/HWMSetup.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using UnityEngine;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
using UnityEditor.Animations;
|
||||
#endif
|
||||
|
||||
namespace RiggingTools.FacialAnimation
|
||||
{
|
||||
[RequireComponent(typeof(SkinnedMeshRenderer))]
|
||||
public class HWMSetup : MonoBehaviour
|
||||
{
|
||||
[Header("Animation Settings")]
|
||||
public float moveDistance = 1f;
|
||||
public float duration = 2f;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
public void GenerateAnimationController()
|
||||
{
|
||||
var skinnedMesh = GetComponent<SkinnedMeshRenderer>();
|
||||
if (skinnedMesh == null)
|
||||
{
|
||||
Debug.LogWarning($"Skinned Mesh Renderer not found on {gameObject.name}");
|
||||
return;
|
||||
}
|
||||
|
||||
string objName = gameObject.name;
|
||||
|
||||
var clip = new AnimationClip();
|
||||
clip.name = objName + "_MoveUpDown";
|
||||
|
||||
Keyframe[] keys = {
|
||||
new Keyframe(0, 0),
|
||||
new Keyframe(duration / 2f, moveDistance),
|
||||
new Keyframe(duration, 0)
|
||||
};
|
||||
|
||||
var curve = new AnimationCurve(keys);
|
||||
clip.SetCurve("", typeof(Transform), "localPosition.y", curve);
|
||||
|
||||
// Make loop
|
||||
var clipSettings = AnimationUtility.GetAnimationClipSettings(clip);
|
||||
clipSettings.loopTime = true;
|
||||
AnimationUtility.SetAnimationClipSettings(clip, clipSettings);
|
||||
|
||||
AssetDatabase.CreateAsset(clip, $"Assets/{objName}_MoveUpDown.anim");
|
||||
|
||||
var controller = AnimatorController.CreateAnimatorControllerAtPath($"Assets/{objName}_Controller.controller");
|
||||
var state = controller.layers[0].stateMachine.AddState("MoveUpDown");
|
||||
state.motion = clip;
|
||||
controller.layers[0].stateMachine.defaultState = state;
|
||||
|
||||
AssetDatabase.SaveAssets();
|
||||
|
||||
Debug.Log($"Generated animator setup for {objName}");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user