AssetBundleMgr.cs 4.9 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
    }

    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)
        {
L
linxinfa 已提交
52
            obj = ab.LoadAsset<Object>("Assets/GameRes/" + uri);
L
linxinfa 已提交
53 54 55 56 57 58
        }
#endif

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

L
linxinfa 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        }
        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目录(热更新目录)中查找资源
75
            bundle = AssetBundle.LoadFromFile(updatePath + abName);
L
linxinfa 已提交
76 77 78 79
        }
        else if (File.Exists(extPath + "/" + abName))
        {
            // 从拓展包目录加载资源
80
            bundle = AssetBundle.LoadFromFile(extPath + abName);
L
linxinfa 已提交
81 82 83
        }
        else
        {
84
            bundle = AssetBundle.LoadFromFile(internalPath + abName);
L
linxinfa 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98
        }

        /*
        // 如果对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 已提交
99
            GameLogger.Log("LoadAssetBundle Ok, abName: " + abName);
L
linxinfa 已提交
100
        }
101

L
linxinfa 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        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);
        }
118
        else if (uri.EndsWith(".spriteatlas"))
L
linxinfa 已提交
119 120 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
        {
            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>
    /// 内部资源目录
159 160 161 162
    /// 获取内部资源路径,对应streamingAssetsPath,
    /// 一般情况,streamingAssetsPath目录中的资源只能用WWW异步加载,但是AssetBundle提供了LoadFromFile方法可以直接同步加载
    /// 在Android中,Application.streamingAssetsPath会在前面加上"jar:file://"
    /// 所以不直接使用Application.streamingAssetsPath,而使用Application.dataPath + "!assets“
L
linxinfa 已提交
163 164 165 166 167 168
    /// </summary>
    /// <value></value>
    public string internalPath
    {
        get
        {
169 170 171
#if UNITY_ANDROID
                return Application.dataPath + "!assets/res/";
#else
L
linxinfa 已提交
172
            return Application.streamingAssetsPath + "/res/";
173
#endif
L
linxinfa 已提交
174 175 176 177 178 179 180
        }
    }

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

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

    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;
        }
    }
}