Esempio n. 1
0
    public static void BuildToOneBundle(Object realass, string path, bool recursive)
    {
        if (CheckModify && recursive)
        {
            if (!CheckAssetModify(realass, path, true))
                return;
        }

        int AssetCount = 0;
        List<ECAssetBundleHeader.DepInfo> ls = new List<ECAssetBundleHeader.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);
                        ECAssetBundleHeader.DepInfo info = new ECAssetBundleHeader.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, ExportAssetBundles.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, ExportAssetBundles.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))
            {
                ECAssetBundleHeader bh = new ECAssetBundleHeader();
                if (ForceSep || NeedSep)
                    bh.option |= ECAssetBundleHeader.BundleOption.SepFile;
                if (realass is Shader || realass is MonoScript || realass is Font)
                    bh.option |= ECAssetBundleHeader.BundleOption.DonotRelease;
                bh.deps = ls.ToArray();
                bh.Save(fs);
                fs.Write(content, 0, content.Length);
            }

            File.Delete(outfile);
        }
        else
            Debug.LogError("BuildAssetBundleExplicitAssetNames");
    }
Esempio n. 2
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, ExportAssetBundles.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, ExportAssetBundles.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))
            {
                ECAssetBundleHeader bh = new ECAssetBundleHeader();
                bool bDefault = true;
                if (ForceSep || NeedSep)
                    bh.option |= ECAssetBundleHeader.BundleOption.SepFile;

                if (realass is Material)
                {
                    bh.specialType = ECAssetBundleHeader.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<ECAssetBundleHeader.DepInfo> deplist = new List<ECAssetBundleHeader.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)
                                        {
                                            ECAssetBundleHeader.DepInfo info = new ECAssetBundleHeader.DepInfo();
                                            info.name = texname;
                                            info.path = realsub;
                                            deplist.Add(info);
                                            findtex = true;
                                        }
                                    }
                                }
                            }

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

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

                if (bDefault)
                {
                    bh.deps = new ECAssetBundleHeader.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);
        }
    }