Esempio n. 1
0
    private static bool BuildSingleFileToAssetBundleTemp(string exportPath, string assetPath)
    {
        bool buildSingleFileAb = false;

        foreach (var path in BuildSingleFilePaths)
        {
            if (assetPath.StartsWith(path))
            {
                buildSingleFileAb = true;
                break;
            }
        }

        if (buildSingleFileAb)
        {
            var tempPath        = EditorFileUtility.GetPathWithoutExt(assetPath);
            var assetbundlePath = exportPath + "/temp/" + tempPath + ".pkg";
            if (HasBuildAssetBundleMapTemp.Contains(assetbundlePath))
            {
                return(true);
            }
            EditorFileUtility.CreateParentDirecotry(assetbundlePath);

            List <Object> objects    = new List <Object>();
            List <string> assetNames = new List <string>();
            objects.Add(AssetDatabase.LoadMainAssetAtPath(assetPath));
            assetNames.Add(assetPath);

            BuildAssetBundleWithoutCollectDependencies(objects.ToArray(), assetNames.ToArray(), assetbundlePath);
            HasBuildAssetBundleMapTemp.Add(assetbundlePath);
            return(true);
        }

        return(false);
    }
Esempio n. 2
0
    private static void BuildAssetBundleWithoutCollectDependencies(Object[] objs, string[] assetsName, string abPath)
    {
        for (int i = 0; i < assetsName.Length; i++)
        {
            assetsName[i] = EditorFileUtility.GetPathWithoutExt(assetsName[i]);
        }

        BuildPipeline.BuildAssetBundleExplicitAssetNames(objs, assetsName,
                                                         abPath,
                                                         BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.DeterministicAssetBundle,
                                                         AssetBundleBuildTarget);
    }
Esempio n. 3
0
    private static void GetAssetDependences(Queue <string> assetQueue, string path)
    {
        if (IsIgoreAsset(path) || GameAssets.ContainsKey(path))
        {
            return;
        }

        var assetInfo = new AssetInfo();

        assetInfo.Path = path;

        var dependencesList = new HashSet <string>();
        var dependences     = AssetDatabase.GetDependencies(new string[] { path });

        foreach (var dependencesPath in dependences)
        {
            if (dependencesPath == path)
            {
                continue;
            }

            if (IsIgoreAsset(dependencesPath))
            {
                continue;
            }

            dependencesList.Add(dependencesPath);
            assetQueue.Enqueue(dependencesPath);
        }

        assetInfo.ChildDependences = dependencesList;
        GameAssets.Add(path, assetInfo);

        var pathWithoutExt = EditorFileUtility.GetPathWithoutExt(path);

        if (AssetWithoutExtMap.ContainsKey(pathWithoutExt))
        {
            Debug.LogError("同一个目录下不应该有相同文件名,不同扩展名的资源命名存在:" + path);
        }
        else
        {
            AssetWithoutExtMap.Add(pathWithoutExt, true);
        }
    }
Esempio n. 4
0
    private static void ExportAssetDependences()
    {
        List <string>            stringBuff      = new List <string>();
        Dictionary <string, int> stringBuffIndex = new Dictionary <string, int>();

        foreach (var gameAsset in GameAssets)
        {
            var path = EditorFileUtility.GetPathWithoutExt(gameAsset.Value.Path);
            if (!stringBuffIndex.ContainsKey(path))
            {
                stringBuff.Add(path);
                stringBuffIndex.Add(path, stringBuff.Count - 1);
            }

            if (!stringBuffIndex.ContainsKey(gameAsset.Value.AssetBundlePath))
            {
                stringBuff.Add(gameAsset.Value.AssetBundlePath);
                stringBuffIndex.Add(gameAsset.Value.AssetBundlePath, stringBuff.Count - 1);
            }

            foreach (var dependence in gameAsset.Value.ChildDependences)
            {
                path = EditorFileUtility.GetPathWithoutExt(dependence);
                if (!stringBuffIndex.ContainsKey(path))
                {
                    stringBuff.Add(path);
                    stringBuffIndex.Add(path, stringBuff.Count - 1);
                }
            }
        }

        FileStream fs = File.Create(Application.dataPath + "/Resources/AssetsDependences.xml");

        //资源的数量
        var count = GameAssets.Count;
        var bytes = BitConverter.GetBytes(count);

        BitConverterUtility.ConvertToLittleEndian(bytes, 0, bytes.Length);
        fs.Write(bytes, 0, bytes.Length);

        //字符串的数量
        count = stringBuff.Count;
        bytes = BitConverter.GetBytes(count);
        BitConverterUtility.ConvertToLittleEndian(bytes, 0, bytes.Length);
        fs.Write(bytes, 0, bytes.Length);

        //写入字符串资源
        var buffsize = GetStringBufferSize(stringBuff);

        bytes = BitConverter.GetBytes(buffsize);
        BitConverterUtility.ConvertToLittleEndian(bytes, 0, bytes.Length);
        fs.Write(bytes, 0, bytes.Length);

        foreach (var str in stringBuff)
        {
            bytes = Encoding.UTF8.GetBytes(str);
            fs.Write(bytes, 0, bytes.Length);
            fs.WriteByte(0);
        }

        //写入每一个资源的条目
        foreach (var gameAsset in GameAssets)
        {
            var index = stringBuffIndex[EditorFileUtility.GetPathWithoutExt(gameAsset.Value.Path)];
            bytes = BitConverter.GetBytes(index);
            BitConverterUtility.ConvertToLittleEndian(bytes, 0, bytes.Length);
            fs.Write(bytes, 0, bytes.Length);

            index = stringBuffIndex[gameAsset.Value.AssetBundlePath];
            bytes = BitConverter.GetBytes(index);
            BitConverterUtility.ConvertToLittleEndian(bytes, 0, bytes.Length);
            fs.Write(bytes, 0, bytes.Length);

            count = gameAsset.Value.ChildDependences.Count;
            bytes = BitConverter.GetBytes(count);
            BitConverterUtility.ConvertToLittleEndian(bytes, 0, bytes.Length);
            fs.Write(bytes, 0, bytes.Length);

            foreach (var d in gameAsset.Value.ChildDependences)
            {
                index = stringBuffIndex[EditorFileUtility.GetPathWithoutExt(d)];
                bytes = BitConverter.GetBytes(index);
                BitConverterUtility.ConvertToLittleEndian(bytes, 0, bytes.Length);
                fs.Write(bytes, 0, bytes.Length);
            }
        }

        fs.Flush();
        fs.Close();

        AssetDatabase.Refresh();
        Object[] objs = new Object[1];
        objs[0] = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/AssetsDependences.xml");
        string[] assetsName = new string[] { "Assets/Resources/AssetsDependences" };
        BuildAssetBundle(objs, assetsName, Path.GetDirectoryName(Application.dataPath) + ExportDir + "/AssetsDependences.pkg");

        File.Delete(Application.dataPath + "/Resources/AssetsDependences.xml");
        File.Delete(Application.dataPath + "/Resources/AssetsDependences.xml.meta");
        AssetDatabase.Refresh();
    }
Esempio n. 5
0
    private static bool BuildSingleFileToAssetBundle(string exportPath, AssetInfo asset)
    {
        bool buildSingleFile = false;

        foreach (var path in BuildSingleFilePaths)
        {
            if (asset.Path.StartsWith(path))
            {
                buildSingleFile = true;
                break;
            }
        }

        if (buildSingleFile)
        {
            //判断是不是已经生成正常的AssetBundle
            var tempPath        = EditorFileUtility.GetPathWithoutExt(asset.Path);
            var assetbundlePath = exportPath + "/" + tempPath + ".pkg";
            if (HasBuildAssetBundleMap.Contains(assetbundlePath))
            {
                return(false);
            }
            EditorFileUtility.CreateParentDirecotry(assetbundlePath);

            //收集asset的所有依赖
            List <string> allChildDependences = new List <string>();
            foreach (var dependence in asset.ChildDependences)
            {
                if (asset.Path == dependence)
                {
                    continue;
                }
                allChildDependences.Add(dependence);
            }

            //删除临时目录的AssetBundle
            EditorFileUtility.DeleteDirectory(exportPath + "/temp");
            HasBuildAssetBundleMapTemp.Clear();

            BuildPipeline.PushAssetDependencies();

            foreach (var dependence in allChildDependences)
            {
                if (BuildAllFileToAssetBundleTemp(allChildDependences, exportPath, dependence))
                {
                    continue;
                }

                if (BuildSingleFileToAssetBundleTemp(exportPath, dependence))
                {
                    continue;
                }

                BuildCurrentDirectoryToAssetBundleTemp(allChildDependences, exportPath, dependence);
            }

            List <Object> objects    = new List <Object>();
            List <string> assetNames = new List <string>();

            objects.Add(AssetDatabase.LoadMainAssetAtPath(asset.Path));
            assetNames.Add(asset.Path);

            BuildPipeline.PushAssetDependencies();
            BuildAssetBundle(objects.ToArray(), assetNames.ToArray(), assetbundlePath);
            BuildPipeline.PopAssetDependencies();

            BuildPipeline.PopAssetDependencies();

            //配置asset的Assetbundle路径
            asset.AssetBundlePath = tempPath + ".pkg";
            HasBuildAssetBundleMap.Add(assetbundlePath);
            return(true);
        }

        return(false);
    }