Пример #1
0
        void LoadAssetBundleSync(AssetBundleContainer bundle, string url, int version, uint crc)
        {
            var realpath = Path.GetDirectoryName(url) + "/" + url.Substring(AssetBundleLoader.GetStreamingAssetsURL("").Length).Replace('/', '_');

// #if UNITY_ANDROID && !UNITY_EDITOR
//             //GetNextEntry    assets/Controller/MainPlayer.unity3d
//             //url             jar:file:///data/app/com.base.mayatest-1.apk!/assets/Controller/MainPlayer.unity3d
//
//             var apkPath = Application.dataPath;
//             var filepath = realpath.Substring(realpath.IndexOf("assets/"));
//             var filestream = new FileStream(apkPath, FileMode.Open, FileAccess.Read);
//             var zipfile = new ZipFile(filestream);
//             var item = zipfile.GetEntry(filepath);
//             if (null != item)
//             {
//                 var stream = zipfile.GetInputStream(item);
//                 byte[] buffer = new byte[stream.Length];
//                 stream.Read(buffer, 0, buffer.Length);
//                 AssetBundle assetBundle = AssetBundle.CreateFromMemoryImmediate(buffer);
//                 BundleAssetList list = null;
//                 list = assetBundle.Load("list", typeof(BundleAssetList)) as BundleAssetList;
//                 bundle.Initialize(realpath, assetBundle, list ? list.Assets : null);
//             }
//             else
//             {
//                 Logger.Error("get file from apk error !:" + realpath);
//             }
// #else
            // path = Application.dataPath + "/Raw";  IOS
            // path = Application.dataPath + "/StreamingAssets"; PC
            var path = realpath.Substring(realpath.IndexOf("file://") + 7);

            if (File.Exists(path))
            {
                var    stream = File.OpenRead(path);
                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                AssetBundle     assetBundle = AssetBundle.CreateFromMemoryImmediate(buffer);
                BundleAssetList list        = null;
                list = assetBundle.Load("list", typeof(BundleAssetList)) as BundleAssetList;
                bundle.Initialize(realpath, assetBundle, list ? list.Assets : null);
            }
            else
            {
                Logger.Error("get file error !:" + path);
            }
//#endif
        }
        static bool CreateAssetBundle(AssetBundleManageData data, AssetBundleData assetbundle, bool rebuild, bool dependency)
        {
            if (assetbundle.GetAllContents().Count <= 0)
            {
                return(false);
            }
            // add all path to list
            List <string> files = new List <string>();

            foreach (var path in assetbundle.GetAllContentsPath())
            {
                if (File.Exists(path))
                {
                    files.Add(path);
                }
            }
            // add special list asset for system
            BundleAssetList list     = null;
            string          listpath = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(data)) + "/list/" + assetbundle.File + "/list.asset";   //Application.temporaryCachePath + "/" + "list.asset";

            if (!assetbundle.ForStreamedScene)
            {
                list        = CreateInstance <BundleAssetList>();
                list.Assets = files.Select(file => {
                    FileInfo info         = new FileInfo(file);
                    BundleAssetInfo asset = new BundleAssetInfo(info.Name);
                    return(asset);
                }).ToArray();

                var directory = System.IO.Path.GetDirectoryName(listpath).Substring("Assets".Length);
                System.IO.Directory.CreateDirectory(Application.dataPath + directory);
                AssetDatabase.CreateAsset(list, listpath);
                //files.Add(listpath);
                AssetBundleUtility.AssetList = list;
                // BundleAssetList.cs(not exist file "BundleAssetList.cs" with DLL)
                //string bundleAssetsListPath = "Assets/" + Path.GetDirectoryName(AssetBundleManager.SettingDataPath) + "/Scripts/BundleAssetList.cs";
                //files.Add(bundleAssetsListPath);
            }
            var targets = new BuildTarget[] { EditorUserBuildSettings.activeBuildTarget };

#if !DLL
            if (assetbundle.PlatformFolder && data.multiPlatform && 0 < data.targets.Count)
            {
                targets = data.targets.ToArray();
            }
#endif

            bool          ret          = false;
            List <string> platformList = new List <string>();
            foreach (BuildTarget target in targets)
            {
                var path = assetbundle.Path.Replace(AssetBundleManager.PlatformFolder, AssetBundleManager.GetPlatformFolder(Application.platform));
#if !DLL
                if (assetbundle.PlatformFolder)
                {
                    var platformFolder = AssetBundleManager.GetPlatformFolder(target);
                    if (platformList.Contains(platformFolder))
                    {
                        continue;
                    }
                    platformList.Add(platformFolder);
                    path = GetFullPath(assetbundle, target);
                }
#endif
                var dir       = Path.GetDirectoryName(path);
                var assetName = Path.GetFileName(path);
                if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);                    //AssetDatabase.CreateFolder(Path.GetDirectoryName(dir), Path.GetFileName(dir));
                }
                if (!string.IsNullOrEmpty(dir))
                {
                    dir = dir + "/";
                }

                if (!assetbundle.ForStreamedScene)
                {
                    if (!assetbundle.Separated)
                    {
                        Debug.Log(nestString + "Build " + assetbundle.File);

                        /*if( AssetBundleUtilityNew.CreateBundle(files.ToArray(), dir, assetName, (BuildAssetBundleOptions)assetbundle.Options, target, rebuild | assetbundle.Changed) )
                         * {
                         *      ret = true;
                         * }*/
                        //Hoavq change to new build system
                        AssetBundleBuild[] buildMap = new AssetBundleBuild[1];

                        /*buildMap[0].assetBundleName = assetName;
                         * Debug.Log("There're " + files.ToArray().Length + " file in " + assetName + " bundle. Created in " + dir);
                         * buildMap[0].assetNames = files.ToArray();*/

                        AddAssetBundleToBuildMap(buildMap, 0, data, assetbundle);
                        BuildPipeline.BuildAssetBundles(dir, buildMap, (BuildAssetBundleOptions)assetbundle.Options, target);
                    }
                }
            }
            assetbundle.Changed = false;
            return(ret);
        }
        static bool CreateAssetBundle(AssetBundleManageData data, AssetBundleData assetbundle, bool rebuild, bool dependency)
        {
            if (assetbundle.GetAllContents().Count <= 0)
            {
                return(false);
            }
            // add all path to list
            List <string> files = new List <string>();

            foreach (var path in assetbundle.GetAllContentsPath())
            {
                if (File.Exists(path))
                {
                    files.Add(path);
                }
            }
            // add special list asset for system
            BundleAssetList list     = null;
            string          listpath = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(data)) + "/list/" + assetbundle.File + "/list.asset";   //Application.temporaryCachePath + "/" + "list.asset";

            if (!assetbundle.ForStreamedScene)
            {
                list        = CreateInstance <BundleAssetList>();
                list.Assets = files.Select(file => {
                    FileInfo info         = new FileInfo(file);
                    BundleAssetInfo asset = new BundleAssetInfo(info.Name);
                    return(asset);
                }).ToArray();

                var directory = System.IO.Path.GetDirectoryName(listpath).Substring("Assets".Length);
                System.IO.Directory.CreateDirectory(Application.dataPath + directory);
                AssetDatabase.CreateAsset(list, listpath);
                //files.Add(listpath);
                AssetBundleUtility.AssetList = list;
                // BundleAssetList.cs(not exist file "BundleAssetList.cs" with DLL)
                //string bundleAssetsListPath = "Assets/" + Path.GetDirectoryName(AssetBundleManager.SettingDataPath) + "/Scripts/BundleAssetList.cs";
                //files.Add(bundleAssetsListPath);
            }
            var targets = new BuildTarget[] { EditorUserBuildSettings.activeBuildTarget };

#if !DLL
            if (assetbundle.PlatformFolder && data.multiPlatform && 0 < data.targets.Count)
            {
                targets = data.targets.ToArray();
            }
#endif

            bool          ret          = false;
            List <string> platformList = new List <string>();
            foreach (BuildTarget target in targets)
            {
                var path = assetbundle.Path.Replace(AssetBundleManager.PlatformFolder, AssetBundleManager.GetPlatformFolder(Application.platform));
#if !DLL
                if (assetbundle.PlatformFolder)
                {
                    var platformFolder = AssetBundleManager.GetPlatformFolder(target);
                    if (platformList.Contains(platformFolder))
                    {
                        continue;
                    }
                    platformList.Add(platformFolder);
                    path = GetFullPath(assetbundle, target);
                }
#endif
                var dir = Path.GetDirectoryName(path);
                if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);                    //AssetDatabase.CreateFolder(Path.GetDirectoryName(dir), Path.GetFileName(dir));
                }
                if (!string.IsNullOrEmpty(dir))
                {
                    dir = dir + "/";
                }

                if (!assetbundle.ForStreamedScene)
                {
                    if (!assetbundle.Separated)
                    {
                        Debug.Log(nestString + "Build " + assetbundle.File);
                        if (AssetBundleUtility.CreateBundle(files.ToArray(), path, (BuildAssetBundleOptions)assetbundle.Options, target, rebuild | assetbundle.Changed))
                        {
                            ret = true;
                        }
                    }
                    else
                    {
                        var infos = new BundleAssetInfo[] { new BundleAssetInfo("") };
                        list.Assets = infos;
                        AssetBundleUtility.AssetList = list;
                        foreach (var file in files)
                        {
                            infos[0].Name = file;
                            string name = Path.GetFileNameWithoutExtension(file) + ".unity3d";
                            Debug.Log(nestString + "Build " + name);
                            if (AssetBundleUtility.CreateBundle(file, dir + name, (BuildAssetBundleOptions)assetbundle.Options, target, rebuild | assetbundle.Changed))
                            {
                                ret = true;
                            }
                            BuildPipeline.PopAssetDependencies();
                            BuildPipeline.PushAssetDependencies();
                        }
                    }
                }
                else
                {
                    if (!assetbundle.Separated)
                    {
                        Debug.Log(nestString + "Build " + assetbundle.File);
                        if (AssetBundleUtility.CreateSceneBundle(files.ToArray(), path, (BuildAssetBundleOptions)assetbundle.Options, target, rebuild | assetbundle.Changed))
                        {
                            ret = true;
                        }
                    }
                    else
                    {
                        foreach (var file in files)
                        {
                            string name = Path.GetFileNameWithoutExtension(file) + ".unity3d";
                            Debug.Log(nestString + "Build " + name);
                            if (AssetBundleUtility.CreateSceneBundle(file, dir + name, (BuildAssetBundleOptions)assetbundle.Options, target, rebuild | assetbundle.Changed))
                            {
                                ret = true;
                            }
                            BuildPipeline.PopAssetDependencies();
                            BuildPipeline.PushAssetDependencies();
                        }
                    }
                }
            }
            assetbundle.Changed = false;
            //AssetDatabase.DeleteAsset(listpath);	Child assetbundle require list.asset of parent assetbundle.
            //AssetDatabase.Refresh();
            return(ret);
        }
Пример #4
0
        IEnumerator LoadAssetBundle(AssetBundleContainer bundle, string url, int version, uint crc)
        {
            WWW www = null;

            var path = Path.Combine(Path.GetDirectoryName(url), url.Substring(AssetBundleLoader.GetStreamingAssetsURL("").Length).Replace('/', '_'));

            path = path.Replace("\\", "/");

            if (0 <= version)
            {
                www = WWW.LoadFromCacheOrDownload(path, version, crc);
            }
            else
            {
                www = new WWW(path);
            }
            yield return(www);

            if (www.error != null)
            {
                Debug.Log(string.Format("AssetBundleManager:\n\tload \"{0}\" error:{1}", path, www.error));
                bundle.Initialize(path, null, null);
                bundle.SetError(www.error);
            }
            else
            {
                BundleAssetList list = null;
                if (Application.HasProLicense())
                {
#if UNITY_5
                    var request = www.assetBundle.LoadAssetAsync <BundleAssetList>("list");
#else
                    var request = www.assetBundle.LoadAsync("list", typeof(BundleAssetList));
#endif
                    yield return(request);

                    list = request.asset as BundleAssetList;
                }
                else
                {
#if UNITY_5
                    list = www.assetBundle.LoadAsset <BundleAssetList>("list");
#else
                    list = www.assetBundle.Load("list", typeof(BundleAssetList)) as BundleAssetList;
#endif
                }
                if (list)
                {
                    bundle.Initialize(path, www.assetBundle, list.Assets);
                }
                else
                {
                    //Debug.LogWarning( "There is no BundleAssetList in " + www.url );
                    bundle.Initialize(path, www.assetBundle, null);
                }
            }
            if (www != null)
            {
                www.Dispose();
            }
        }