Animator与Animation之间的互转

本文介绍如何解决Unity中材质属性动画的问题。通过调整动画类型、修改动画文件格式等步骤,实现从新版Animator到Legacy Animation系统的迁移。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Legacy Animation Can’t Animate Material Property

问题描述

使用Animator可以控制材质的属性,但是Animator的初始化比较耗CPU,我们这里想继续使用Animation,但是Aniamtion自Unity 4.3之后就无法直接控制材质的属性了.

问题原因

Unity新版改变了Material 在 Aniamtion Clip 关键帧的格式,旧版Animation不支持.

The problem is that Unity has changed its Material keyframe formatting when it saves animation clips. If you need to use the legacy animation system here’s how to fix it:

Change animation type to 1

open the clip in a text editor, and replace classID 23 with classID 21

Still in the text editor, remove material. from the beginning of each Material attribute

Don’t know if this will fix the problem for everyone, but it worked wonders for me after banging my head against a wall for a while.

-Sterling Sr. Animator - Proletariat Inc.

解决方法

  1. Change this to “Force Text” [Edit->Project Settings->Editor] -> Asset Serialization
  2. 用文本编辑器打开生成的动画文件
  3. 将 ‘classID: 23’ 替换为’classID: 21’
  4. 将 ‘material.’ 替换为 ” (空串)
  5. 保存
  6. 在动画的Inspector面板上将Legacy选项勾上.

Animator转Animation:
  public static void ior2ion(string path)
    {

        string relPath = UniversalEditorUtility.AssetPath2SysFullPath(path);
        var aniContent = File.ReadAllText(relPath);
        if (string.IsNullOrEmpty(aniContent)) return;
        if (aniContent.Contains("m_Legacy:")) {
            aniContent = aniContent.Replace("m_Legacy: 0", "m_Legacy: 1");
        } else {
            aniContent = aniContent.Replace("AnimationClip:\n", "AnimationClip:\n  m_Legacy: 1\n");
        }

        aniContent = aniContent.Replace("m_AnimationType: 2", "m_AnimationType: 1");
        aniContent = aniContent.Replace("classID: 23", "classID: 21");
        aniContent = aniContent.Replace("attribute: material._", "attribute: _");
        if (aniContent.Contains("m_LoopTime: 1")) {
            aniContent = aniContent.Replace("m_WrapMode: 0", "m_WrapMode: 2");
        }
        UniversalEditorUtility.MakeFileWriteable(relPath);
        File.WriteAllText(relPath, aniContent);
        var clip = AssetDatabase.LoadAssetAtPath<AnimationClip>(path);
        clip.legacy = true;
    }

Animation转Animator:
  public static void ion2ior(string path)
    {
        string relPath = UniversalEditorUtility.AssetPath2SysFullPath(path);
        var aniContent = File.ReadAllText(relPath);
        if (string.IsNullOrEmpty(aniContent)) return;

        var regex = new Regex(@"attribute: _(\w_\.)+\s+path:.\s+classID: 21");
        //var regex = new Regex(@"attribute:\s+_\S+\n");
        aniContent = regex.Replace(aniContent, (m) => {
            string result = m.Value.Replace("attribute: _", "attribute: material._");
            result = result.Replace("classID: 21", "classID: 23");
            return result;
        });
        UniversalEditorUtility.MakeFileWriteable(relPath);
        File.WriteAllText(relPath, aniContent);
        var clip = AssetDatabase.LoadAssetAtPath<AnimationClip>(path);
        clip.legacy = false;
        EditorUtility.SetDirty(clip);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独立游戏开发指南

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值