目前包含功能主要有:
- 设置预制tag
- 设置预制字体。
- 右键复制对象路径(绝对路径和相对路径)
面板截图如下:
直接上代码:
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class ToolsExtend : EditorWindow
{
[MenuItem("Tools/Tools Extend")]
public static void ShowPanel()
{
GetWindow<ToolsExtend>().Show();
}
private void OnGUI()
{
DrawSetTagGizmos();
DrawSetFontGizmos();
}
#region Set Tag
private static bool isSetChildTag = false;
private static string newTag = string.Empty;
private static GameObject prefabTag = null;
private void DrawSetTagGizmos()
{
GUILayout.Label("设置预制Tag:");
GUILayout.BeginHorizontal();
GUILayout.Label("Target:", GUILayout.Width(60));
prefabTag = (GameObject)EditorGUILayout.ObjectField(prefabTag, typeof(GameObject), true);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Tag:", GUILayout.Width(60));
newTag = EditorGUILayout.TextField(newTag);
GUILayout.EndHorizontal();
isSetChildTag = EditorGUILayout.Toggle("是否设置子节点:", isSetChildTag);
if (GUILayout.Button("设置预制Tag"))
{
if (string.IsNullOrEmpty(newTag))
{
Debug.LogError("Tag can't be null or empty!");
return;
}
if (prefabTag == null)
{
Debug.LogError("Prefab can't be null!");
return;
}
prefabTag.tag = newTag;
if (isSetChildTag)
{
SetChildTags(prefabTag.transform);
}
}
GUILayout.Space(20);
}
private static void SetChildTags(Transform target)
{
if (target == null)
{
return;
}
foreach (Transform item in target)
{
item.tag = newTag;
SetChildTags(item);
}
}
#endregion
#region Set Font
private static Font font = null;
private static bool isSetChildFont = false;
private static GameObject prefabFont = null;
private void DrawSetFontGizmos()
{
GUILayout.Label("设置预制字体:");
GUILayout.BeginHorizontal();
GUILayout.Label("Target:", GUILayout.Width(60));
prefabFont = (GameObject)EditorGUILayout.ObjectField(prefabFont, typeof(GameObject), true);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Font:", GUILayout.Width(60));
font = (Font)EditorGUILayout.ObjectField(font, typeof(Font), true);
GUILayout.EndHorizontal();
isSetChildFont = EditorGUILayout.Toggle("是否设置子节点:", isSetChildFont);
if (GUILayout.Button("设置预制Font"))
{
if (font == null)
{
Debug.LogError("Font can't be null!");
return;
}
if (prefabFont == null)
{
Debug.LogError("Prefab can't be null!");
return;
}
var text = prefabFont.GetComponent<Text>();
if (text)
{
text.font = font;
}
if (isSetChildFont)
{
SetChildFont(prefabFont.transform);
}
Debug.Log("设置预制Font成功!");
}
GUILayout.Space(20);
}
private static void SetChildFont(Transform target)
{
if (target == null)
{
return;
}
foreach (Transform item in target)
{
var text = item.GetComponent<Text>();
if (text)
{
text.font = font;
}
SetChildFont(item);
}
}
#endregion
#region Copy Path
[MenuItem("Assets/My Tools/Copy GameObject Full Path", false, 152)]
private static void CopyFullPath()
{
var path = AssetDatabase.GetAssetOrScenePath(Selection.activeGameObject);
GUIUtility.systemCopyBuffer = Path.GetFullPath(path);
}
[MenuItem("Assets/My Tools/Copy GameObject Relative Path", false, 152)]
private static void CopyRelativePath()
{
var path = AssetDatabase.GetAssetOrScenePath(Selection.activeGameObject).Replace('/', '\\');
GUIUtility.systemCopyBuffer = path;
}
#endregion
}
备注:如果提交时发现预制未修改,但是在inspector面板却已经改变的情况下,在编辑器中直接Ctrl+s保存一下就好。