Skip to content

Instantly share code, notes, and snippets.

@iwashihead
Last active September 28, 2016 08:25
Show Gist options
  • Save iwashihead/2863681f2cd58869568b76ddfa918f2e to your computer and use it in GitHub Desktop.
Save iwashihead/2863681f2cd58869568b76ddfa918f2e to your computer and use it in GitHub Desktop.
Project内のSceneやPrefabに対して一括処理を実行するヘルパースクリプト
using System;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Griphone.Agito.EditorExtension
{
/// <summary>
/// シーン操作
/// </summary>
public class EditorSceneOperation
{
/// <summary>
/// Project内の全てのシーンに対して処理を実行します
/// </summary>
public static void DoForAll(Action<Scene> action)
{
var cnt = 0;
var scenePathList = AssetDatabase.GetAllAssetPaths().Where(_ => Path.GetExtension(_) == ".unity").ToList();
foreach (var path in scenePathList)
{
var scene = EditorSceneManager.OpenScene(path);
EditorUtility.DisplayProgressBar("全Scene操作", scene.name + "シーンの操作をしています", (float)cnt / scenePathList.Count);
if (action != null) action(scene);
EditorSceneManager.SaveScene(scene);
EditorSceneManager.CloseScene(scene, true);
cnt++;
}
EditorUtility.ClearProgressBar();
}
/// <summary>
/// 指定のシーンに対して処理を実行します
/// </summary>
/// <param name="sceneName">シーン名</param>
public static void DoFor(Action<Scene> action, params string[] sceneName)
{
var scenePathList = AssetDatabase.GetAllAssetPaths().Where(_ => Path.GetExtension(_) == ".unity").ToList();
foreach (var path in scenePathList)
{
if (!sceneName.Contains(Path.GetFileNameWithoutExtension(path))) continue;
var scene = EditorSceneManager.OpenScene(path);
if (action != null) action(scene);
EditorSceneManager.SaveScene(scene);
EditorSceneManager.CloseScene(scene, true);
}
}
}
/// <summary>
/// Prefab操作
/// </summary>
public class EditorPrefabOperation
{
/// <summary>
/// Project内の全てのプレハブに対して処理を実行します
/// </summary>
public static void DoForAll(Action<GameObject> action)
{
var cnt = 0;
var prefabPathList = AssetDatabase.GetAllAssetPaths().Where(_ => Path.GetExtension(_) == ".prefab").ToList();
foreach (var path in prefabPathList)
{
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
if (!prefab) { cnt++; continue; }
EditorUtility.DisplayProgressBar("全Prefab操作", prefab.name + "Prefabの操作をしています", (float)cnt / prefabPathList.Count);
if (action != null) action(prefab);
cnt++;
}
EditorUtility.ClearProgressBar();
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
/// <summary>
/// 指定のプレハブに対して処理を実行します
/// </summary>
/// <param name="prefabPathList">プレハブパス</param>
public static void DoFor(Action<GameObject> action, params string[] prefabPathList)
{
var cnt = 0;
foreach (var path in prefabPathList)
{
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
if (!prefab) { cnt++; continue; }
EditorUtility.DisplayProgressBar("全Prefab操作", prefab.name + "Prefabの操作をしています", (float)cnt / prefabPathList.Length);
if (action != null) action(prefab);
cnt++;
}
EditorUtility.ClearProgressBar();
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment