原理
使用关键码对文件夹名称和描述进行拆分,拆分后通过ScriptableObject存储文件夹信息
运行效果图
关键代码
using System.Collections.Generic;
using UnityEngine;
public class ProjectPanelExtensionSetting : ScriptableObject
{
[Header("是否显示扩展内容")] public bool isDisplay = true;
[Header("描述特征码")] public char keyCode='#';
[Header("字体颜色")]public Color fontColor=Color.green;
[Header("背景颜色")]public Color backgroundColor=Color.white;
[Header("字体样式")]
public TextAnchor textAnchor= TextAnchor.MiddleCenter;
public FontStyle fontStyle= FontStyle.Normal;
[Header("文件夹信息")]
public List<FolderInfo> Folder;
}
[System.Serializable]
public class FolderInfo
{
public string FolderName;
public string Description;
}
using System.Linq;
using UnityEditor;
using UnityEngine;
#if UNITY_EDITOR
[InitializeOnLoad]
public class ProjectPanelExtension
{
private static readonly ProjectPanelExtensionSetting ProjectPanelExtensionSetting;
private const string FileName = "ProjectPanelSetting";
static ProjectPanelExtension()
{
var dataArray = AssetDatabase.FindAssets(FileName);
if (dataArray.Length >= 1)
{
var path = AssetDatabase.GUIDToAssetPath(dataArray[0]);
ProjectPanelExtensionSetting = AssetDatabase.LoadAssetAtPath<ProjectPanelExtensionSetting>(path);
EditorApplication.projectWindowItemOnGUI += ProjectWindowItemOnGUI;
}
}
// 根据给定的guid在Project窗口上绘制一个项目文件夹项
private static void ProjectWindowItemOnGUI(string guid, Rect rect)
{
if (!ProjectPanelExtensionSetting.isDisplay) return;
// 将guid转换为对应的资产路径
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
// 检查是否是一个有效的文件夹路径且不是根目录
if (AssetDatabase.IsValidFolder(assetPath) && !assetPath.Equals("Assets"))
{
// 获取文件夹名称
string folderName = System.IO.Path.GetFileName(assetPath);
// 检查文件夹名称是否包含ProjectFolderData.keyCode
if (folderName.Contains(ProjectPanelExtensionSetting.keyCode))
{
// 根据ProjectFolderData.keyCode将文件夹名称分割成两部分
string[] parts = folderName.Split(ProjectPanelExtensionSetting.keyCode);
// 检查两部分是否为空字符串,如果是则返回
if (parts[0].Equals("") || parts[1].Equals("")) return;
// 检查ProjectFolderData.Folder是否不为空
if (ProjectPanelExtensionSetting.Folder.Count >= 1)
{
// 在ProjectFolderData.Folder中查找是否存在同名的文件夹
var existingFolder = ProjectPanelExtensionSetting.Folder.FirstOrDefault(f => f.FolderName.Contains(parts[0]));
// 如果存在则更新文件夹名称和描述
if (existingFolder != null)
{
existingFolder.FolderName = parts[0];
existingFolder.Description = parts[1];
}
// 如果不存在则向ProjectFolderData.Folder中添加新的文件夹
else
{
ProjectPanelExtensionSetting.Folder.Add(new FolderInfo()
{
FolderName = parts[0],
Description = parts[1]
});
}
}
// 如果ProjectFolderData.Folder为空,则向ProjectFolderData.Folder中添加新的文件夹
else
{
ProjectPanelExtensionSetting.Folder.Add(new FolderInfo()
{
FolderName = parts[0],
Description = parts[1]
});
}
// 重命名资产路径的文件夹名称
AssetDatabase.RenameAsset(assetPath, parts[0]);
}
// 如果文件夹名称不包含ProjectFolderData.keyCode,则检查是否存在同名的文件夹
else
{
var existingFolder = ProjectPanelExtensionSetting.Folder.FirstOrDefault(f => f.FolderName.Equals(folderName));
// 如果存在则绘制文件夹的描述UI
if (existingFolder != null)
{
DrawUI(existingFolder.Description, rect);
}
}
}
}
static void DrawUI(string description,Rect rect)
{
const float descriptionWidth = 120f;
rect.x += rect.width - descriptionWidth;
rect.width = descriptionWidth;
var content=new GUIContent(description);
GUIStyle style = new GUIStyle
{
normal =
{
textColor = ProjectPanelExtensionSetting.fontColor
},
fontSize = 11,
fontStyle = ProjectPanelExtensionSetting.fontStyle,
alignment = ProjectPanelExtensionSetting.textAnchor
};
Vector2 size = style.CalcSize(content);
Rect backgroundRect = new Rect(rect.x + (rect.width - size.x) / 2, rect.y + (rect.height - size.y) / 2, size.x+10f, size.y);
EditorGUI.DrawRect(backgroundRect,ProjectPanelExtensionSetting.backgroundColor);
GUI.Label(backgroundRect, content, style);
}
}
#endif