Exemplo n.º 1
0
        void TestLoadAB()
        {
            //TextAsset textAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>(Application.streamingAssetsPath + "/AssetBundleConfig.bytes");

            AssetBundle configAB  = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/assetbundleconfig");
            TextAsset   textAsset = configAB.LoadAsset <TextAsset>("AssetBundleConfig");

            MemoryStream      stream            = new MemoryStream(textAsset.bytes);
            BinaryFormatter   bf                = new BinaryFormatter();
            AssetBundleConfig assetBundleConfig = (AssetBundleConfig)bf.Deserialize(stream);

            stream.Close();

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

            ABBase abBase = null;

            for (int i = 0; i < assetBundleConfig.ABList.Count; i++)
            {
                if (assetBundleConfig.ABList[i].Crc == crc)
                {
                    abBase = assetBundleConfig.ABList[i];
                }
            }

            for (int i = 0; i < abBase.ABDependency.Count; i++)
            {
                AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABDependency[i]);
            }
            AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abBase.ABName);
            GameObject  obj         = GameObject.Instantiate(assetBundle.LoadAsset <GameObject>(abBase.AssetName));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 加载AB配置表
        /// </summary>
        /// <returns></returns>
        public bool LoadAssetBundleConfig()
        {
            m_ResourceItemDict.Clear();


            string      configPath = Application.streamingAssetsPath + "/assetbundleconfig";
            AssetBundle configAB   = AssetBundle.LoadFromFile(configPath);
            TextAsset   textAsset  = configAB.LoadAsset <TextAsset>("assetbundleconfig");

            if (textAsset == null)
            {
                Debug.LogError("AssetBundleConfig is no exist!");
                return(false);
            }

            MemoryStream stream = new MemoryStream(textAsset.bytes);

            BinaryFormatter   bf     = new BinaryFormatter();
            AssetBundleConfig config = (AssetBundleConfig)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_AssetName    = abBase.AssetName;
                item.m_ABName       = abBase.ABName;
                item.m_DependABName = abBase.ABDependency;

                if (m_ResourceItemDict.ContainsKey(item.m_Crc))
                {
                    Debug.LogError("重复的Crc 资源名: " + item.m_AssetName + " ab包名: " + item.m_ABName);
                }
                else
                {
                    m_ResourceItemDict.Add(item.m_Crc, item);
                }
            }


            return(true);
        }
Exemplo n.º 3
0
        static void WriteData(Dictionary <string, string> resPathDic)
        {
            AssetBundleConfig config = new AssetBundleConfig();

            config.ABList = new List <ABBase>();
            foreach (var path in resPathDic.Keys)
            {
                ABBase abBase = new ABBase();
                abBase.Path      = path;
                abBase.Crc       = CRC32.GetCRC32(path);
                abBase.ABName    = resPathDic[path];
                abBase.AssetName = path.Remove(0, path.LastIndexOf("/") + 1);

                abBase.ABDependency = new List <string>();
                string[] resDependency = AssetDatabase.GetDependencies(path);
                for (int i = 0; i < resDependency.Length; i++)
                {
                    string tempPath = resDependency[i];
                    if (tempPath == path || path.EndsWith(".cs"))
                    {
                        continue;
                    }

                    string abName = "";

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

                        if (!abBase.ABDependency.Contains(abName))
                        {
                            abBase.ABDependency.Add(abName);
                        }
                    }
                }

                config.ABList.Add(abBase);
            }

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

            if (File.Exists(xmlPath))
            {
                File.Delete(xmlPath);
            }
            FileStream    fileStream = new FileStream(xmlPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
            StreamWriter  sw         = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
            XmlSerializer xs         = new XmlSerializer(config.GetType());

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

            // 写入二进制
            foreach (var abBase in config.ABList)
            {
                abBase.Path = "";
            }

            string bytePath = "Assets/GameData/Data/ABData/AssetBundleConfig.bytes";

            fileStream = new FileStream(bytePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
            BinaryFormatter bf = new BinaryFormatter();

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