AssetBundleMgr.cs 5.0 KB
Newer Older
L
linxinfa 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;


public class AssetBundleMgr
{
    public AssetBundleMgr()
    {
        m_bundles = new Dictionary<string, AssetBundle>();
        m_assets = new Dictionary<string, Object>();

    }

    public void PreloadAssetBundles()
    {
20 21 22 23 24
#if UNITY_EDITOR
        // Nothing
#else
        m_normalCfgBundle = LoadAssetBundle("normal_cfg.bundle");
#endif
L
linxinfa 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    }

    public Object LoadAsset(string uri)
    {
        System.Type t = GetAssetType(uri);
        if (m_assets.ContainsKey(uri))
            return m_assets[uri];

        Object obj = null;
#if UNITY_EDITOR
        obj = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/GameRes/" + uri, t);
#else
        // resources.bytes的第一级目录为AssetBundle
        var abName = uri.Substring(0, uri.IndexOf("/")).ToLower() + ".bundle";
        var fname = Path.GetFileName(uri);
        AssetBundle ab = null;
        if (File.Exists(updatePath + "/" + fname))
        {
            // 热更的资源,是一个独立的AssetBundle文件,以fname为文件名
            ab = LoadAssetBundle(fname);
        }
        else
        {
            ab = LoadAssetBundle(abName);
        }
        if (null != ab)
        {
            var assetName = fname.Substring(0, fname.IndexOf("."));
            obj = ab.LoadAsset<Object>(assetName);
            GameLogger.LogGreen("Load Asset From AssetBundle: assetName: " + assetName);
        }
#endif

        if (null != obj)
        {
            m_assets[uri] = obj;
61

L
linxinfa 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        }
        return obj;
    }


    public AssetBundle LoadAssetBundle(string abName)
    {
        if (m_bundles.ContainsKey(abName))
            return m_bundles[abName];


        AssetBundle bundle = null;
        if (File.Exists(updatePath + "/" + abName))
        {
            // 优先从update目录(热更新目录)中查找资源
77
            bundle = AssetBundle.LoadFromFile(updatePath + abName);
L
linxinfa 已提交
78 79 80 81
        }
        else if (File.Exists(extPath + "/" + abName))
        {
            // 从拓展包目录加载资源
82
            bundle = AssetBundle.LoadFromFile(extPath + abName);
L
linxinfa 已提交
83 84 85
        }
        else
        {
86
            bundle = AssetBundle.LoadFromFile(internalPath + abName);
L
linxinfa 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100
        }

        /*
        // 如果对AssetBundle做了加密处理,则需要使用流式读取,进行解密后再通过AssetBundle.LoadFromMemory加载AssetBundle
        byte[] stream = null;
        stream = File.ReadAllBytes(path + "/" + abName);
        // TOOD 对stream做解密

        var bundle = AssetBundle.LoadFromMemory(stream); 
        */

        if (null != bundle)
        {
            m_bundles[abName] = bundle;
L
linxinfa 已提交
101
            GameLogger.Log("LoadAssetBundle Ok, abName: " + abName);
L
linxinfa 已提交
102
        }
103

L
linxinfa 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        return bundle;
    }




    protected System.Type GetAssetType(string uri)
    {
        if (uri.EndsWith(".prefab"))
        {
            return typeof(GameObject);
        }
        else if (uri.EndsWith(".ogg") || uri.EndsWith(".wav"))
        {
            return typeof(AudioClip);
        }
120
        else if (uri.EndsWith(".spriteatlas"))
L
linxinfa 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        {
            return typeof(UnityEngine.U2D.SpriteAtlas);
        }
        else if (uri.EndsWith(".mat"))
        {
            return typeof(Material);
        }
        else if (uri.EndsWith(".anim"))
        {
            return typeof(AnimationClip);
        }
        return typeof(AssetBundle);
    }

    /// <summary>
    /// 热更新目录
    /// </summary>
    /// <value></value>
    public string updatePath
    {
        get
        {
            return Application.persistentDataPath + "/update/";
        }
    }

    /// <summary>
    /// 拓展包目录
    /// </summary>
    /// <value></value>
    public string extPath
    {
        get
        {
            return Application.persistentDataPath + "/ext/";
        }
    }

    /// <summary>
    /// 内部资源目录
161 162 163 164
    /// 获取内部资源路径,对应streamingAssetsPath,
    /// 一般情况,streamingAssetsPath目录中的资源只能用WWW异步加载,但是AssetBundle提供了LoadFromFile方法可以直接同步加载
    /// 在Android中,Application.streamingAssetsPath会在前面加上"jar:file://"
    /// 所以不直接使用Application.streamingAssetsPath,而使用Application.dataPath + "!assets“
L
linxinfa 已提交
165 166 167 168 169 170
    /// </summary>
    /// <value></value>
    public string internalPath
    {
        get
        {
171 172 173
#if UNITY_ANDROID
                return Application.dataPath + "!assets/res/";
#else
L
linxinfa 已提交
174
            return Application.streamingAssetsPath + "/res/";
175
#endif
L
linxinfa 已提交
176 177 178 179 180 181 182
        }
    }

    /// <summary>
    /// 常规配置AssetBundle (C# 使用的xml配置表)
    /// </summary>
    public AssetBundle m_normalCfgBundle;
183

L
linxinfa 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

    private Dictionary<string, Object> m_assets;
    private Dictionary<string, AssetBundle> m_bundles;

    private static AssetBundleMgr s_instance;
    public static AssetBundleMgr instance
    {
        get
        {
            if (null == s_instance)
                s_instance = new AssetBundleMgr();
            return s_instance;
        }
    }
}