unity代码导出unitypackage包

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>();

            // 递归找出所有要导出的资源 - explicitAssetPath
            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);
                    //GetAllAssetFiles(implicitAssetPath, "*.*", ref explicitAssetPath);
                }
            }

            // 递归找出所有资源的依赖 - allAsset2Export
            foreach (string explicitAsset in explicitAssetPath)
            {
                // meta 跳过
                if (explicitAsset.ToLower().EndsWith(".meta"))
                    continue;

                allAsset2Export.Add(explicitAsset);
                string[] allDeps = AssetDatabase.GetDependencies(explicitAsset, true);
                if (allDeps != null)
                    allAsset2Export.AddRange(allDeps);
            }

            // 剔除 MonoScript - *.cs
            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;
            }

            //AssetDatabase.ExportPackage(assetPaths, exportPath, ExportPackageOptions.Recurse | ExportPackageOptions.IncludeDependencies);
            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);
            }
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值