Unity(三十九):非运行状态下脚本播放动画、Animator Override Controller、RuntimeAnimatorController
企业开发
2022-06-01 14:43:38
阅读次数: 0
非运行状态下脚本播放动画
- 脚本
using UnityEngine;
namespace Example_01.Scripts
{
[RequireComponent(typeof(Animator))]
public class PlayAnim : MonoBehaviour
{
}
}
using System.Linq;
using Example_01.Scripts;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
namespace Example_01.Editor
{
[CustomEditor(typeof(PlayAnim))]
public class PlayAnimEditor : UnityEditor.Editor
{
private AnimationClip[] _animationClips;
private PlayAnim _script;
private void OnEnable()
{
_script = target as PlayAnim;
Animator animator = _script!.gameObject.GetComponent<Animator>();
AnimatorController animatorController = (AnimatorController)animator.runtimeAnimatorController;
_animationClips = animatorController.animationClips;
}
private int _selectedIndex;
private float _sliderValue;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EditorGUI.BeginChangeCheck();
string[] displayedOptions = _animationClips.Select(clip => clip.name).ToArray();
_selectedIndex = EditorGUILayout.Popup("动画", _selectedIndex, displayedOptions);
_sliderValue = EditorGUILayout.Slider("进度", _sliderValue, 0.0f, 1.0f);
if (EditorGUI.EndChangeCheck())
{
AnimationClip animationClip = _animationClips[_selectedIndex];
float time = animationClip.length * _sliderValue;
animationClip.SampleAnimation(_script.gameObject, time);
}
}
}
}
Animator Override Controller
可复用动画控制器,为不同的模型附上相同的动画效果使用
RuntimeAnimatorController
读取/删除 动画控制器
转载自blog.csdn.net/weixin_43526371/article/details/123659820