Last active
January 24, 2023 11:44
-
-
Save R-Sudo/133dd1701dffb4dc0a6bf498e2baf758 to your computer and use it in GitHub Desktop.
プレハブの一括操作・保存用エディタ拡張ウィンドウ
This file contains 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 System.IO; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.UI; | |
namespace Editor | |
{ | |
public class PrefabEditEditorWindow : EditorWindow | |
{ | |
private DefaultAsset targetFolder; | |
private string targetFilePath; | |
private Sprite convertSprite; | |
[MenuItem("PrefabEdit/プレハブ変換")] | |
private static void OpenWindow() | |
{ | |
var window = GetWindow<PrefabEditEditorWindow>("プレハブ変換"); | |
window.Show(); | |
} | |
private void OnGUI() | |
{ | |
var currentTargetFolder = (DefaultAsset)EditorGUILayout.ObjectField("対象フォルダ", targetFolder, typeof(DefaultAsset), false); | |
if (currentTargetFolder != null) | |
{ | |
targetFolder = currentTargetFolder; | |
targetFilePath = AssetDatabase.GetAssetOrScenePath(targetFolder); | |
} | |
var currentConvertSprite = (Sprite)EditorGUILayout.ObjectField("置換用画像", convertSprite, typeof(Sprite), false); | |
if (currentConvertSprite != null) | |
{ | |
convertSprite = currentConvertSprite; | |
} | |
if (GUILayout.Button("Convert")) | |
{ | |
ConvertAssets(); | |
} | |
} | |
private void ConvertAssets() | |
{ | |
Debug.Log("Start Convert Assets"); | |
if (string.IsNullOrEmpty(targetFilePath)) | |
{ | |
return; | |
} | |
var assetGuids = AssetDatabase.FindAssets("t:prefab", new[] { targetFilePath }); | |
foreach (var assetGuid in assetGuids) | |
{ | |
var path = AssetDatabase.GUIDToAssetPath(assetGuid); | |
Debug.Log($"EditAssetPath: {path}"); | |
EditAsset(path); | |
} | |
AssetDatabase.SaveAssets(); | |
} | |
private void EditAsset(string path) | |
{ | |
// 画像変更処理 | |
var prefabObject = PrefabUtility.LoadPrefabContents(path); | |
prefabObject.GetComponent<Image>().sprite = convertSprite; | |
PrefabUtility.UnloadPrefabContents(prefabObject); | |
// リネーム処理 | |
var nameOld = Path.GetFileNameWithoutExtension(path); | |
var nameNew = $"{nameOld}_edit"; | |
AssetDatabase.RenameAsset(path, nameNew); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment