Last active
January 9, 2023 14:24
-
-
Save atori708/5fff53686c7557d49cfa1ee5d2004ca0 to your computer and use it in GitHub Desktop.
【Unity】アセットのmetaファイルの中身を見れるボタンを用意する
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; | |
/// <summary> | |
/// ProjectView拡張 | |
/// ボタンを追加する | |
/// </summary> | |
public class ProjectView | |
{ | |
private static Vector2 cacheSize; | |
private static GUIContent cacheContent; | |
private static GUIStyle cacheStyle; | |
[InitializeOnLoadMethod] | |
private static void ProjectWindow() | |
{ | |
EditorApplication.projectWindowItemOnGUI += ProjectWindowGUI; | |
} | |
private static void ProjectWindowGUI(string guid, Rect rect) | |
{ | |
if (string.IsNullOrEmpty(guid)) { | |
return; | |
} | |
var asset = AssetDatabase.GUIDToAssetPath(guid); | |
if (IsFolder(asset)) { | |
return; | |
} | |
if (cacheStyle == null) { | |
cacheStyle = (GUIStyle)"Box"; | |
cacheContent = EditorGUIUtility.IconContent("console.infoicon.inactive.sml"); | |
cacheSize = cacheStyle.CalcSize(cacheContent); | |
} | |
rect.xMin = rect.xMax - cacheSize.x; | |
// ボタンを押したらmetaファイルの詳細を開く | |
if (GUI.Button(rect, cacheContent, EditorStyles.iconButton)) { | |
PopupWindow.Show(rect, new AssetMetaInfoPopupWindow(asset)); | |
} | |
} | |
/// <summary> | |
/// フォルダかどうか判定 | |
/// </summary> | |
private static bool IsFolder(string path) | |
{ | |
if (string.IsNullOrEmpty(path)) { | |
return false; | |
} | |
return File.GetAttributes(path).HasFlag(FileAttributes.Directory); | |
} | |
} |
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 UnityEngine; | |
using UnityEditor; | |
using System.IO; | |
/// <summary> | |
/// metaファイル詳細表示のポップアップウィンドウ | |
/// </summary> | |
public class AssetMetaInfoPopupWindow : PopupWindowContent | |
{ | |
string _assetPath; | |
string _metaText; | |
public AssetMetaInfoPopupWindow(string assetPath) : base() | |
{ | |
_assetPath = assetPath; | |
using (var sr = new StreamReader(assetPath + ".meta")) { | |
_metaText = sr.ReadToEnd(); | |
} | |
} | |
public override Vector2 GetWindowSize() | |
{ | |
return new Vector2(300, 200); | |
} | |
public override void OnGUI(Rect rect) | |
{ | |
if (GUILayout.Button("CopyGUID")) { | |
GUIUtility.systemCopyBuffer = AssetDatabase.AssetPathToGUID(_assetPath); | |
} | |
rect.xMin += 2; | |
rect.yMin += EditorGUIUtility.singleLineHeight; | |
// 一応コピペできるようにSelectableLabelを使う | |
EditorGUI.SelectableLabel(rect, _metaText); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment