Skip to content

Commit

Permalink
- add: FolderIcon
Browse files Browse the repository at this point in the history
  • Loading branch information
DucNV2000 committed Jul 30, 2024
1 parent 40e689a commit 05250f3
Show file tree
Hide file tree
Showing 53 changed files with 2,981 additions and 29 deletions.
72 changes: 72 additions & 0 deletions Module/ControlPanel/CPFolderIconDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using UnityEditor;
using UnityEngine;
using Virtuesky.FolderIcon.Editor;
using VirtueSky.UtilsEditor;

namespace VirtueSky.ControlPanel.Editor
{
public class CPFolderIconDrawer
{
private static UnityEditor.Editor _editor;
private static FolderIconSettings _settings;

public static void OnEnable()
{
Init();
}

private static void Init()
{
if (_editor != null)
{
_editor = null;
}

_settings = CreateAsset.GetScriptableAsset<FolderIconSettings>();
_editor = UnityEditor.Editor.CreateEditor(_settings);
}

public static void OnDrawFolderIcon()
{
GUILayout.Space(10);
GUILayout.BeginVertical();
GUILayout.Label("FOLDER ICON", EditorStyles.boldLabel);
GUILayout.Space(10);
if (_settings == null)
{
if (GUILayout.Button("Create FolderIconSetting"))
{
_settings = CreateAsset.CreateAndGetScriptableAsset<FolderIconSettings>(
"Assets/_Sunflower/Editor/FolderIcon");
Init();
}
}
else
{
if (_editor == null)
{
EditorGUILayout.HelpBox("Couldn't create the settings editor.",
MessageType.Error);
return;
}
else
{
_editor.OnInspectorGUI();
GUILayout.Space(10);
if (_settings.customIcon)
{
if (GUILayout.Button("Import texture icon folder"))
{
AssetDatabase.ImportPackage(
FileExtension.GetPathInCurrentEnvironent(
"Module/FolderIcon/Editor/PackageIcon/icon_folder.unitypackage"), false);
}
}
}
}


GUILayout.EndVertical();
}
}
}
3 changes: 3 additions & 0 deletions Module/ControlPanel/CPFolderIconDrawer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Module/ControlPanel/ControlPanelWindowEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ private void OnEnable()
statePanelControl = StatePanelControl.About;
CPAdvertisingDrawer.OnEnable();
CPIapDrawer.OnEnable();
CPFolderIconDrawer.OnEnable();
}

