Exemple #1
0
        public static void ClassInitialize(TestContext context)
        {
            var file = @".\Data\bundle_5.1.unity3d";
            var abr  = new AssestFile(file);

            _header = abr.Header;
        }
Exemple #2
0
    static void SaveAssetBundleOutputFile(AssetBundleHeader header, string assetBundleFilePath, string outputFilePath)
    {
        string outputFileDir = Path.GetDirectoryName(outputFilePath);

        Directory.CreateDirectory(outputFileDir);

        var oldOutputFileInfo = new FileInfo(outputFilePath);

        if (oldOutputFileInfo.Exists)
        {
            oldOutputFileInfo.IsReadOnly = false;
        }
        using (FileStream fs = File.Open(outputFilePath, FileMode.Create, FileAccess.Write))
        {
            header.Save(fs);
            byte[] bundleContent = File.ReadAllBytes(assetBundleFilePath);
            fs.Write(bundleContent, 0, bundleContent.Length);
        }
    }
Exemple #3
0
    public static void BuildToOneBundle(Object realass, string path, bool recursive)
    {
        if (CheckModify && recursive)
        {
            if (!CheckAssetModify(realass, path, true))
            {
                return;
            }
        }

        int AssetCount = 0;
        List <AssetBundleHeader.DepInfo> ls = new List <AssetBundleHeader.DepInfo>();
        string asspath = AssetDatabase.GetAssetPath(realass);

        if (recursive)
        {
            string[] deppaths = AssetDatabase.GetDependencies(new string[] { asspath });
            Dictionary <string, bool> realdeps = new Dictionary <string, bool>();
            foreach (string realdep in deppaths)
            {
                realdeps[realdep] = true;
            }

            Object[] depobjs = EditorUtility.CollectDependencies(new Object[] { realass });
            BuildPipeline.PushAssetDependencies();
            AssetCount++;

            for (int i = 0; i < depobjs.Length; i++)
            {
                Object dep = depobjs[i];
                if (ReferenceEquals(dep, realass))
                {
                    continue;
                }

                string texpath = AssetDatabase.GetAssetPath(dep);
                if (!realdeps.ContainsKey(texpath))
                {
                    continue;
                }

                if (dep is Texture || dep is Shader || dep is MonoScript || dep is Font)
                {
                    if (!texpath.ToLower().EndsWith(".asset") && !string.IsNullOrEmpty(texpath))
                    {
                        BuildToOneBundle(dep, path, false);
                        AssetBundleHeader.DepInfo info = new AssetBundleHeader.DepInfo();
                        info.name = "";
                        string realname = ConvertFileName(texpath) + ".u3dext";
                        info.path = realname.ToLower();
                        ls.Add(info);
                    }
                }
            }

            BuildPipeline.PushAssetDependencies();
            AssetCount++;
        }

        string assetpath = ConvertFileName(Path.GetDirectoryName(asspath));
        string outpath;

        if (asspath != "")
        {
            outpath = path + "/" + assetpath;
        }
        else
        {
            outpath = assetpath;
        }

        Directory.CreateDirectory(outpath);
        string guid    = AssetDatabase.AssetPathToGUID(asspath);
        string outfile = outpath + "/" + guid;

        BuildAssetBundleOptions option;

        option  = BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies;
        option |= BuildAssetBundleOptions.UncompressedAssetBundle;

        bool suc = BuildPipeline.BuildAssetBundleExplicitAssetNames(new Object[] { realass }, new string[] { "1" }, outfile,
                                                                    option, ExportAssetBundlesHelper.CurBuildTarget);

        // do not compress font
        bool ForceSep = realass is Font;
        bool NeedSep  = false;

        if (suc && !ForceSep)
        {
            FileInfo fi = new FileInfo(outfile);

            if (realass is AudioClip)
            {
                if (fi.Length > min_audio_clip_bundel_size)
                {
                    NeedSep = true;
                }
            }
            else if (fi.Length > min_compress_bundle_size)
            {
                option = BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies;
                suc    = BuildPipeline.BuildAssetBundleExplicitAssetNames(new Object[] { realass }, new string[] { "1" }, outfile,
                                                                          option, ExportAssetBundlesHelper.CurBuildTarget);
                Debug.LogWarning("Big bundle: " + outfile + " Origin Size: " + fi.Length);
            }
        }

        for (int n = 0; n < AssetCount; n++)
        {
            BuildPipeline.PopAssetDependencies();
        }

        if (suc)
        {
            byte[] content  = File.ReadAllBytes(outfile);
            string outfile2 = outpath + "/" + Path.GetFileName(asspath) + ".u3dext";
            using (FileStream fs = File.Open(outfile2, FileMode.Create, FileAccess.Write))
            {
                AssetBundleHeader bh = new AssetBundleHeader();
                if (ForceSep || NeedSep)
                {
                    bh.option |= AssetBundleHeader.BundleOption.SepFile;
                }
                if (realass is Shader || realass is MonoScript || realass is Font)
                {
                    bh.option |= AssetBundleHeader.BundleOption.DonotRelease;
                }
                bh.deps = ls.ToArray();
                bh.Save(fs);
                fs.Write(content, 0, content.Length);
            }

            File.Delete(outfile);
        }
        else
        {
            Debug.LogError("BuildAssetBundleExplicitAssetNames");
        }
    }
Exemple #4
0
    private static void DetermineAssetBundleOption(AssetBundleManifest manifest, string assetBundleName, string cacheDir, out bool needCompress, out AssetBundleHeader header)
    {
        Object asset = AssetDatabase.LoadMainAssetAtPath("assets/" + assetBundleName);

        needCompress = false;
        header       = new AssetBundleHeader();

        //确定 needCompress, SepFile
        if (asset is Font)
        {
            header.option |= AssetBundleHeader.BundleOption.SepFile;
        }
        else
        {
            FileInfo fi = new FileInfo(cacheDir + "/uncompressed/assets/" + assetBundleName);

            if (asset is AudioClip)
            {
                if (fi.Length > min_audio_clip_bundel_size)
                {
                    header.option |= AssetBundleHeader.BundleOption.SepFile;
                }
            }
            else
            {
                if (fi.Length > min_compress_bundle_size)
                {
                    needCompress = true;
                }
            }
        }

        //确定其他选项
        string[] depBundleNames = manifest.GetDirectDependencies(assetBundleName);
        System.Array.Sort(depBundleNames);
        if (asset is Material)
        {
            header.specialType = AssetBundleHeader.BundleSpecialType.Material;

            header.deps = CalcMaterialDeps(assetBundleName, asset as Material, manifest);
        }
        else
        {
            if (asset is Texture)
            {
                header.option     |= AssetBundleHeader.BundleOption.ManuallyResolve;
                header.specialType = AssetBundleHeader.BundleSpecialType.Texture;
            }
            else if (asset is Shader)
            {
                header.option |= AssetBundleHeader.BundleOption.DonotRelease;
            }
            else if (asset is MonoScript)
            {
                header.option |= AssetBundleHeader.BundleOption.DonotRelease;
            }
            else if (asset is Font)
            {
                header.option |= AssetBundleHeader.BundleOption.DonotRelease;
            }

            header.deps = CalcNormalDeps(assetBundleName, asset, manifest);
        }
    }
Exemple #5
0
    static void BuildOneAssetBundle(string expcur, Dictionary <string, int> objmark, Dictionary <string, List <string> > allobjchild, string path, ref int depcount)
    {
        // 假设已经输出则跳过
        if (objmark.ContainsKey(expcur))
        {
            return;
        }

        List <string> subs = allobjchild[expcur];

        if (subs != null)
        {
            foreach (string sub in subs)
            {
                // 假设已经输出则跳过
                if (objmark.ContainsKey(sub))
                {
                    continue;
                }

                BuildOneAssetBundle(sub, objmark, allobjchild, path, ref depcount);
            }
        }

        objmark[expcur] = 1;

        string asspath = expcur;

        if (string.IsNullOrEmpty(asspath))
        {
            return;
        }

        string guid      = AssetDatabase.AssetPathToGUID(asspath);
        string assetpath = ConvertFileName(Path.GetDirectoryName(asspath));
        string outpath;

        if (asspath != "")
        {
            outpath = path + "/" + assetpath;
        }
        else
        {
            outpath = assetpath;
        }

        Directory.CreateDirectory(outpath);

        //string outfile = Path.Combine(outpath, Path.GetFileNameWithoutExtension(asspath));
        string outfile = outpath + "/" + guid;

        BuildPipeline.PushAssetDependencies();
        depcount++;
        Object realass = AssetDatabase.LoadMainAssetAtPath(expcur);

        if (realass == null)
        {
            return;
        }

        Object[] depobjs = EditorUtility.CollectDependencies(new Object[] { realass });
        Dictionary <string, int> deppathmap = new Dictionary <string, int>();

        foreach (Object depobj in depobjs)
        {
            string deppath = AssetDatabase.GetAssetPath(depobj);

            if (string.IsNullOrEmpty(deppath))
            {
                continue;
            }

            deppathmap[deppath] = 1;
        }

        List <string> realsubs = new List <string>();

        if (subs != null)
        {
            foreach (string sub in subs)
            {
                if (deppathmap.ContainsKey(sub))
                {
                    string realname = ConvertFileName(sub) + ".u3dext";
                    realsubs.Add(realname.ToLower());
                }
            }
        }

        BuildAssetBundleOptions option;

        option  = BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies;
        option |= BuildAssetBundleOptions.UncompressedAssetBundle;

        bool suc = BuildPipeline.BuildAssetBundleExplicitAssetNames(new Object[] { realass }, new string[] { "1" }, outfile,
                                                                    option, ExportAssetBundlesHelper.CurBuildTarget);

        Debug.Log("src file: " + asspath + " " + depcount);

        if (/*realass is MonoScript || */ !deppathmap.ContainsKey(expcur))
        {
            File.Delete(outfile);
        }
        else if (suc)
        {
            // do not compress font
            bool ForceSep = realass is Font;
            bool NeedSep  = false;
            if (!ForceSep)
            {
                FileInfo fi = new FileInfo(outfile);

                if (realass is AudioClip)
                {
                    if (fi.Length > min_audio_clip_bundel_size)
                    {
                        NeedSep = true;
                    }
                }
                else if (fi.Length > min_compress_bundle_size)
                {
                    option = BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies;
                    suc    = BuildPipeline.BuildAssetBundleExplicitAssetNames(new Object[] { realass }, new string[] { "1" }, outfile,
                                                                              option, ExportAssetBundlesHelper.CurBuildTarget);
                    Debug.LogWarning("Big bundle: " + outfile + " Origin Size: " + fi.Length);
                }
            }

            byte[] content  = File.ReadAllBytes(outfile);
            string outfile2 = outpath + "/" + Path.GetFileName(asspath) + ".u3dext";
            _depmap[outfile2.ToLower()] = 1;
            var oldFileInfo = new FileInfo(outfile2);
            if (oldFileInfo.Exists)
            {
                oldFileInfo.IsReadOnly = false;
            }
            using (FileStream fs = File.Open(outfile2, FileMode.Create, FileAccess.Write))
            {
                AssetBundleHeader bh = new AssetBundleHeader();
                bool bDefault        = true;
                if (ForceSep || NeedSep)
                {
                    bh.option |= AssetBundleHeader.BundleOption.SepFile;
                }

                if (realass is Material)
                {
                    bh.specialType = AssetBundleHeader.BundleSpecialType.Material;

                    Material mt = realass as Material;

                    if (mt.shader != null)
                    {
                        bDefault = false;
                        List <string> pnames  = new List <string>();
                        int           nPCount = ShaderUtil.GetPropertyCount(mt.shader);

                        for (int n = 0; n < nPCount; n++)
                        {
                            ShaderUtil.ShaderPropertyType spt = ShaderUtil.GetPropertyType(mt.shader, n);

                            if (spt == ShaderUtil.ShaderPropertyType.TexEnv)
                            {
                                string pn = ShaderUtil.GetPropertyName(mt.shader, n);
                                pnames.Add(pn);
                            }
                        }

                        List <AssetBundleHeader.DepInfo> deplist = new List <AssetBundleHeader.DepInfo>();
                        foreach (var realsub in realsubs)
                        {
                            bool findtex = false;
                            foreach (var texname in pnames)
                            {
                                Texture tex = mt.GetTexture(texname);

                                if (tex)
                                {
                                    string texpath = AssetDatabase.GetAssetPath(tex);

                                    if (!string.IsNullOrEmpty(texpath))
                                    {
                                        string realpath = ConvertFileName(texpath) + ".u3dext";
                                        realpath = realpath.ToLower();

                                        if (realpath == realsub)
                                        {
                                            AssetBundleHeader.DepInfo info = new AssetBundleHeader.DepInfo();
                                            info.name = texname;
                                            info.path = realsub;
                                            deplist.Add(info);
                                            findtex = true;
                                        }
                                    }
                                }
                            }

                            if (!findtex)
                            {
                                AssetBundleHeader.DepInfo info = new AssetBundleHeader.DepInfo();
                                info.name = "";
                                info.path = realsub;
                                deplist.Add(info);
                            }
                        }

                        bh.deps = deplist.ToArray();
                    }
                }
                else if (realass is Texture)
                {
                    bh.option     |= AssetBundleHeader.BundleOption.ManuallyResolve;
                    bh.specialType = AssetBundleHeader.BundleSpecialType.Texture;
                }
                else if (realass is Shader)
                {
                    bh.option |= AssetBundleHeader.BundleOption.DonotRelease;
                }
                else if (realass is MonoScript)
                {
                    bh.option |= AssetBundleHeader.BundleOption.DonotRelease;
                }
                else if (realass is Font)
                {
                    bh.option |= AssetBundleHeader.BundleOption.DonotRelease;
                }

                if (bDefault)
                {
                    bh.deps = new AssetBundleHeader.DepInfo[realsubs.Count];

                    for (int n = 0; n < realsubs.Count; n++)
                    {
                        bh.deps[n].name = "";
                        bh.deps[n].path = realsubs[n];
                    }
                }

                bh.Save(fs);
                fs.Write(content, 0, content.Length);
            }

            File.Delete(outfile);
        }
    }