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



### 创建和使用自定义属性Unity中,`Attribute`类允许关联预定义的系统信息或用户定义的自定义信息到目标元素上[^4]。为了创建自定义属性,需继承于`System.Attribute`基类并重载无参构造函数。 #### 定义自定义属性 下面是一个简单的例子来展示如何定义一个名为`MyCustomAttribute`的自定义属性: ```csharp using System; public class MyCustomAttribute : Attribute { public string Description { get; } public MyCustomAttribute(string description) { this.Description = description; } } ``` 此代码片段展示了怎样通过传递描述字符串给构造器来自定义属性的行为。 #### 应用自定义属性 一旦定义好了自定义属性,则可以将其应用于各种编程结构之上,如下所示应用到了字段、方法以及类本身: ```csharp [MyCustom("This is my custom class")] public class MyClass { [MyCustom("A simple integer field.")] private int _myField; [MyCustom("Method that does something interesting.")] public void DoSomething() { Console.WriteLine("Doing something..."); } } ``` 这里利用方括号语法将`MyCustomAttribute`附加给了不同的成员,同时传入特定的信息作为参数。 #### 获取自定义属性 为了让这些元数据变得有用,还需要一种方式去检索它们。这可以通过反射机制完成,即运行时动态获取对象类型的特性及其上的所有属性: ```csharp var type = typeof(MyClass); foreach (var attr in type.GetCustomAttributes(typeof(MyCustomAttribute), false)) { var customAttr = attr as MyCustomAttribute; if(customAttr != null){ Debug.Log($"Type has attribute with message: '{customAttr.Description}'"); } } // For fields and methods... foreach(var field in type.GetFields()){ foreach(var attr in field.GetCustomAttributes(true)){ // Similar processing here. } } ``` 这段脚本遍历了指定类型的所有自定义属性,并打印出了每个找到的`Description`值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值