Files
Main/Assets/Editor/DIY/Custom/ChangeUILayerEditor.cs
2025-01-25 04:38:09 +08:00

104 lines
3.7 KiB
C#

using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace Thousandto.DIY
{
public class ChangeUILayerEditor : EditorWindow
{
[MenuItem("Ares/ChangeUILayerEditor")]
static void Open()
{
if (Application.isPlaying)
{
return;
}
var win = (ChangeUILayerEditor)EditorWindow.GetWindow(typeof(ChangeUILayerEditor));
win.Show();
}
private string _prefabPath = string.Empty;
private string[] _layerNames = null;
private int _orginLayer = 0;
private int _targetLayer = 0;
void OnGUI()
{
if(_layerNames == null)
{
var layerList = new List<string>();
for (int i = 0; i < 32; ++i)
{
var layerName = LayerMask.LayerToName(i);
if(!string.IsNullOrEmpty(layerName))
{
layerList.Add(layerName);
}
}
_layerNames = layerList.ToArray();
}
EditorGUILayout.BeginVertical();
EditorGUILayout.LabelField("选择路径");
EditorGUILayout.Space();
EditorGUILayout.LabelField("需要修改层的Prefab路径");
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("浏览", GUILayout.Width(50)))
{
_prefabPath = EditorUtility.OpenFolderPanel("Change Layer Prefab Path", _prefabPath, "");
}
if (string.IsNullOrEmpty(_prefabPath))
{
EditorGUILayout.LabelField("null...");
}
else
{
EditorGUILayout.LabelField(_prefabPath);
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.EndVertical();
EditorGUILayout.BeginVertical();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("原层");
_orginLayer = EditorGUILayout.Popup(_orginLayer, _layerNames);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("目标层");
_targetLayer = EditorGUILayout.Popup(_targetLayer, _layerNames);
EditorGUILayout.EndHorizontal();
EditorGUILayout.EndVertical();
if (GUILayout.Button("转换", GUILayout.Width(50)))
{
if(_orginLayer == _targetLayer)
{
return;
}
if (string.IsNullOrEmpty(_prefabPath))
return;
int oriLayer = LayerMask.NameToLayer(_layerNames[_orginLayer]);
int targetLayer = LayerMask.NameToLayer(_layerNames[_targetLayer]);
string originLayerText = string.Format("m_Layer: {0}", oriLayer);
string targetLayerText = string.Format("m_Layer: {0}", targetLayer);
if (Directory.Exists(_prefabPath))
{
DirectoryInfo direction = new DirectoryInfo(_prefabPath);
FileInfo[] files = direction.GetFiles("*.prefab", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; ++i)
{
var path = files[i].FullName;
var prefabText = File.ReadAllText(path);
prefabText = prefabText.Replace(originLayerText, targetLayerText);
File.WriteAllText(path, prefabText);
}
}
}
}
}
}