和Package Uninstaller 功能类似,不过他做的有点差。
反射扩展 http://www.jianshu.com/p/cf05e80e4d1c
using System;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
using Babybus.Framework.ExtensionMethods;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System.Linq;
namespace Babybus.Framework.Extension
{
public class PackageModifier : EditorWindow
{
private EditorWindow packageImportWindow;
private static string packagePath;
private const string key = "directory@PackageModifier";
private static string directory
{
get
{
return EditorPrefs.GetString(key);
}
set
{
EditorPrefs.SetString(key, value);
}
}
sealed class ImportPackageItem
{
public string exportedAssetPath;
public string destinationAssetPath;
public string sourceFolder;
public string previewPath;
public string guid;
public int enabledStatus;
public bool isFolder;
public bool exists;
public bool assetChanged;
public bool pathConflict;
public bool projectAsset;
}
void Awake()
{
ImportPackage(packagePath);
var packageImportType = typeof(Editor).Assembly.GetType("UnityEditor.PackageImport");
packageImportWindow = GetWindow(packageImportType, true);
minSize = packageImportWindow.minSize;
maxSize = packageImportWindow.maxSize;
position = packageImportWindow.position;
titleContent = packageImportWindow.titleContent;
packageImportWindow.Close();
}
void OnDestroy()
{
}
void OnGUI()
{
try
{
packageImportWindow.Invoke("OnGUI");
}
catch (Exception)
{
}
GUILayout.BeginVertical();
if (GUILayout.Button("Delete"))
{
var directorys = new List();
var m_ImportPackageItems = packageImportWindow.GetFieldValue("m_ImportPackageItems") as IEnumerable;
foreach (var item in m_ImportPackageItems)
{
var json = JsonUtility.ToJson(item);
var importPackageItem = JsonUtility.FromJson(json);
if (importPackageItem.exists && (importPackageItem.isFolder || importPackageItem.enabledStatus == 1))
{
var assetPath = AssetDatabase.GUIDToAssetPath(importPackageItem.guid);
Debug.Log(assetPath);
if (importPackageItem.isFolder)
directorys.Add(assetPath);
else
AssetDatabase.DeleteAsset(assetPath);
}
}
for (var i = directorys.Count - 1; i >= 0; i--)
{
var path = directorys[i];
var files = new DirectoryInfo(path).GetFiles();
if (files.Where(item => item.Extension != ".meta").Count() == 0)
AssetDatabase.DeleteAsset(path);
}
Close();
}
GUILayout.Space(10f);
GUILayout.EndVertical();
}
[DidReloadScripts]
static void ClosePackageImportWindow()
{
var array = Resources.FindObjectsOfTypeAll(typeof(PackageModifier));
if (array == null || array.Length <= 0)
return;
GetWindow(true).Close();
}
[MenuItem("BabybusFrame/Window/Package Modifier")]
static void ShowPackageImportWindow()
{
packagePath = EditorUtility.OpenFilePanelWithFilters("Import package ...", directory, new string[] { "unitypackage", "unitypackage" });
if (packagePath == "")
return;
directory = Path.GetDirectoryName(packagePath);
GetWindow(true).Show();
}
public void ImportPackage(string packagePath)
{
string packageIconPath = "";
bool allowReInstall = false;
var assembly = typeof(Editor).Assembly;
var PackageUtility = assembly.GetType("UnityEditor.PackageUtility");
var parameters = new object[] { packagePath, packageIconPath, allowReInstall };
var array = PackageUtility.Invoke("ExtractAndPrepareAssetList", parameters);
packageIconPath = (string)parameters[1];
allowReInstall = (bool)parameters[2];
if (array == null)
{
return;
}
var PackageImport = assembly.GetType("UnityEditor.PackageImport");
PackageImport.Invoke("ShowImportPackage", packagePath, array, packageIconPath, allowReInstall);
}
}
}