棋牌手游前后端开发教程-0001AssetBundle打包

我们需要创建一个Assets目录下创建一个新的目录,名为Editor/CustomMenu/AssetBundle,然后我们在这个目录下创建一个名为AssetBundleTool的脚本,看图

棋牌手游前后端开发教程-0001AssetBundle打包_第1张图片

我们需要创建一个资源目录,同样在Assets目录下创建一个新的目录,名为UI/Prefasb/Views,再看图棋牌手游前后端开发教程-0001AssetBundle打包_第2张图片

 

这时候我们先创建一个简单的界面,这里我们使用的是ugui,为什么使用ugui,哪有那么多为什么,就是懒得去导入ngui了,是的我就是这么懒,所以随时可能会断更,大家不要太在意,具体的如下

文字描述:创建一个Panel,然后改名为FirstView,然后再创建一个Text,嗯,这里就不替它取名了

图片描述:

棋牌手游前后端开发教程-0001AssetBundle打包_第3张图片

棋牌手游前后端开发教程-0001AssetBundle打包_第4张图片

棋牌手游前后端开发教程-0001AssetBundle打包_第5张图片

界面创建完毕了,我们要把他搞成预制体,实际上就是按住FirstView,然后拖到UI/Prefasb/Views目录下,如下图

 

棋牌手游前后端开发教程-0001AssetBundle打包_第6张图片

 

 

好了,接下来就是代码部分了,我们打开刚刚的创建的AssetBundleTool,然后代码如下:

   public static string sourcePath = Application.dataPath + "/UI/Prefabs";

   private static string GetPlatformFolderByTarget(BuildTarget target)

    {

        switch (target)

        {

            case BuildTarget.iOS:

                return "Assets/AllResources/ios";

            case BuildTarget.Android:

                return "Assets/AllResources/android";

            default:

                return "Assets/AllResources/other";

        }

    }

    [MenuItem("Resource/一键打包/打包UI/所有")]

    public static void BuildAssetBundle()

    {

        string path = GetPlatformFolderByTarget(EditorUserBuildSettings.activeBuildTarget) + "/Resource";

        Pack(sourcePath);

        if (!Directory.Exists(path))

        {

            Directory.CreateDirectory(path);

        }

        BuildPipeline.BuildAssetBundles(path, 0, EditorUserBuildSettings.activeBuildTarget);

        AssetDatabase.Refresh();

        Debug.Log("打包完成");

    }

    private static void Pack(string source)

    {

        DirectoryInfo folder = new DirectoryInfo(source);

        FileSystemInfo[] files = folder.GetFileSystemInfos();

        int length = files.Length;

        for (int i = 0; i < length; i++)

        {

            if (files[i] is DirectoryInfo)

            {

                Pack(files[i].FullName);

            }

            else

            {

                if (!files[i].Name.EndsWith(".meta"))

                {

                    file(files[i].FullName);

                }

            }

        }

}

    private  static void file(string source)

    {

        string _source = source.Replace("\\", "/");

        string _assetPath = "Assets" + _source.Substring(Application.dataPath.Length);

        string assetName = _assetPath.Substring(_assetPath.LastIndexOf("/") + 1).Replace(Path.GetExtension(_assetPath), "");

        AssetImporter assetImporter = AssetImporter.GetAtPath(_assetPath);

        assetImporter.assetBundleName = assetName;

    }

如果打包成功,则会自动在Assets\AllResources下生成指定的目录以及文件,如图

棋牌手游前后端开发教程-0001AssetBundle打包_第7张图片

 

接着我们提交文件到包内容,代码如下

 

private static string GetResourceFolderByTarget(BuildTarget target)

    {

        switch (target)

        {

            case BuildTarget.iOS:

                return Application.dataPath+"/AllResources/android/Resource/";

            case BuildTarget.Android:

                return Application.dataPath+"/AllResources/ios/Resource/";

            default:

                return Application.dataPath+"/AllResources/other/Resource/";

        }

    }

    [MenuItem("Resource/提交UI文件到包内容")]

    public static void SendAllFiles()

    {

        string resourcePath = GetResourceFolderByTarget(EditorUserBuildSettings.activeBuildTarget);

        string targetPath = Application.dataPath + "/StreamingAssets/Resource/";

        if (Directory.Exists(targetPath))

        {

            List oldfilesList = GetThisDirectoriesFiles(targetPath);

            foreach (string filePath in oldfilesList)

            {

                File.Delete(filePath);

            }

            Directory.Delete(targetPath);

        }

        List filesList = GetThisDirectoriesFiles(resourcePath);

        foreach (string file in filesList)

        {

            string extension = Path.GetExtension(file);

            if (extension == ".meta")

            {

                continue;

            }

            string thisFileName = file.Replace(resourcePath, "");

            string willToFileName = targetPath + thisFileName;

            string dirName = Path.GetDirectoryName(willToFileName);

            if (!Directory.Exists(dirName))

            {

                Directory.CreateDirectory(dirName);

            }

            File.Copy(file, willToFileName);

        }

        AssetDatabase.Refresh();

    }

    private static List GetThisDirectoriesFiles(string path)

    {

        List fileList = new List();

        string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);

        fileList.AddRange(new List(files));

        return fileList;

  }

如果提交成功,则会自动生成StreamingAssets,如下图所示:

棋牌手游前后端开发教程-0001AssetBundle打包_第8张图片

 

你可能感兴趣的:(棋牌手游前后端开发教程-0001AssetBundle打包)