/// <summary> /// Performs the specified modification on all assets in the project of the specified type. /// </summary> /// <param name="onModifyAsset">The callback to invoke for each asset found.</param> /// <param name="undoMessage">Optional undo message to use.</param> /// <param name="isMatch">Optional predicate to determine whether the found asset should be included.</param> /// <typeparam name="T">A <see cref="UnityEngine.Object"/> type.</typeparam> public static void ModifyAllAssetsInProject <T>( ModifyAssetCallback <T> onModifyAsset, string undoMessage = null, AssetMatchPredicate <T> isMatch = null ) where T : Object { Dictionary <T, string> assets = null; FindAllAssets(assets, isMatch); ModifyAllAssetsInProject(assets, onModifyAsset, undoMessage); }
/// <summary> /// Performs the specified modification on all components of the specified type on assets in the project. /// </summary> /// <param name="onModifyComponent">The callback to invoke for each component found.</param> /// <param name="undoMessage">Optional undo message to use.</param> /// <typeparam name="T">A <see cref="UnityEngine.Component"/> type.</typeparam> public static void ModifyAllComponentsInProject <T>( ModifyAssetCallback <T> onModifyComponent, string undoMessage = null ) where T : Component { Dictionary <List <T>, string> assetsWithComponent = null; FindAllAssetsWithComponent(ref assetsWithComponent); Dictionary <T, string> assets = new Dictionary <T, string>(); foreach (KeyValuePair <List <T>, string> comps in assetsWithComponent) { foreach (T comp in comps.Key) { assets[comp] = comps.Value; } } ModifyAllAssetsInProject(assets, onModifyComponent, undoMessage); }
/// <summary> /// Performs the specified modification on all assets in the project of the specified type. /// </summary> /// <param name="assets">The assets to modify and their respective paths.</param> /// <param name="onModifyAsset">The callback to invoke for each asset found.</param> /// <param name="undoMessage">Undo message to use.</param> /// <typeparam name="T">A <see cref="UnityEngine.Object"/> type.</typeparam> private static void ModifyAllAssetsInProject <T>( Dictionary <T, string> assets, ModifyAssetCallback <T> onModifyAsset, string undoMessage ) where T : Object { int undoGroup = Undo.GetCurrentGroup(); undoMessage = string.IsNullOrEmpty(undoMessage) ? string.Format("Modify All {0} in Project", typeof(T)) : undoMessage; int assetIndex = 0; string formatString = typeof(GameObject).IsAssignableFrom(typeof(T)) || typeof(Component).IsAssignableFrom(typeof(T)) ? "Processing Prefab {0} / {1}" : "Processing Asset {0} / {1}"; foreach (KeyValuePair <T, string> kv in assets) { string message = string.Format(formatString, assetIndex, assets.Count); EditorUtility.DisplayProgressBar(message, message, (float)assetIndex / assets.Count); #if !UNITY_4_6 && !UNITY_4_7 Undo.SetCurrentGroupName(undoMessage); #endif onModifyAsset(kv.Key, kv.Value); ++assetIndex; } Undo.CollapseUndoOperations(undoGroup); EditorUtility.ClearProgressBar(); AssetDatabase.SaveAssets(); if ( !EditorUtility.DisplayDialog( "Search Scenes?", "Prefab hierarchies have been modified.\n\n" + string.Format( "Do you also wish to modify {0} in scenes that are not in prefab hierarchies? " + "(This operation is not undoable and will clear your undo queue.)", typeof(T).Name ), "Yes", "No" ) ) { return; } List <string> scenePaths = new List <string>( from guid in AssetDatabase.FindAssets("t:Scene") select AssetDatabase.GUIDToAssetPath(guid) ); scenePaths.RemoveAll(p => !System.IO.File.Exists(p)); for (int i = 0; i < scenePaths.Count; ++i) { string scenePath = scenePaths[i]; string message = string.Format("Processing Scene {0} / {1}", i, scenePaths.Count); EditorUtility.DisplayProgressBar(message, message, (float)i / scenePaths.Count); #if NO_SCENE_MANAGER EditorApplication.OpenScene(scenePath); #else UnityEditor.SceneManagement.EditorSceneManager.OpenScene( scenePath, UnityEditor.SceneManagement.OpenSceneMode.Single ); #endif HashSet <T> allObjects = new HashSet <T>(); if (!typeof(Component).IsAssignableFrom(typeof(T))) { foreach (T obj in Object.FindObjectsOfType <T>()) { allObjects.Add(obj); } } else { foreach (GameObject go in Object.FindObjectsOfType <GameObject>()) { foreach (Component comp in go.GetComponentsInChildren(typeof(T), true)) { allObjects.Add(comp as T); } } } undoGroup = Undo.GetCurrentGroup(); foreach (T obj in allObjects) { onModifyAsset(obj, scenePaths[i]); } Undo.CollapseUndoOperations(undoGroup); if (allObjects.Count > 0) { #if NO_SCENE_MANAGER EditorApplication.SaveScene(); #else UnityEditor.SceneManagement.EditorSceneManager.SaveOpenScenes(); #endif } } EditorUtility.ClearProgressBar(); #if NO_SCENE_MANAGER EditorApplication.NewScene(); #else UnityEditor.SceneManagement.EditorSceneManager.NewScene( UnityEditor.SceneManagement.NewSceneSetup.DefaultGameObjects, UnityEditor.SceneManagement.NewSceneMode.Single ); #endif }