Unity Animation采样和自定义属性

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 frame
	void 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;
    }
}
这样就可以自由操作动画的播放了



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值