using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
public class BuildScene : EditorWindow
{
[MenuItem("Custom Editor/AssetBundle/Bundle Scene")]
static void ShowGUI()
{
BuildScene buildScene = GetWindow<BuildScene>();
buildScene.titleContent = new GUIContent("场景打包参数设置");
buildScene.Show();
}
private SceneAsset scene;
private string scene_suffix = "unity3d";
private BuildTarget build_target = BuildTarget.StandaloneWindows64;
private BuildOptions build_options = BuildOptions.BuildAdditionalStreamedScenes;
//Test
private string pathShow;
private UnityEngine.Object objectShow;
private void OnGUI()
{
scene = EditorGUILayout.ObjectField("要打包的场景", scene, typeof(SceneAsset), true) as SceneAsset;
scene_suffix = EditorGUILayout.TextField("后缀名(不用加.)", scene_suffix);
build_target = (BuildTarget)EditorGUILayout.EnumPopup("构建平台", build_target);
build_options = (BuildOptions)EditorGUILayout.EnumFlagsField("构建操作", build_options);
if(GUILayout.Button("打包"))
{
CreateScene();
}
if (GUILayout.Button("清除默认地址"))
{
ClearFilePath();
}
GUILayout.BeginHorizontal();
GUILayout.EndHorizontal();
GUILayout.Label(string.Format("{0}", "************************************************************************************************"));
objectShow = EditorGUILayout.ObjectField("查看的资源", objectShow, typeof(Object), true);
GUILayout.Label(string.Format("{0}\t{1}", "资源路径", pathShow));
if (GUILayout.Button("检查资源路径 AssetDatabase.GetAssetPath"))
{
Debug.Log(Application.dataPath);
if(objectShow as SceneAsset)
{
pathShow = AssetDatabase.GetAssetPath(objectShow);
}
else
{
//EditorUtility.DisplayPopupMenu(new Rect(0, 0, 500, 500), "Window/Package Manager", new MenuCommand(objectShow));
EditorUtility.DisplayDialog("警告", "不是Assets下的资源", "no good");
}
}
}
private void CreateScene()
{
//清空一下缓存
Caching.ClearCache();
if(null == scene)
{
return;
}
if(string.IsNullOrEmpty(scene_suffix))
{
return;
}
scene_suffix = scene_suffix.Replace(".", "");
string Path = string.Empty;
string filepath = PlayerPrefs.GetString("Path");
if (string.IsNullOrEmpty(Path))
{
if (string.IsNullOrEmpty(filepath))
{
Path = EditorUtility.SaveFilePanel("保存资源", "SS", "" + scene.name, scene_suffix);
filepath = Path.Substring(0, Path.LastIndexOf('/'));
}
else
{
Path = filepath + "/" + scene.name +"." + scene_suffix;
}
PlayerPrefs.SetString("Path", filepath);
}
//另一种获得用户选择的路径,默认把打包后的文件放在Assets目录下
//string Path = Application.dataPath + "/MyScene.unity3d";
//选择的要保存的对象
string[] levels = {
AssetDatabase.GetAssetPath(scene)};
try
{
//打包场景
BuildPipeline.BuildPlayer(levels, Path, build_target, build_options);
// 刷新,可以直接在Unity工程中看见打包后的文件
AssetDatabase.Refresh();
if (!string.IsNullOrEmpty(filepath))
{
System.Diagnostics.Process.Start(filepath);
Debug.Log("Bundle Success!!!");
}
}
catch(System.Exception e)
{
Debug.Log("Bundle fail!!! message:" + e);
}
}
//[MenuItem("Custom Editor/AssetBundle/清除默认地址")]
static public void ClearFilePath()
{
PlayerPrefs.DeleteKey("Path");
Debug.Log("默认地址清除成功!" + PlayerPrefs.GetString("Path"));
}
}
Unity 打包场景
Guess you like
Origin blog.csdn.net/qq_41179365/article/details/119389199
Ranking