Last active
August 29, 2015 14:06
-
-
Save strich/ce27728b789119f00981 to your computer and use it in GitHub Desktop.
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
| public class BulkMaterialSwapWindow : MonoBehaviour { | |
| public Material oldMat; | |
| public Material newMat; | |
| [MenuItem("WFTO/Bulk Asset Modifier/Bulk Material Swap")] | |
| static void Init() { | |
| BulkMaterialSwapWindow window = EditorWindow.GetWindowWithRect(typeof(BulkMaterialSwapWindow), new Rect(0, 0, 165, 100)); | |
| window.Show(); | |
| } | |
| void OnGUI() { | |
| EditorGUILayout.BeginHorizontal(); | |
| oldMat = EditorGUILayout.ObjectField("Old Material", oldMat, typeof(Material), true); | |
| EditorGUILayout.EndHorizontal(); | |
| EditorGUILayout.BeginHorizontal(); | |
| newMat = EditorGUILayout.ObjectField("New Material", newMat, typeof(Material), true); | |
| EditorGUILayout.EndHorizontal(); | |
| if (GUILayout.Button("Swap material")) | |
| if (oldMat == null || newMat == null) | |
| .ShowNotification(new GUIContent("No material selected for swapping!")); | |
| else | |
| SwapMaterials(); | |
| } | |
| private void SwapMaterials() { | |
| var assets = GetAssetsOfType<Object>(".prefab"); | |
| Debug.Log(assets.Length + " prefabs found."); | |
| int materialCounter = 0; | |
| foreach (var assetObject in assets) { | |
| GameObject assetGO = assetObject as GameObject; | |
| if (assetGO == null) | |
| continue; | |
| List<Renderer> renderers = new List<Renderer>(); | |
| renderers.AddRange(assetGO.GetComponentsInChildren<Renderer>(true)); | |
| foreach (var renderer in renderers) { | |
| materialCounter++; | |
| renderer = newMat; | |
| } | |
| } | |
| Debug.Log(materialCounter + " materials swapped."); | |
| AssetDatabase.SaveAssets(); | |
| AssetDatabase.Refresh(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment