您的当前位置:首页正文

Unity导出AssetBundle到指定路径

2024-11-13 来源:个人技术集锦
 1 using System.Collections;
 2 using UnityEngine;
 3 using UnityEditor;
 4 using System.IO;
 5 
 6 /// <summary>
 7 /// 把工程中设置了AssetBundle Name的资源打包成.unity3d 到StreamingAssets目录下
 8 /// </summary>
 9 public class ExportAssetBundle : EditorWindow
10 {
11     // public static string sourcePath = Application.dataPath + "/Resources";
12     private string OutputPath = "Assets/StreamingAssets";
13 
14     [MenuItem("AssetsManager/导出所有AssetBundle")]
15     static void AddWindow()
16     {
17         //创建窗口
18         ExportAssetBundle window = (ExportAssetBundle)EditorWindow.GetWindowWithRect(typeof(ExportAssetBundle),new Rect(Screen.width/2,Screen.height/2,400,80), true, "导出AssetBundle");
19         window.Show();
20 
21     }
22 
23 
24     void OnGUI()
25     {
26         EditorGUILayout.BeginHorizontal();
27         GUILayout.Label("导出路径:");
28         OutputPath = EditorGUILayout.TextField(OutputPath);
29         if (GUILayout.Button("浏览"))
30         {
31             //EditorApplication.delayCall += OpenFolder;
32             OutputPath = EditorUtility.OpenFolderPanel("选择要导出的路径", "", "");
33         }
34         EditorGUILayout.EndHorizontal();
35         if (GUILayout.Button("打包",GUILayout.MinHeight(50)))
36         {
37             BuildAssetBundle();
38             this.Close();
39         }
40     }
41 
42 
43     public void BuildAssetBundle()
44     {
45         string outputPath = Path.Combine(OutputPath, Platform.GetPlatformFolder(EditorUserBuildSettings.activeBuildTarget));
46         if (!Directory.Exists(outputPath))
47         {
48             Directory.CreateDirectory(outputPath);
49         }
50 
51         //根据BuildSetting里面所激活的平台进行打包
52         BuildPipeline.BuildAssetBundles(outputPath, 0, EditorUserBuildSettings.activeBuildTarget);
53 
54         AssetDatabase.Refresh();
55 
56         Debug.Log("打包完成");
57 
58     }
59 }
60 
61 public class Platform
62 {
63     public static string GetPlatformFolder(BuildTarget target)
64     {
65         switch (target)
66         {
67             case BuildTarget.Android:
68                 return "Android";
69             case BuildTarget.iOS:
70                 return "IOS";
71             case BuildTarget.WebGL:
72                 return "WebGL";
73             case BuildTarget.StandaloneWindows:
74             case BuildTarget.StandaloneWindows64:
75                 return "Windows";
76             case BuildTarget.StandaloneOSXIntel:
77             case BuildTarget.StandaloneOSXIntel64:
78             case BuildTarget.StandaloneOSXUniversal:
79                 return "OSX";
80             default:
81                 return null;
82         }
83     }
84 }

 

转载于:https://www.cnblogs.com/luxishi/p/6638617.html

显示全文