当前位置:首页 > 编程笔记 > 正文
已解决

Unity Editor工具,导出unitypackage可选择是否包含脚本

来自网友在路上 179879提问 提问时间:2023-11-01 18:44:04阅读次数: 79

最佳答案 问答题库798位专家为你答疑解惑

概述

Unity自带的Export Package...功能,如果选中资源中包含脚本,或者Prefab挂载了自定义的脚本。在之后弹出的选择框内,如果勾选了Include dependencies会将整个项目所有的脚本全部都包含在内。等于导入了很多不相关的代码。如果取消勾选Include dependencies,又只有选中的那个资源,其他引用到的资源又不包含在内。

但是在实际的工作中导出资源时,只想导出跟选中资源相关的资源,并不想包含脚本,或者仅包含该prefab挂载的脚本。这时候就需要自己编写一个Editor工具实现该功能。具体的代码如下

工具代码

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using System.IO;public class AssetSelectPopUpWindow : EditorWindow
{private Vector2 scrollPosition;private List<string> items = null;//是否导出脚本public static bool exportWithScript = false;private bool[] selectionStates;#region editor菜单相关[MenuItem("Assets/Tools/导出Unity资源包")]public static void ExportWithoutScript(){exportWithScript = false;ShowWindow();}[MenuItem("Assets/Tools/导出Unity资源包(包含脚本)")]public static void ExportWithScript(){exportWithScript = true;ShowWindow();}public static void ShowWindow(){AssetSelectPopUpWindow wnd = GetWindow<AssetSelectPopUpWindow>();wnd.titleContent = new GUIContent("资源导出");wnd.minSize = new Vector2(450, 200);wnd.maxSize = new Vector2(1920, 720);wnd.Show();}#endregionpublic void GetAllFiles(bool withScript){//获取鼠标选中的所有文件Object[] selectedObjects = Selection.GetFiltered<Object>(SelectionMode.Assets);List<string> assetPathNames = new List<string>();for (int i = 0; i < selectedObjects.Length; i++){string directoryPath = AssetDatabase.GetAssetPath(selectedObjects[i]);if (directoryPath != null){//如果是文件夹,就遍历文件夹下的所有资源if (Directory.Exists(directoryPath)){string[] folders = Directory.GetFiles(directoryPath);for (int j = 0; j < folders.Length; j++){//过滤掉.meta文件if (!folders[j].EndsWith(".meta")){assetPathNames.Add(folders[j]);}}}else{assetPathNames.Add(directoryPath);}}}items = new List<string>();for (int i = 0; i < assetPathNames.Count; i++){var depends = AssetDatabase.GetDependencies(assetPathNames[i], true);for (int j = 0; j < depends.Length; j++){AddFiles(withScript, depends[j]);}}items.Sort();selectionStates = new bool[items.Count];//默认全选 SelectAllItems();}private void OnEnable(){GetAllFiles(exportWithScript);}//打印所有选择的文件private void ShowFiles(){for (int i = 0; i < items.Count; i++){Debug.Log($"all Files is {items[i]}");}}private void AddFiles(bool withScript, string filePath){//特定的目录不处理if (filePath.StartsWith("Packages/")){return;}if (withScript || !filePath.EndsWith(".cs")){if (!items.Contains(filePath)){items.Add(filePath);}}}private void OnGUI(){EditorGUILayout.Space(10);// Scroll viewusing (var scrollView = new EditorGUILayout.ScrollViewScope(scrollPosition)){scrollPosition = scrollView.scrollPosition;for (int i = 0; i < items.Count; i++){selectionStates[i] = EditorGUILayout.ToggleLeft(items[i], selectionStates[i]);}}GUILayout.Space(10);// ButtonsGUILayout.BeginHorizontal();if (GUILayout.Button("全选")){SelectAllItems();}if (GUILayout.Button("全不选")){DeselectAllItems();}GUILayout.EndHorizontal();GUILayout.Space(10);if (GUILayout.Button("导出")){OutputSelectedItems();}}#region 按钮事件private void SelectAllItems(){for (int i = 0; i < selectionStates.Length; i++){selectionStates[i] = true;}}private void DeselectAllItems(){for (int i = 0; i < selectionStates.Length; i++){selectionStates[i] = false;}}private void OutputSelectedItems(){List<string> exportItems = new List<string>();    for (int i = 0; i < items.Count; i++){if (selectionStates[i]){exportItems.Add(items[i]);}}if(exportItems.Count == 0){EditorUtility.DisplayDialog("没有任何选中的资源", "请选择想要导出的资源", "OK");return;}var path = EditorUtility.SaveFilePanel("导出资源包", "", "", "unitypackage");if (path == "")return;var flag = ExportPackageOptions.Interactive | ExportPackageOptions.Recurse;//如果选择完后还想导出该资源关联的资源可以添加ExportPackageOptions.IncludeDependencies的Flag//if (exportWithScript)//{//    flag = flag | ExportPackageOptions.IncludeDependencies;//}AssetDatabase.ExportPackage(exportItems.ToArray(), path, flag);Close();}#endregion}

示例

首先随便创建一个prefab,并且给prefab创建一些动画机,修改材质,并且挂载一个自定义的脚本

之后右键Cube.prefab使用unity自带的 Export Package...

如果勾选了Include dependencies,可以看到unity会将所有的脚本都包含在内,如下图所示

然而实际上跟这个Cube.prefab相关的资源只有下面这些

如果取消勾选Include dependencies,又只包含了这个prefab,相关的材质动画等资源全部没有包含在内

这时候改用我们自定义的AssetSelectPopUpWindow的导出功能,右键Cube.prefab,在弹出的菜单选择"Tools/导出Unity资源包”

可以看到实际包含的就只有跟prefab相关的资源

如果选择"导出Unity资源包(包含脚本)",会将Cube上包含的TestBehaviour也加入到列表内

列表内确认需要的资源后点击底部的导出按钮,即可导出package

参考文档

Editor界面编写:https://docs.unity.cn/Manual/UIE-HowTo-CreateEditorWindow.html

创作不易,如果觉得这篇文章对你有所帮助,可以动动小手,点个赞哈,ღ( ´・ᴗ・` )比心

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"Unity Editor工具,导出unitypackage可选择是否包含脚本":http://eshow365.cn/6-29530-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!