Example #1
0
    /// <summary>
    /// 预加载所有AssetBundle资源
    /// </summary>
    public void LoadAllAssetBundle()
    {
        abResourceDict.Clear();

        AssetBundle ab        = AssetBundle.LoadFromFile(ConstConfig.ASSETBUNDLEPATH + "/assetbundleconfig");
        TextAsset   textAsset = ab.LoadAsset <TextAsset>("AssetBundleConfig.bytes");

        if (textAsset == null)
        {
            Debug.LogError("Asset丢失或未找到资源");
            return;
        }
        MemoryStream    stream = new MemoryStream(textAsset.bytes);
        BinaryFormatter bf     = new BinaryFormatter();
        ABBundleConfig  config = (ABBundleConfig)bf.Deserialize(stream);

        stream.Close();
        for (int i = 0; i < config.ABList.Count; i++)
        {
            ABBase       abBase = config.ABList[i];
            ResourceItem item   = new ResourceItem();
            item.m_Crc        = abBase.Crc;
            item.m_ABName     = abBase.ABName;
            item.m_AssetName  = abBase.AssetName;
            item.m_ABDependce = abBase.ABDependce;
            if (abResourceDict.ContainsKey(item.m_Crc))
            {
                Debug.LogError("存在重复的Crc, AB包名:" + item.m_ABName + " 资源名:" + item.m_AssetName);
            }
            else
            {
                abResourceDict.Add(item.m_Crc, item);
            }
        }
    }
Example #2
0
    /// <summary>
    /// 创建配置表
    /// </summary>
    static void CreateConfigTable(Dictionary <string, string> resultPathDic)
    {
        ABBundleConfig bundleConfig = new ABBundleConfig();

        bundleConfig.ABList = new List <ABBase>();
        foreach (string path in resultPathDic.Keys)
        {
            if (!ValidPath(path))
            {
                continue;
            }

            ABBase abBase = new ABBase();
            abBase.Path       = path;
            abBase.Crc        = CRC32.GetCRC32(path);
            abBase.ABName     = resultPathDic[path];
            abBase.AssetName  = path.Remove(0, path.LastIndexOf("/") + 1); //资源名是全路径名去掉最后一个'/'前面的字符串
            abBase.ABDependce = new List <string>();
            string[] allDependce = AssetDatabase.GetDependencies(path);
            for (int i = 0; i < allDependce.Length; i++)
            {
                string tempPath = allDependce[i];
                if (tempPath == path || path.EndsWith(".cs"))
                {
                    continue;
                }

                string abName = "";
                if (resultPathDic.TryGetValue(tempPath, out abName))
                {
                    if (abName == resultPathDic[path])
                    {
                        continue;
                    }

                    if (!abBase.ABDependce.Contains(abName))
                    {
                        abBase.ABDependce.Add(abName);
                    }
                }
            }
            bundleConfig.ABList.Add(abBase);
        }

        //写入xml
        string xmlPath = Application.dataPath + "/AssetBundleConfig.xml";

        if (File.Exists(xmlPath))
        {
            File.Delete(xmlPath);
        }
        FileStream    fs1           = new FileStream(xmlPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        StreamWriter  sw1           = new StreamWriter(fs1, System.Text.Encoding.UTF8);
        XmlSerializer xmlSerializer = new XmlSerializer(bundleConfig.GetType());

        xmlSerializer.Serialize(sw1, bundleConfig);
        sw1.Close();
        fs1.Close();

        //写入二进制

        if (File.Exists(ABCONFIGBINNERPATH))
        {
            File.Delete(ABCONFIGBINNERPATH);
        }
        FileStream      fs2 = new FileStream(ABCONFIGBINNERPATH, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        BinaryFormatter bf2 = new BinaryFormatter();

        bf2.Serialize(fs2, bundleConfig);
        fs2.Close();
        AssetDatabase.Refresh();
        SetABName("assetbundleconfig", ABCONFIGBINNERPATH);
    }