Skip to content

Instantly share code, notes, and snippets.

@a1678991
Last active November 10, 2021 15:13
Show Gist options
  • Save a1678991/ec325f49fd1aa467397729c254383514 to your computer and use it in GitHub Desktop.
Save a1678991/ec325f49fd1aa467397729c254383514 to your computer and use it in GitHub Desktop.
UnityのAssetsフォルダを右クリックからAvestaで開けるやつ
/*
* © @a1678991
*/
using System;
using System.Collections.Generic;
using System.Text;
using UnityEditor;
using UnityEngine;
public class ShowInAvesta : EditorWindow
{
private const uint ConfigVersion = 1; // この拡張機能のバージョン
[Serializable]
private struct Settings
{
public uint version;
public string avestaPath;
public OpenMode openMode;
public int openAssetLimit; // 一度に開けるアセットの数(誤爆防止用)
public Settings(string avestaPath, OpenMode openMode, int openAssetLimit)
{
this.version = ConfigVersion;
this.avestaPath = avestaPath;
this.openMode = openMode;
this.openAssetLimit = openAssetLimit;
}
}
private enum OpenMode
{
Goto, // 今開いてるタブで開く
Open, // 新しいタブを追加し、そのタブのみ表示する
Switch, // 新しいタブを追加し、フォーカスする
Append, // 新しいタブを追加するが、フォーカスは変わらない
Reserve, // 新しいタブを非表示で追加する
Replace, // 新しいタブを追加し、元のタブを隠す
}
private static readonly Dictionary<OpenMode, string> OpenModeCommand = new Dictionary<OpenMode, string>
{
{ OpenMode.Goto, "-goto" },
{ OpenMode.Open, "-open" },
{ OpenMode.Switch, "-switch" },
{ OpenMode.Append, "-append" },
{ OpenMode.Reserve, "-reverse" },
{ OpenMode.Replace, "-replace" },
};
[MenuItem("Assets/Show in Avesta")]
private static void ShowInAvestaFunc()
{
var settings = LoadConfig();
var selectedAssets = Selection.assetGUIDs;
if (settings.openAssetLimit != 0 && selectedAssets.Length >= settings.openAssetLimit)
{
// Show Confirmation Dialogue in future
Debug.LogWarning($"ShowInAvesta: {settings.openAssetLimit}個以上のアセットを一度に開こうとしたため、実行しませんでした。");
return;
}
var assetPaths = new List<string>();
foreach (var assetGuid in selectedAssets)
{
var path = AssetDatabase.GUIDToAssetPath(assetGuid);
assetPaths.Add(path);
}
StartAvesta(settings.avestaPath, settings.openMode, assetPaths);
}
private static void StartAvesta(string avestaPath, OpenMode openMode, IEnumerable<string> assetPaths)
{
var sb = new StringBuilder();
foreach (var path in assetPaths)
{
sb.Append(OpenModeCommand[openMode]).Append(" ").Append('"').Append(Application.dataPath.Substring(0, Application.dataPath.Length -6)).Append(path).Append('"').Append(" ");
}
var process = new System.Diagnostics.Process();
var startInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = avestaPath,
Arguments = sb.ToString()
};
process.StartInfo = startInfo;
process.Start();
}
private static void SaveConfig(Settings settings)
{
string json;
try
{
json = JsonUtility.ToJson(settings);
}
catch (Exception e)
{
Debug.LogErrorFormat($"ShowInAvesta: 設定の保存に失敗しました\n{e}");
return;
}
EditorPrefs.SetString(nameof(ShowInAvesta), json);
}
private static Settings LoadConfig()
{
Settings settings;
try
{
var json = EditorPrefs.GetString(nameof(ShowInAvesta));
settings = JsonUtility.FromJson<Settings>(json);
}
catch (Exception e)
{
Debug.LogErrorFormat($"ShowInAvesta: 設定ファイルが読み込めなかったため、初期設定を適用しました\n{e}");
settings = new Settings("", OpenMode.Switch, 0);
SaveConfig(settings);
}
if (settings.version != ConfigVersion)
{
Debug.LogError("ShowInAvesta: 設定ファイルのバージョンが違うため、初期化しました。");
settings = new Settings("", OpenMode.Switch, 0);
SaveConfig(settings);
}
return settings;
}
[MenuItem("Tools/Show in Avesta Settings")]
static void Open()
{
var window = GetWindow<ShowInAvesta>();
window.Init();
window.Show();
}
private Settings _settings;
private void Init()
{
_settings = LoadConfig();
}
void OnGUI()
{
EditorGUILayout.LabelField("Avesta Path");
_settings.avestaPath = EditorGUILayout.TextField(_settings.avestaPath);
EditorGUILayout.LabelField("Open Mode");
_settings.openMode = (OpenMode)EditorGUILayout.EnumPopup(_settings.openMode);
EditorGUILayout.LabelField("Open Asset Limit (0 = Unlimited)");
_settings.openAssetLimit = EditorGUILayout.IntField(_settings.openAssetLimit);
EditorGUILayout.Space();
if (GUILayout.Button("Save"))
{
SaveConfig(_settings);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment