Created
May 31, 2023 22:47
-
-
Save nicoplv/564a88bf768edc265038f10e6380396a to your computer and use it in GitHub Desktop.
Script to clear FX prefab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEditor; | |
using UnityEngine; | |
namespace Utils | |
{ | |
public class ClearFxPrefab | |
{ | |
#region Methods | |
[UnityEditor.MenuItem("Tools/Clear fx Prefab")] | |
protected static void Clear() | |
{ | |
GameObject[] selectedPrefabs = Selection.gameObjects; | |
foreach (GameObject selectedPrefab in selectedPrefabs) | |
{ | |
if (selectedPrefab != null && PrefabUtility.GetPrefabAssetType(selectedPrefab) != PrefabAssetType.NotAPrefab) | |
{ | |
bool hasChanged = false; | |
// Get all child lights | |
Light[] childLights = selectedPrefab.GetComponentsInChildren<Light>(); | |
// Destroy child lights | |
foreach (Light childLight in childLights) | |
{ | |
if(PrefabUtility.GetPrefabAssetType(childLight.gameObject) == PrefabAssetType.Regular) | |
{ | |
hasChanged = true; | |
Undo.DestroyObjectImmediate(childLight.gameObject); | |
} | |
} | |
// Get all components | |
Component[] components = selectedPrefab.GetComponentsInChildren<Component>(true); | |
foreach (Component component in components) | |
{ | |
if (component.GetType() == typeof(Transform)) | |
continue; | |
if (component.GetType() == typeof(ParticleSystem)) | |
continue; | |
if (component.GetType() == typeof(ParticleSystemRenderer)) | |
continue; | |
if (PrefabUtility.GetPrefabAssetType(component.gameObject) == PrefabAssetType.Regular) | |
{ | |
hasChanged = true; | |
Undo.DestroyObjectImmediate(component); | |
} | |
} | |
// Save the changes to the prefab | |
if (hasChanged) | |
PrefabUtility.SavePrefabAsset(selectedPrefab); | |
} | |
} | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment