Exemple #1
0
    public static void LoadABTest()
    {
        TextAsset               textAsset       = UnityEditor.AssetDatabase.LoadAssetAtPath <TextAsset>(Application.dataPath + "LocalAssetBundleConfig.bytes");
        MemoryStream            memoryStream    = new MemoryStream(textAsset.bytes);
        BinaryFormatter         binaryFormatter = new BinaryFormatter();
        LocalAssetsBundleConfig config          = binaryFormatter.Deserialize(memoryStream) as LocalAssetsBundleConfig;

        memoryStream.Close();

        string path = "Assets/GameData/Prefabs/Attack.prefab";
        uint   crc  = Crc32.GetCrc32(path);

        LocalAssetBundleVO info = null;

        for (int i = 0; i < config.configList.Count; ++i)
        {
            if (config.configList[i].Crc == crc)
            {
                info = config.configList[i];
            }
        }

        for (int i = 0; i < info.ABDependce.Count; ++i)
        {
            AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + info.ABDependce[i]);
        }
        AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + info.BundleName);
        GameObject  obj         = GameObject.Instantiate(assetBundle.LoadAsset <GameObject>(info.AssetName));
    }
Exemple #2
0
    //创建AssetsBundle本地依赖表
    private static void CreateLocalConfig()
    {
        LocalAssetsBundleConfig config = new LocalAssetsBundleConfig();

        config.configList = new List <LocalAssetBundleVO>();

        foreach (string path in PathBundleNameDic.Keys)
        {
            LocalAssetBundleVO vo = new LocalAssetBundleVO();
            vo.Path       = path;
            vo.Crc        = Crc32.GetCrc32(path);
            vo.BundleName = PathBundleNameDic[path];
            vo.AssetName  = path.Remove(0, path.LastIndexOf("/") + 1);  //取最后一个/后的内容
            vo.ABDependce = new List <string>();

            string[] allDependce = AssetDatabase.GetDependencies(path);

            //Debug.LogError(allDependce.Length + "_Length");

            for (int i = 0; i < allDependce.Length; ++i)
            {
                if (allDependce[i] == path || path.EndsWith(".cs"))
                {
                    //Debug.LogError("continue");
                    continue;
                }

                string tempName = "";
                if (PathBundleNameDic.TryGetValue(allDependce[i], out tempName))
                {
                    //Debug.LogError("??????????");
                    //同一个ab包无须添加依赖
                    if (tempName == PathBundleNameDic[path])
                    {
                        //Debug.LogError("continue22222222222222222222");
                        continue;
                    }

                    //可能存在一个prefab引用某ab包下多个文件的情况
                    if (!vo.ABDependce.Contains(tempName))
                    {
                        //Debug.LogError("可能存在一个prefab引用某ab包下多个文件的情况");
                        vo.ABDependce.Add(tempName);
                    }
                }
            }
            config.configList.Add(vo);
        }

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

        if (File.Exists(xmlPath))
        {
            File.Delete(xmlPath);
        }

        FileStream    fs = new FileStream(xmlPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        StreamWriter  sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
        XmlSerializer xs = new XmlSerializer(config.GetType());

        xs.Serialize(sw, config);
        sw.Close();
        fs.Close();

        //写入二进制

        //二进制不需要路径 减少文件大小
        foreach (var info in config.configList)
        {
            info.Path = "";
        }


        string binaryPath = Application.dataPath + "/LocalAssetBundleConfig.bytes";
        //为什么删除二进制文件会出错
        //if (File.Exists(binaryPath))
        //{
        //    File.Delete(binaryPath);
        //}
        FileStream      binaryFs = new FileStream(binaryPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        BinaryFormatter bf       = new BinaryFormatter();

        bf.Serialize(binaryFs, config);
        binaryFs.Close();
    }