Exemplo n.º 1
0
    // Use this for initialization
    void Start()
    {
        //加载ab的
        AssetBundle abData    = AssetBundle.LoadFromFile(MPathUtils.ASSETBUNDLE_PATH + "/" + MPathUtils.ASSETBUNDLE_AB_DATA_NAME);
        TextAsset   textAsset = abData.LoadAsset <TextAsset>("MAssetBundleConfig.bytes");

        MemoryStream       memoryStream       = new MemoryStream(textAsset.bytes);
        BinaryFormatter    binaryFormatter    = new BinaryFormatter();
        MAssetBundleConfig mAssetBundleConfig = (MAssetBundleConfig)binaryFormatter.Deserialize(memoryStream);

        memoryStream.Close();

        string           prefabPath       = "Assets/Resources/UI/Prefabs/TestPrefab.prefab";
        uint             crc              = MCrcHelper.GetCRC32(prefabPath);
        MAssetBundleBase mAssetBundleBase = null;

        for (int i = 0; i < mAssetBundleConfig.AssetBundleList.Count; i++)
        {
            if (mAssetBundleConfig.AssetBundleList[i].Crc == crc)
            {
                mAssetBundleBase = mAssetBundleConfig.AssetBundleList[i];
            }
        }

        for (int i = 0; i < mAssetBundleBase.AbDependence.Count; i++)
        {
            AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + mAssetBundleBase.AbDependence[i]);
        }

        AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + mAssetBundleBase.AbName);
        GameObject  @object     = GameObject.Instantiate(assetBundle.LoadAsset <GameObject>(mAssetBundleBase.AssetName));
    }
Exemplo n.º 2
0
        //从AB文件中加载AB的所有信息文件
        public bool LoadAssetBundleConfig()
        {
            if (m_resourcesItemDic == null)
            {
                m_resourcesItemDic = new Dictionary <uint, MResourceItem>();
            }

            m_resourcesItemDic.Clear();
            string      abConfigPath = MPathUtils.ASSETBUNDLE_PATH + "/" + MPathUtils.ASSETBUNDLE_AB_DATA_NAME;
            AssetBundle abData       = AssetBundle.LoadFromFile(abConfigPath);

            if (abData != null)
            {
                TextAsset textAsset = abData.LoadAsset <TextAsset>(MPathUtils.ASSETBUNDLE_AB_BYTES_NAME);
                if (textAsset)
                {
                    MemoryStream       memoryStream       = new MemoryStream(textAsset.bytes);
                    BinaryFormatter    binaryFormatter    = new BinaryFormatter();
                    MAssetBundleConfig mAssetBundleConfig = (MAssetBundleConfig)binaryFormatter.Deserialize(memoryStream);
                    memoryStream.Close();

                    for (int i = 0; i < mAssetBundleConfig.AssetBundleList.Count; i++)
                    {
                        MAssetBundleBase mAssetBundleBase = mAssetBundleConfig.AssetBundleList[i];
                        MResourceItem    mResource        = new MResourceItem();
                        mResource.m_crc          = mAssetBundleBase.Crc;
                        mResource.m_abName       = mAssetBundleBase.AbName;
                        mResource.m_assetName    = mAssetBundleBase.AssetName;
                        mResource.m_path         = mAssetBundleBase.Path;
                        mResource.m_abDependence = mAssetBundleBase.AbDependence;
                        if (m_resourcesItemDic.ContainsKey(mResource.m_crc))
                        {
                            MDebug.singleton.AddErrorLog("已经添加了这个数据信息 :AbName " + mResource.m_abName + "资源名 " + mResource.m_assetName);
                        }
                        else
                        {
                            m_resourcesItemDic.Add(mResource.m_crc, mResource);
                        }
                    }
                    return(true);
                }
                else
                {
                    MDebug.singleton.AddErrorLog("这个AB文件中找不到这个资源 ResName " + MPathUtils.ASSETBUNDLE_AB_BYTES_NAME);
                    return(false);
                }
            }
            else
            {
                MDebug.singleton.AddErrorLog("请检查AB文件找不到这个Ab Path " + abConfigPath);
                return(false);
            }
        }
Exemplo n.º 3
0
    //数据导出
    public static void WriteToData(Dictionary <string, string> resPathDic)
    {
        MAssetBundleConfig mAssetBundleConfig = new MAssetBundleConfig();

        mAssetBundleConfig.AssetBundleList = new List <MAssetBundleBase>();
        foreach (var item in resPathDic)
        {
            string           nowPath          = item.Key;
            MAssetBundleBase mAssetBundleBase = new MAssetBundleBase();
            mAssetBundleBase.Path         = nowPath;
            mAssetBundleBase.Crc          = MCrcHelper.GetCRC32(nowPath);
            mAssetBundleBase.AbName       = item.Value;
            mAssetBundleBase.AssetName    = item.Key.Remove(0, nowPath.LastIndexOf("/") + 1);
            mAssetBundleBase.AbDependence = new List <string>();
            //先获取到这个路径的所有依赖项的路径 然后遍历依赖项
            string[] resDependences = AssetDatabase.GetDependencies(nowPath);
            for (int i = 0; i < resDependences.Length; i++)
            {
                string tempPath = resDependences[i];
                //如果当前遍历路径==查询路径 或者 是脚本文件
                if (tempPath == nowPath || tempPath.EndsWith(".cs"))
                {
                    continue;
                }

                //然后根据上文传进来的Dic 判定每一个依赖路径所对应的AB名字 进行添加
                string abName;
                if (resPathDic.TryGetValue(tempPath, out abName))
                {
                    if (abName == resPathDic[nowPath])
                    {
                        continue;
                    }
                    if (!mAssetBundleBase.AbDependence.Contains(abName))
                    {
                        mAssetBundleBase.AbDependence.Add(abName);
                    }
                }
            }
            //数据插入
            mAssetBundleConfig.AssetBundleList.Add(mAssetBundleBase);
        }

        //写入XML
        if (File.Exists(MPathUtils.ASSETBUNDLE_XML_PATH))
        {
            File.Delete(MPathUtils.ASSETBUNDLE_XML_PATH);
        }
        FileStream    fileStream    = new FileStream(MPathUtils.ASSETBUNDLE_XML_PATH, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        StreamWriter  streamWriter  = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
        XmlSerializer xmlSerializer = new XmlSerializer(mAssetBundleConfig.GetType());

        xmlSerializer.Serialize(streamWriter, mAssetBundleConfig);
        streamWriter.Close();
        fileStream.Close();

        //写入Bytes
        if (File.Exists(MPathUtils.ASSETBUNDLE_BYTES_PATH))
        {
            File.Delete(MPathUtils.ASSETBUNDLE_BYTES_PATH);
        }
        FileStream      bytesStresm     = new FileStream(MPathUtils.ASSETBUNDLE_BYTES_PATH, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        BinaryFormatter binaryFormatter = new BinaryFormatter();

        //写入二进制 不写入Path 优化Bytes大小
        for (int i = 0; i < mAssetBundleConfig.AssetBundleList.Count; i++)
        {
            mAssetBundleConfig.AssetBundleList[i].Path = null;
        }
        binaryFormatter.Serialize(bytesStresm, mAssetBundleConfig);
        bytesStresm.Close();
    }