UnityEditor编辑器扩展快速导出包用的脚本剔除的代码分享
Posted avi9111
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UnityEditor编辑器扩展快速导出包用的脚本剔除的代码分享相关的知识,希望对你有一定的参考价值。
纯代码分享
可用于导出模型,把脚本排除了
Assets/Export pack方便很多
using System.Collections;
using System.Collections.Generic;
using System.IO;
using LightUtility;
using UnityEditor;
using UnityEngine;
public class SpecMenuTools
{
public const string KSpecToolsGroup = "Tools/SpecScrollView/";
public const string KAssetExport = "Assets/SpecScrollView/";
//[ContextMenu("Assets/CopyPrefabNoScripts")]
[MenuItem(KAssetExport+ "CopyPrefabNoScripts")]
public static void CopyPrefabDeleteScript(MenuCommand command)
{
GameObject go = command.context as GameObject;
if (go == null) return;
var path = AssetDatabase.GetAssetPath(go);
var isDone = AssetDatabase.CopyAsset(path, path.Replace(".prefab", "_noscript.prefab"));
if (isDone == false) return;
}
[MenuItem(KAssetExport+"CopyDeletAllScripts")]
public static void RemoveAllScripts(MenuCommand command)
{
//通过 command 获取 go 现在有问题
//GameObject go = command.context as GameObject;
GameObject go = Selection.activeGameObject;
if (go == null) return;
//三个monobehaviou,自己看着办,反正最后的才能用
//1
// var scripts = go.GetComponents<MonoBehaviour>();
// Resources.UnloadAsset(go);//无灭用方法
// for (int i = 0; i < scripts.Length; i++)
// {
//
// DestroyImmediate(scripts[i]);
// }
//2
//RemoveComponents(scripts);
//3
CheckDeleteMonoBehavior(go);
}
private static bool RemoveComponents(Component[] components)
{
if (components.Length == 0)
{
return false;
}
foreach (var component in components)
{
SerializedObject so = new SerializedObject(component.gameObject);
SerializedProperty sp = so.FindProperty("m_Component");
var allComponents = component.gameObject.GetComponents<Component>();
for (int i = 0, len = allComponents.Length; i < len; i++)
{
if (allComponents[i] == component)
{
sp.DeleteArrayElementAtIndex(i);
so.ApplyModifiedProperties();
break;
}
}
}
return true;
}
static void CheckDeleteMonoBehavior(GameObject obj)
{
//实例化物体
var go = PrefabUtility.InstantiatePrefab(obj) as GameObject;
//递归删除
//searchChild(go);
var scripts = go.GetComponents<MonoBehaviour>();
for (int i = 0; i < scripts.Length; i++)
{
Object.DestroyImmediate(scripts[i]);
}
// 将数据替换到asset
var newPath = AssetDatabase.GetAssetPath(obj);
PrefabUtility.SaveAsPrefabAsset(go, newPath);
go.hideFlags = HideFlags.HideAndDontSave;
//删除掉实例化的对象
Object.DestroyImmediate(go);
}
}
以上是关于UnityEditor编辑器扩展快速导出包用的脚本剔除的代码分享的主要内容,如果未能解决你的问题,请参考以下文章
UnityEditor编辑器扩展开发之一些有用的Gizmos扩展方法