-
Notifications
You must be signed in to change notification settings - Fork 322
/
PrefabModify.cs
46 lines (41 loc) · 1.68 KB
/
PrefabModify.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
namespace ToolKits
{
public class PrefabModify
{
private const string PrefabPath = "Assets/GameAssets/Prefabs/Cube.prefab";
[MenuItem("Tools/PrefabModify/AddComponentAndSave", priority = 26)]
private static void AddComponentAndSave()
{
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(PrefabPath);
var gameobject = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
if (null == gameobject.GetComponent<ComponentA>())
{
gameobject.AddComponent<ComponentA>();
}
PrefabUtility.SaveAsPrefabAssetAndConnect(gameobject, PrefabPath,InteractionMode.AutomatedAction);
GameObject.DestroyImmediate(gameobject);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
[MenuItem("Tools/PrefabModify/DelComponentAndSave", priority = 26)]
private static void DelComponentAndSave()
{
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(PrefabPath);
var gameobject = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
var cmps = gameobject.GetComponents<ComponentA>();
foreach (var item in cmps)
{
Object.DestroyImmediate(item);
}
PrefabUtility.SaveAsPrefabAssetAndConnect(gameobject, PrefabPath,InteractionMode.AutomatedAction);
GameObject.DestroyImmediate(gameobject);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
}