「Unity/EditorWindow/GameObjectをPrefabに変更」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「==全シーンの、指定したGameObjectをPrefabに変更するコード== <pre> using UnityEditor; using UnityEngine; using UnityEditor.SceneManagement; using UnityEn...」) |
|||
行6: | 行6: | ||
using UnityEngine.SceneManagement; | using UnityEngine.SceneManagement; | ||
using System.IO; | using System.IO; | ||
+ | using System.Text; | ||
public class ReplaceObjectWithPrefab : EditorWindow | public class ReplaceObjectWithPrefab : EditorWindow | ||
{ | { | ||
− | private string directoryPath = "Assets/Scenes"; | + | private string directoryPath = "Assets/Scenes"; // デフォルト値 |
− | private string objectName = "ReturnButton"; | + | private string objectName = "ReturnButton"; // デフォルト値 |
private GameObject prefab; | private GameObject prefab; | ||
行39: | 行40: | ||
Debug.LogError("Please specify the directory path, object name, and prefab."); | Debug.LogError("Please specify the directory path, object name, and prefab."); | ||
return; | return; | ||
+ | } | ||
+ | |||
+ | // 現在のシーンを保存し、シーンパスを記録 | ||
+ | Scene currentScene = EditorSceneManager.GetActiveScene(); | ||
+ | string currentScenePath = currentScene.path; | ||
+ | |||
+ | if (currentScene.isDirty) | ||
+ | { | ||
+ | EditorSceneManager.SaveScene(currentScene); | ||
} | } | ||
行63: | 行73: | ||
continue; | continue; | ||
} | } | ||
+ | |||
+ | // ヒエラルキーでの位置情報を保持する | ||
+ | Transform parentTransform = targetObject.transform.parent; | ||
+ | Vector3 localPosition = targetObject.transform.localPosition; | ||
+ | Quaternion localRotation = targetObject.transform.localRotation; | ||
+ | Vector3 localScale = targetObject.transform.localScale; | ||
+ | int siblingIndex = targetObject.transform.GetSiblingIndex(); | ||
// オブジェクトをプレハブに置き換える | // オブジェクトをプレハブに置き換える | ||
GameObject newObject = (GameObject)PrefabUtility.InstantiatePrefab(prefab, scene); | GameObject newObject = (GameObject)PrefabUtility.InstantiatePrefab(prefab, scene); | ||
− | newObject.transform. | + | |
− | newObject.transform. | + | // 新しいオブジェクトを元の位置に配置する |
+ | newObject.transform.SetParent(parentTransform); | ||
+ | newObject.transform.localPosition = localPosition; | ||
+ | newObject.transform.localRotation = localRotation; | ||
+ | newObject.transform.localScale = localScale; | ||
+ | newObject.transform.SetSiblingIndex(siblingIndex); | ||
// 旧オブジェクトを削除 | // 旧オブジェクトを削除 | ||
DestroyImmediate(targetObject); | DestroyImmediate(targetObject); | ||
+ | |||
+ | // ヒエラルキーパスを生成 | ||
+ | string hierarchyPath = GetHierarchyPath(newObject); | ||
+ | |||
+ | // シーンパスとヒエラルキーパスをログに出力 | ||
+ | Debug.Log($"Replaced object in scene: {scenePath}\nHierarchy Path: {hierarchyPath}"); | ||
// シーンを保存する | // シーンを保存する | ||
EditorSceneManager.MarkSceneDirty(scene); | EditorSceneManager.MarkSceneDirty(scene); | ||
EditorSceneManager.SaveScene(scene); | EditorSceneManager.SaveScene(scene); | ||
+ | } | ||
− | + | // 最後に元のシーンを再度開く | |
+ | if (!string.IsNullOrEmpty(currentScenePath)) | ||
+ | { | ||
+ | EditorSceneManager.OpenScene(currentScenePath); | ||
} | } | ||
+ | } | ||
+ | |||
+ | // ヒエラルキーパスを生成するメソッド | ||
+ | private string GetHierarchyPath(GameObject obj) | ||
+ | { | ||
+ | StringBuilder path = new StringBuilder(obj.name); | ||
+ | Transform current = obj.transform.parent; | ||
+ | |||
+ | while (current != null) | ||
+ | { | ||
+ | path.Insert(0, $"{current.name}/"); | ||
+ | current = current.parent; | ||
+ | } | ||
+ | |||
+ | return path.ToString(); | ||
} | } | ||
} | } | ||
</pre> | </pre> |
2024年8月11日 (日) 20:16時点における版
全シーンの、指定したGameObjectをPrefabに変更するコード
using UnityEditor; using UnityEngine; using UnityEditor.SceneManagement; using UnityEngine.SceneManagement; using System.IO; using System.Text; public class ReplaceObjectWithPrefab : EditorWindow { private string directoryPath = "Assets/Scenes"; // デフォルト値 private string objectName = "ReturnButton"; // デフォルト値 private GameObject prefab; [MenuItem("Tools/Replace Object With Prefab")] public static void ShowWindow() { GetWindow<ReplaceObjectWithPrefab>("Replace Object With Prefab"); } private void OnGUI() { GUILayout.Label("Directory and Object Settings", EditorStyles.boldLabel); directoryPath = EditorGUILayout.TextField("Directory Path", directoryPath); objectName = EditorGUILayout.TextField("Object Name", objectName); prefab = (GameObject)EditorGUILayout.ObjectField("Prefab", prefab, typeof(GameObject), false); if (GUILayout.Button("Replace Objects in Scenes")) { ReplaceObjectsInScenes(); } } private void ReplaceObjectsInScenes() { if (string.IsNullOrEmpty(directoryPath) || string.IsNullOrEmpty(objectName) || prefab == null) { Debug.LogError("Please specify the directory path, object name, and prefab."); return; } // 現在のシーンを保存し、シーンパスを記録 Scene currentScene = EditorSceneManager.GetActiveScene(); string currentScenePath = currentScene.path; if (currentScene.isDirty) { EditorSceneManager.SaveScene(currentScene); } // ディレクトリ内のすべてのシーンファイルを取得 string[] sceneFiles = Directory.GetFiles(directoryPath, "*.unity", SearchOption.AllDirectories); foreach (string scenePath in sceneFiles) { // シーンを開く Scene scene = EditorSceneManager.OpenScene(scenePath); if (!scene.IsValid()) { Debug.LogError($"Invalid scene path: {scenePath}"); continue; } // オブジェクトを探す GameObject targetObject = GameObject.Find(objectName); if (targetObject == null) { Debug.LogWarning($"Object '{objectName}' not found in scene: {scenePath}"); continue; } // ヒエラルキーでの位置情報を保持する Transform parentTransform = targetObject.transform.parent; Vector3 localPosition = targetObject.transform.localPosition; Quaternion localRotation = targetObject.transform.localRotation; Vector3 localScale = targetObject.transform.localScale; int siblingIndex = targetObject.transform.GetSiblingIndex(); // オブジェクトをプレハブに置き換える GameObject newObject = (GameObject)PrefabUtility.InstantiatePrefab(prefab, scene); // 新しいオブジェクトを元の位置に配置する newObject.transform.SetParent(parentTransform); newObject.transform.localPosition = localPosition; newObject.transform.localRotation = localRotation; newObject.transform.localScale = localScale; newObject.transform.SetSiblingIndex(siblingIndex); // 旧オブジェクトを削除 DestroyImmediate(targetObject); // ヒエラルキーパスを生成 string hierarchyPath = GetHierarchyPath(newObject); // シーンパスとヒエラルキーパスをログに出力 Debug.Log($"Replaced object in scene: {scenePath}\nHierarchy Path: {hierarchyPath}"); // シーンを保存する EditorSceneManager.MarkSceneDirty(scene); EditorSceneManager.SaveScene(scene); } // 最後に元のシーンを再度開く if (!string.IsNullOrEmpty(currentScenePath)) { EditorSceneManager.OpenScene(currentScenePath); } } // ヒエラルキーパスを生成するメソッド private string GetHierarchyPath(GameObject obj) { StringBuilder path = new StringBuilder(obj.name); Transform current = obj.transform.parent; while (current != null) { path.Insert(0, $"{current.name}/"); current = current.parent; } return path.ToString(); } }