Unity3D之Mecanim动画系统学习笔记(十):Mecanim动画的资源加载相关

本文介绍Unity中Mecanim动画资源的两种加载方式:Resources加载和AssetBundle加载,并演示了模型与动画一体或分离存放的不同处理方法。

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

资源加载是必备的知识点,这里就说说Mecanim动画的资源如何打包及加载。

注意,Unity4.x和Unity5.x的AssetBundle打包策略不一样,本笔记是基于Unity4.x的AssetBundle进行打包的。

我们一般使用FBX类型的模型及动画文件,而动画文件的储存一般有两种情况,一是所有的动画和模型都一起存放到一个文件中,还有一种情况是模型单独一个文件而动画单独一个文件。这里我们就两种情况都看一下。

使用的资源是Unity3D自带的以及从一本教材中取出的两种类型的动画资源,同时需要对其动画创建对应的Animator Controller。

模型动画都存放在一个文件中的情况

一个FBX文件保存了模型、骨骼和动画,如下图:



下面是配置的Animator Controller:


需要注意的是,官方并没有提供为Animator设置Animator Controller的接口,所以我们必须将配置好的GameObject制作为一个预制件进行加载。

Resources加载

using UnityEngine;
using System.Collections;

public class AllInOneResourcesLoad : MonoBehaviour
{
    private Animator _animator;

    void Start()
    {
        GameObject go = Resources.Load<GameObject>("AllInOne/ConstructorPrefab");

        GameObject man = Instantiate(go) as GameObject;
        _animator = man.GetComponent<Animator>();
    }
    
    void OnGUI()
    {
        if(GUI.Button(new Rect(0, 0, 100, 30), "idle"))
        {
            _animator.SetBool("walk", false);
            _animator.SetBool("run", false);
        }
        if(GUI.Button(new Rect(100, 0, 100, 30), "walk"))
        {
            _animator.SetBool("walk", true);
            _animator.SetBool("run", false);
        }
        if(GUI.Button(new Rect(200, 0, 100, 30), "run"))
        {
            _animator.SetBool("walk", false);
            _animator.SetBool("run", true);
        }
        if(GUI.Button(new Rect(300, 0, 100, 30), "jump"))
        {
            _animator.SetTrigger("jump");
        }
    }
}



AssetBundle加载

打包

using UnityEngine;
using UnityEditor;

public class CreateAllInOneAB
{
    [MenuItem("Tool/CreateAllInOneAB")]
    private static void Create()
    {
        BuildPipeline.BuildAssetBundle(null, new[]
            {
                AssetDatabase.LoadAssetAtPath("Assets/Resources/AllInOne/ConstructorPrefab.prefab", typeof(GameObject))
            },
            Application.streamingAssetsPath + "/AllInOne.assetbundle",
            BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.UncompressedAssetBundle,
            BuildTarget.StandaloneWindows64);
    }
}



加载

using UnityEngine;
using System.Collections;

public class AllInOneAssetBundleLoad : MonoBehaviour
{
    private Animator _animator;

    void Start()
    {
        AssetBundle assetBundle = AssetBundle.CreateFromFile(Application.streamingAssetsPath + "/AllInOne.assetbundle");

        GameObject go = assetBundle.Load("ConstructorPrefab", typeof(GameObject)) as GameObject;

        GameObject man = Instantiate(go) as GameObject;
        _animator = man.GetComponent<Animator>();
    }

    void OnGUI()
    {
        if(GUI.Button(new Rect(0, 0, 100, 30), "idle"))
        {
            _animator.SetBool("walk", false);
            _animator.SetBool("run", false);
        }
        if (GUI.Button(new Rect(100, 0, 100, 30), "walk"))
        {
            _animator.SetBool("walk", true);
            _animator.SetBool("run", false);
        }
        if (GUI.Button(new Rect(200, 0, 100, 30), "run"))
        {
            _animator.SetBool("walk", false);
            _animator.SetBool("run", true);
        }
        if (GUI.Button(new Rect(300, 0, 100, 30), "jump"))
        {
            _animator.SetTrigger("jump");
        }
    }
}

模型动画分开存放的情况

还有一种情况是模型和动画是分为多个FBX文件存放的,比如下面是模型文件:


虽然有一个Take 001的动画,但是实际上我们并不使用该动画,而是使用下面仅保存了动画的FBX文件:


下面是配置的Animator Controller:


除了没有提供设置Animator Controller的接口,也无法在运行时对动画剪辑进行增加删除的操作,所以我们一般打包时就收集所有的依赖项一起打包,归根结底还是只需要一个制作好的预制件即可。

从这个角度看,其实是否将动画进行拆分最终的使用方式都是一样的。

Resources加载

using UnityEngine;
using System.Collections;

public class ResourcesLoad : MonoBehaviour
{
    private Animator _animator;

    void Start()
    {
        GameObject go = Resources.Load<GameObject>("ZombieNurse/ZombieNursePrefab");

        GameObject man = Instantiate(go) as GameObject;
        _animator = man.GetComponent<Animator>();
    }

    void OnGUI()
    {
        if(GUI.Button(new Rect(0, 0, 100, 30), "idle"))
        {
            _animator.SetBool("run", false);
        }
        if(GUI.Button(new Rect(100, 0, 100, 30), "run"))
        {
            _animator.SetBool("run", true);
        }
        if(GUI.Button(new Rect(200, 0, 100, 30), "attack"))
        {
            _animator.SetTrigger("attack");
        }
        if(GUI.Button(new Rect(300, 0, 100, 30), "dead"))
        {
            _animator.SetTrigger("dead");
        }
    }
}


AssetBundle加载

打包

using UnityEditor;
using UnityEngine;

public class CreateAB
{
    [MenuItem("Tool/CreateAB")]
    private static void Create()
    {
        BuildPipeline.BuildAssetBundle(null, new[]
            {
                AssetDatabase.LoadAssetAtPath("Assets/Resources/ZombieNurse/ZombieNursePrefab.prefab", typeof(GameObject))
            },
            Application.streamingAssetsPath + "/AB.assetbundle",
            BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.UncompressedAssetBundle,
            BuildTarget.StandaloneWindows64);
    }
}

加载

using UnityEngine;
using System.Collections;

public class AssetBundleLoad : MonoBehaviour
{
    private Animator _animator;

    void Start()
    {
        AssetBundle assetBundle = AssetBundle.CreateFromFile(Application.streamingAssetsPath + "/AB.assetbundle");

        GameObject go = assetBundle.Load("ZombieNursePrefab", typeof(GameObject)) as GameObject;

        GameObject man = Instantiate(go) as GameObject;
        _animator = man.GetComponent<Animator>();
    }

    void OnGUI()
    {
        if(GUI.Button(new Rect(0, 0, 100, 30), "idle"))
        {
            _animator.SetBool("run", false);
        }
        if(GUI.Button(new Rect(100, 0, 100, 30), "run"))
        {
            _animator.SetBool("run", true);
        }
        if(GUI.Button(new Rect(200, 0, 100, 30), "attack"))
        {
            _animator.SetTrigger("attack");
        }
        if(GUI.Button(new Rect(300, 0, 100, 30), "dead"))
        {
            _animator.SetTrigger("dead");
        }
    }
}

天道酬勤,功不唐捐!

原文地址:http://www.cnblogs.com/hammerc/p/4832650.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值