private void OnDisable()
Expand Down Expand Up @@ -76,6 +77,7 @@ void DrawButton()
DrawButtonChooseState("Level Editor", StatePanelControl.LevelEditor, () => CPLevelEditorDrawer.OnEnable());
DrawButtonChooseState("Firebase", StatePanelControl.Firebase);
DrawButtonChooseState("Game Service", StatePanelControl.GameService);
DrawButtonChooseState("Folder Icon", StatePanelControl.FolderIcon);
DrawButtonChooseState("Hierarchy", StatePanelControl.Hierarchy);
DrawButtonChooseState("Scripting Define Symbols", StatePanelControl.ScriptDefineSymbols);
DrawButtonChooseState("Register Package", StatePanelControl.RegisterPackage);
Expand Down Expand Up @@ -111,6 +113,9 @@ void DrawContent()
case StatePanelControl.RegisterPackage:
CPRegisterPackageDrawer.OnDrawRegisterPackageByManifest(position);
break;
case StatePanelControl.FolderIcon:
CPFolderIconDrawer.OnDrawFolderIcon();
break;
case StatePanelControl.Hierarchy:
CPHierarchyDrawer.OnDrawQHierarchyEvent(position, this);
break;
Expand Down Expand Up @@ -173,6 +178,7 @@ public enum StatePanelControl
ScriptDefineSymbols,
RegisterPackage,
Hierarchy,
FolderIcon,
GameService,
Extensions,
About,
Expand Down
59 changes: 30 additions & 29 deletions Module/ControlPanel/Wolf.UnityCommon.ControlPanel.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
{
"name": "Wolf.UnityCommon.ControlPanel.Editor",
"rootNamespace": "",
"references": [
"GUID:8579ab42c9ab63d4bac5fb07bd390b46",
"GUID:b1979aef4cc42ae46af23ce2c4c109cc",
"GUID:324caed91501a9c47a04ebfd87b68794",
"GUID:32dbaa332e571bf429b7de517f75f074",
"GUID:0b6289df6f84a6f4b982ff72d23e0273",
"GUID:49674d15b25185649b7ec8ac5d378747",
"GUID:fca7ec166e04dc948b624a983315e2c9",
"GUID:1d60d62a565144e418b3d9c8acc8eb80",
"GUID:fcc03584b8436214db637bda56107579",
"GUID:37f16d35c05c6f34eacb567043cec159",
"GUID:6cd5a82756aabbb4dae8f59b2773a270",
"GUID:b59f3d97c7580944baac8dafbd217904",
"GUID:343a8c0b839a66c43afaf273c5e29583",
"GUID:d25c81a6e03d11c4fb58d263c8aaea84"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
"name": "Wolf.UnityCommon.ControlPanel.Editor",
"rootNamespace": "",
"references": [
"GUID:8579ab42c9ab63d4bac5fb07bd390b46",
"GUID:b1979aef4cc42ae46af23ce2c4c109cc",
"GUID:324caed91501a9c47a04ebfd87b68794",
"GUID:32dbaa332e571bf429b7de517f75f074",
"GUID:0b6289df6f84a6f4b982ff72d23e0273",
"GUID:49674d15b25185649b7ec8ac5d378747",
"GUID:fca7ec166e04dc948b624a983315e2c9",
"GUID:1d60d62a565144e418b3d9c8acc8eb80",
"GUID:fcc03584b8436214db637bda56107579",
"GUID:37f16d35c05c6f34eacb567043cec159",
"GUID:6cd5a82756aabbb4dae8f59b2773a270",
"GUID:b59f3d97c7580944baac8dafbd217904",
"GUID:343a8c0b839a66c43afaf273c5e29583",
"GUID:d25c81a6e03d11c4fb58d263c8aaea84",
"GUID:877be62fd20bbdb489dd86ee21cee87d"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
8 changes: 8 additions & 0 deletions Module/FolderIcon.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Module/FolderIcon/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions Module/FolderIcon/Editor/CustomFolder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.IO;
using UnityEditor;
using UnityEngine;

namespace Virtuesky.FolderIcon.Editor
{
[InitializeOnLoad]
public class CustomFolder
{
static CustomFolder()
{
IconDictionaryCreator.BuildDictionary();
EditorApplication.projectWindowItemOnGUI += DrawFolderIcon;
}

static void DrawFolderIcon(string guid, Rect rect)
{
var path = AssetDatabase.GUIDToAssetPath(guid);
var iconDictionary = IconDictionaryCreator.IconDictionary;

if (path == "" ||
Event.current.type != EventType.Repaint ||
!File.GetAttributes(path).HasFlag(FileAttributes.Directory) ||
!iconDictionary.ContainsKey(Path.GetFileName(path)))
{
return;
}

Rect imageRect;

if (rect.height > 20)
{
imageRect = new Rect(rect.x - 1, rect.y - 1, rect.width + 2, rect.width + 2);
}
else if (rect.x > 20)
{
imageRect = new Rect(rect.x - 1, rect.y - 1, rect.height + 2, rect.height + 2);
}
else
{
imageRect = new Rect(rect.x + 2, rect.y - 1, rect.height + 2, rect.height + 2);
}

var texture = IconDictionaryCreator.IconDictionary[Path.GetFileName(path)];
if (texture == null)
{
return;
}

GUI.DrawTexture(imageRect, texture);
}
}
}
11 changes: 11 additions & 0 deletions Module/FolderIcon/Editor/CustomFolder.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Module/FolderIcon/Editor/FolderIconSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using VirtueSky.Inspector;

namespace Virtuesky.FolderIcon.Editor
{
[HideMonoScript]
public class FolderIconSettings : ScriptableObject
{
public bool setupIconDefault;
public bool customIcon;

[ShowIf(nameof(customIcon)), TableList]
public List<FolderIconData> folderIconDatas;

public void OnValidate()
{
IconDictionaryCreator.BuildDictionary();
}
}

[Serializable]
public class FolderIconData
{
public Texture2D icon;
public string folderName;
}
}
11 changes: 11 additions & 0 deletions Module/FolderIcon/Editor/FolderIconSettings.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions Module/FolderIcon/Editor/IconDictionaryCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using VirtueSky.UtilsEditor;

namespace Virtuesky.FolderIcon.Editor
{
public class IconDictionaryCreator : AssetPostprocessor
{
private const string AssetsPath = "/Module/FolderIcon/Icons";
internal static Dictionary<string, Texture> IconDictionary;

private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets,
string[] movedAssets, string[] movedFromAssetPaths)
{
if (!ContainsIconAsset(importedAssets) &&
!ContainsIconAsset(deletedAssets) &&
!ContainsIconAsset(movedAssets) &&
!ContainsIconAsset(movedFromAssetPaths))
{
return;
}

BuildDictionary();
}

private static bool ContainsIconAsset(string[] assets)
{
foreach (string str in assets)
{
if (ReplaceSeparatorChar(Path.GetDirectoryName(str)) ==
FileExtension.GetPathInCurrentEnvironent(AssetsPath))
{
return true;
}
}

return false;
}

private static string ReplaceSeparatorChar(string path)
{
return path.Replace("\\", "/");
}

internal static void BuildDictionary()
{
var dictionary = new Dictionary<string, Texture>();
var folderIconSettings = CreateAsset.GetScriptableAsset<FolderIconSettings>();
if (folderIconSettings != null)
{
if (folderIconSettings.setupIconDefault)
{
var dir = new DirectoryInfo(FileExtension.GetPathInCurrentEnvironent(AssetsPath));
FileInfo[] info = dir.GetFiles("*.png");
foreach (FileInfo f in info)
{
var texture =
(Texture)AssetDatabase.LoadAssetAtPath(
FileExtension.GetPathInCurrentEnvironent($"{AssetsPath}/{f.Name}"),
typeof(Texture2D));
dictionary.Add(Path.GetFileNameWithoutExtension(f.Name), texture);
}
}

if (folderIconSettings.customIcon)
{
foreach (var folderIconData in folderIconSettings.folderIconDatas)
{
if (folderIconData == null) continue;
if (folderIconData.icon == null || folderIconData.folderName == null) continue;
dictionary.TryAdd(folderIconData.folderName, folderIconData.icon);
}
}
}

IconDictionary = dictionary;
}
}
}
11 changes: 11 additions & 0 deletions Module/FolderIcon/Editor/IconDictionaryCreator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 05250f3

Please sign in to comment.