Last active
June 26, 2017 00:15
-
-
Save winkels/303a7ac77cbbf4c026a93bc38598caa0 to your computer and use it in GitHub Desktop.
Set of Unity editor classes that provide a template for programatically refactoring prefabs. As a trivial example, this version changes all colliders to be triggers. Customize it to your needs by changing the code in RefactorTemplate.Refactor() (i.e. the component type you are searching for and what is done those components).
This file contains hidden or 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 UnityEditor; | |
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class RefactorTemplate : MonoBehaviour | |
{ | |
[MenuItem("Refactor/Refactor")] | |
static void Refactor() | |
{ | |
GameObject[] prefabs = RefactorUtilities.LoadAllPrefabsWithComponent(typeof(Collider)); | |
foreach(GameObject prefab in prefabs) | |
{ | |
Collider[] colliders = prefab.GetComponentsInChildren<Collider>(true); | |
foreach(Collider collider in colliders) | |
{ | |
collider.isTrigger = true; | |
EditorUtility.SetDirty(collider); | |
} | |
} | |
} | |
} |
This file contains hidden or 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 UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class RefactorUtilities | |
{ | |
public static System.IO.FileInfo[] GetFileInfosForAssetsWithExtension(string fileExtension) | |
{ | |
if(fileExtension != null && fileExtension.Length > 0 && fileExtension.IndexOf(".") != 0) | |
{ | |
fileExtension = "." + fileExtension; | |
} | |
string searchPattern = fileExtension != null ? "*" + fileExtension : "*"; | |
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(Application.dataPath); | |
return directory.GetFiles(searchPattern, System.IO.SearchOption.AllDirectories); | |
} | |
public static string[] GetAssetPaths(string fileExtension) | |
{ | |
System.IO.FileInfo[] fileInfos = GetFileInfosForAssetsWithExtension(fileExtension); | |
List<string> paths = new List<string>(); | |
foreach(System.IO.FileInfo fileInfo in fileInfos) | |
{ | |
paths.Add(fileInfo.FullName.Replace(Application.dataPath, "Assets")); | |
} | |
return paths.ToArray(); | |
} | |
public static T[] LoadAssets<T>(string extension) where T : class | |
{ | |
string[] paths = GetAssetPaths("controller"); | |
List<T> assets = new List<T>(); | |
foreach(string path in paths) | |
{ | |
T asset = UnityEditor.AssetDatabase.LoadAssetAtPath(path, typeof(T)) as T; | |
assets.Add(asset); | |
} | |
return assets.ToArray(); | |
} | |
public static GameObject[] LoadAllPrefabsWithComponent(System.Type componentType) | |
{ | |
string[] paths = GetAssetPaths("prefab"); | |
List<GameObject> prefabs = new List<GameObject>(); | |
foreach(string path in paths) | |
{ | |
GameObject go = UnityEditor.AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject; | |
if(go.GetComponentsInChildren(componentType, true).Length > 0) | |
{ | |
prefabs.Add(go); | |
} | |
} | |
return prefabs.ToArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment