Unity Animation采样和自定义属性

来源:互联网 发布:淘宝店铺类目修改2016 编辑:程序博客网 时间:2024/05/21 01:29

1、了解Animation的采样函数Sample怎么使用。

2、了解自定义属性面板

动画的采样可用于自定义编辑器等工具,也可以自己实现一套动画系统

首先看一下自定义属性面板,网上都有相关代码和教程,在这里粘贴一处网上的代码,如果冒犯,望海涵~

using UnityEngine;using System.Collections;public class MyRangeAttribute : PropertyAttribute{    public float min;    public float max;    public string label;    public MyRangeAttribute( float min, float max, string label = "")    {        this.min = min;        this.max = max;        this.label = label;    }}

using UnityEngine;using UnityEditor;using System.Collections;//该代码放在Editor目录下[CustomPropertyDrawer(typeof(MyRangeAttribute))]public class MyRangeAttributeDrawer : PropertyDrawer {    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)    {        MyRangeAttribute range = this.attribute as MyRangeAttribute;        if( property.propertyType == SerializedPropertyType.Float )        {            if (range.label != string.Empty)            {                label.text = range.label;            }            EditorGUI.Slider(position, property, range.min, range.max, label);        }        else if( property.propertyType == SerializedPropertyType.Integer )        {            if( range.label != string.Empty )            {                label.text = range.label;            }            EditorGUI.IntSlider(position, property, (int)range.min, (int)range.max, label);        }    }}


动画采集及自定义属性使用

using UnityEngine;using System.Collections;[ExecuteInEditMode]public class AnimationTest : MonoBehaviour {    public Animation anim;    [MyRangeAttribute(0f,1f,"动画采样")]    public float curValue = 0f;    float deltaTime = 0f;    float lastFrameTime = 0f;    float progressTime = 0f;    string clipName = "run";void Start ()     {        anim.enabled = false;        AnimationState state = anim[clipName];        state.enabled = true;        state.weight = 1;        state.normalizedTime = 0;        anim.Sample();        state.enabled = false;}// Update is called once per framevoid Update ()     {        deltaTime = Time.realtimeSinceStartup;        progressTime += deltaTime - lastFrameTime;        Example();        lastFrameTime = deltaTime;}    void Example()    {        AnimationState animState = anim[clipName];        animState.enabled = true;        animState.speed = 1f;        animState.weight = 1;        if (curValue > 1f)        {            curValue = 0f;        }        animState.normalizedTime = curValue;        anim.Sample();        animState.enabled = false;    }}
这样就可以自由操作动画的播放了



0 0
原创粉丝点击