unity代码导出unitypackage包
using Sirenix.OdinInspector.Editor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace Tools
{
public class ExporEditor : OdinEditorWindow
{
[MenuItem("Assets/导出unitypackage", false, 1)]
private static void ExportToUnityPackage()
{
List<string> selectedPaths = new List<string>();
string[] selectedGUIDs = Selection.assetGUIDs;
foreach (string guid in selectedGUIDs)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
selectedPaths.Add(path);
Debug.Log("Selected file: " + path);
}
string savePath = Path.Combine(Tools.GetSaveUnityPackagePath(), "ExportedPackage.unitypackage") ;
DataMgr.Instance.ExportPackageAssetsPath = savePath;
Debug.Log("保存路径:" + savePath);
ExportToUnityPackage(selectedPaths.ToArray(), savePath);
}
private static void ExportToUnityPackage(string[] assetPaths, string exportPath)
{
List<string> explicitAssetPath = new List<string>();
List<string> allAsset2Export = new List<string>();
foreach (string implicitAssetPath in assetPaths)
{
if (File.Exists(implicitAssetPath))
{
explicitAssetPath.Add(implicitAssetPath);
}
else if (Directory.Exists(implicitAssetPath))
{
List<string> str1 = new List<string>();
List<string> str2 = new List<string>();
List<string> str3 = new List<string>();
List<string> str4 = new List<string>();
GetAllAssetFiles(implicitAssetPath, "*.tga", ref str1);
GetAllAssetFiles(implicitAssetPath, "*.png", ref str2);
GetAllAssetFiles(implicitAssetPath, "*.fbx", ref str3);
GetAllAssetFiles(implicitAssetPath, "*.prefab", ref str4);
explicitAssetPath.AddRange(str1);
explicitAssetPath.AddRange(str2);
explicitAssetPath.AddRange(str3);
explicitAssetPath.AddRange(str4);
}
}
foreach (string explicitAsset in explicitAssetPath)
{
if (explicitAsset.ToLower().EndsWith(".meta"))
continue;
allAsset2Export.Add(explicitAsset);
string[] allDeps = AssetDatabase.GetDependencies(explicitAsset, true);
if (allDeps != null)
allAsset2Export.AddRange(allDeps);
}
for (int i = allAsset2Export.Count - 1; i >= 0; i--)
{
if (allAsset2Export[i].EndsWith(".cs"))
allAsset2Export.RemoveAt(i);
}
HashSet<string> uniqueAssetPathSet = new HashSet<string>(allAsset2Export);
allAsset2Export.Clear();
allAsset2Export.AddRange(uniqueAssetPathSet);
if (allAsset2Export.Count <= 0)
{
Debug.Log("当前文件夹没有png或者tga文件!!!");
return;
}
DataMgr.Instance.ExportPackageAssetsPaths = allAsset2Export;
AssetDatabase.ExportPackage(allAsset2Export.ToArray(), exportPath, ExportPackageOptions.Default);
Debug.Log("导出成功!!");
ArtUploadToolsEditor.OpenWindow();
}
static private void GetAllAssetFiles(string directory, string pattern, ref List<string> list)
{
DirectoryInfo directoryInfo = new DirectoryInfo(directory);
foreach (FileInfo info in directoryInfo.GetFiles(pattern))
{
string assetPath = info.FullName.Substring(info.FullName.IndexOf("Assets"));
assetPath = assetPath.Replace('\\', '/');
list.Add(assetPath);
}
foreach (DirectoryInfo info in directoryInfo.GetDirectories())
{
GetAllAssetFiles(info.FullName, pattern, ref list);
}
}
}
}