Skip to content

Commit

Permalink
複数のアセットブックマークが作れるようになりました
Browse files Browse the repository at this point in the history
  • Loading branch information
rngtm committed Dec 22, 2016
1 parent 7b3b178 commit bd92fbf
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 27 deletions.
53 changes: 53 additions & 0 deletions Assets/AssetBookmarker/Core/Editor/GenericDataGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
///-----------------------------------------
/// AssetBookmarker
/// @ 2016 RNGTM(https://github.com/rngtm)
///-----------------------------------------
namespace AssetBookmarker
{
using System.IO;
using UnityEngine;
using UnityEditor;

/// <summary>
/// ファイルの作成を行うクラス
/// </summary>
public static class GenericDataGenerator
{
public static T CreateData<T>(string rootFolderName, string saveFolderRelativePath, string dataName) where T : ScriptableObject
{
var guid = AssetDatabase.FindAssets(rootFolderName)[0];
var rootDirectory = AssetDatabase.GUIDToAssetPath(guid);
var directory = Path.Combine(rootDirectory, saveFolderRelativePath);
if (string.IsNullOrEmpty(rootDirectory))
{
directory = "Assets";
}

var name = dataName + ".asset";
var path = Path.Combine(directory, name);
var instance = ScriptableObject.CreateInstance<T>();
ProjectWindowUtil.CreateAsset(instance, path);

return instance;
}

public static T CreateDataImmediately<T>(string rootFolderName, string saveFolderRelativePath, string dataName) where T : ScriptableObject
{
var guid = AssetDatabase.FindAssets(rootFolderName)[0];
var rootDirectory = AssetDatabase.GUIDToAssetPath(guid);
var directory = Path.Combine(rootDirectory, saveFolderRelativePath);
if (string.IsNullOrEmpty(rootDirectory))
{
directory = "Assets";
}

var name = dataName + ".asset";
var path = Path.Combine(directory, name);
var instance = ScriptableObject.CreateInstance<T>();
AssetDatabase.CreateAsset(instance, path);
Debug.Log("Create: " + path, instance);

return instance;
}
}
}
12 changes: 12 additions & 0 deletions Assets/AssetBookmarker/Core/Editor/GenericDataGenerator.cs.meta

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

6 changes: 2 additions & 4 deletions Assets/AssetBookmarker/Core/Scripts/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
///-----------------------------------------
namespace AssetBookmarker
{
using UnityEngine;

/// <summary>
/// 設定
/// </summary>
public class Config
public static class Config
{
// バージョン情報
public const string VERSION_TEXT = "Asset Bookmarker v2.0";
public const string VERSION_TEXT = "Asset Bookmarker v2.1";

// Menuテキスト
public const string GUI_MENU_TEXT_OPEN_BOOKMARK_PROJECT = "Tools/Asset Bookmarker/Assets";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
///-----------------------------------------
namespace AssetBookmarker.Hierarchy
{
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
Expand Down Expand Up @@ -149,7 +150,9 @@ static void Open()
static void RegisterSelectionToPalette()
{
var data = DataLoader.LoadData();
data.SearchInfos.Add(new SearchInfo { Text = Selection.activeObject.name } );
data.SearchInfos.AddRange(
Selection.gameObjects.Select(go => new SearchInfo { Text = go.name })
);
EditorUtility.SetDirty(data);
Open();
}
Expand Down
Binary file modified Assets/AssetBookmarker/Project/Data/BookmarkData.asset
Binary file not shown.
4 changes: 2 additions & 2 deletions Assets/AssetBookmarker/Project/Data/BookmarkData.asset.meta

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

150 changes: 134 additions & 16 deletions Assets/AssetBookmarker/Project/Editor/GUI/ProjectBookmarkWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
///-----------------------------------------
namespace AssetBookmarker.Project
{
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditorInternal;

/// <summary>
Expand All @@ -14,31 +16,114 @@ namespace AssetBookmarker.Project
public class ProjectBookmarkWindow : EditorWindow
{
/// <summary>
/// Bookmarkデータ
/// Label領域の大きさ
/// </summary>
private ProjectBookmarkData bookmarkData;
private const float LabelWidth = 68f;

/// <summary>
/// ボタンの大きさ
/// </summary>
private const float ButtonWidth = 46f;

/// <summary>
/// ポップアップ表示用の文字列
/// </summary>
private string[] popupDisplayedOptions;

/// <summary>
/// Bookmark表示用のReorderableList
/// </summary>
private ReorderableList bookmarkList;

/// <summary>
/// 現在選択しているブックマーク
/// </summary>
[SerializeField] private int currentBookmarkIndex = 0;

/// <summary>
/// 現在選択しているブックマークデータ名
/// </summary>
[SerializeField] private string currentBookmarkName;

/// <summary>
/// ブックマーク情報
/// </summary>
private ProjectBookmarkData[] bookmarkDatas;

private static EditorWindow window;
private static bool needReloadData = false;
private static Object[] willRegisterAssets = null;

/// <summary>
/// アセットのロード時に呼ばれる
/// </summary>
[DidReloadScripts]
[InitializeOnLoadMethodAttribute]
public static void OnLoadAssets()
{
needReloadData = true;
}

/// <summary>
/// ウィンドウの描画処理
/// </summary>
void OnGUI()
private void OnGUI()
{
if (this.bookmarkData == null)
if (window == null)
{
this.bookmarkData = DataLoader.LoadData();
window = this;
}


if (needReloadData)
{
needReloadData = false;
this.ReloadDatas();
}

if (bookmarkDatas == null)
{
this.ReloadDatas();
}

if (this.bookmarkList == null)
{
this.RebuildBookmarkList();
}

if (willRegisterAssets != null)
{
var data = this.bookmarkDatas[this.currentBookmarkIndex];
data.Assets.AddRange(willRegisterAssets);
EditorUtility.SetDirty(data);
willRegisterAssets = null;
}

EditorGUILayout.LabelField(Config.GUI_WINDOW_PROJECT_TEXT_OVERVIEW);
EditorGUILayout.BeginHorizontal();
EditorGUI.BeginChangeCheck();
EditorGUILayout.LabelField("Bookmark", GUILayout.Width(LabelWidth));
int index = EditorGUILayout.Popup(this.currentBookmarkIndex, this.popupDisplayedOptions);
if (EditorGUI.EndChangeCheck())
{
if (index < bookmarkDatas.Length)
{
this.currentBookmarkIndex = index;
this.currentBookmarkName = this.bookmarkDatas[this.currentBookmarkIndex].name;
this.RebuildBookmarkList();
}
else
{
DataGenerator.CreateBookmarkData();
this.ReloadDatas();
this.RebuildBookmarkList();
}
}
if (GUILayout.Button("Select", EditorStyles.miniButton, GUILayout.Width(ButtonWidth)))
{
EditorGUIUtility.PingObject(bookmarkDatas[this.currentBookmarkIndex]);
}
EditorGUILayout.EndHorizontal();

this.bookmarkList.DoLayoutList();

CustomUI.VersionLabel();
Expand All @@ -47,15 +132,50 @@ void OnGUI()
/// <summary>
/// ReorderableListを作成する
/// </summary>
void RebuildBookmarkList()
private void RebuildBookmarkList()
{
this.bookmarkList = CreateBookmarkList(bookmarkDatas[this.currentBookmarkIndex]);
}

/// <summary>
/// データのリロード
/// </summary>
private void ReloadDatas()
{
this.bookmarkList = this.CreateBookmarkList();
this.bookmarkDatas = DataLoader.LoadData();
if (this.bookmarkDatas.Length == 0)
{
Debug.LogWarning("bookmark not found");
DataGenerator.CreateBookmarkDataImmediately();
this.bookmarkDatas = DataLoader.LoadData();
this.RebuildBookmarkList();
}

this.popupDisplayedOptions = this.bookmarkDatas
.Select(b => b.name)
.Concat(new[] { "", "New..." }).ToArray();

var selection = this.bookmarkDatas
.Select((d,i) => new { Data = d, Index = i })
.FirstOrDefault(item => item.Data.name == this.currentBookmarkName);

if (selection != null)
{
this.currentBookmarkIndex = selection.Index;
}
else
{
// 直前に選択していたブックマークが見つからなかった場合は選択リセット
this.currentBookmarkIndex = 0;
}

this.currentBookmarkName = this.bookmarkDatas[this.currentBookmarkIndex].name;
}

/// <summary>
/// ReorderableListを作成する
/// </summary>
private ReorderableList CreateBookmarkList()
static private ReorderableList CreateBookmarkList(ProjectBookmarkData bookmarkData)
{
var list = new ReorderableList(bookmarkData.Assets, typeof(Object));

Expand Down Expand Up @@ -105,7 +225,8 @@ private ReorderableList CreateBookmarkList()

if (GUI.Button(buttonRect, "-"))
{
this.DoRemoveButton(list, index);
DoRemoveButton(list, index);
EditorUtility.SetDirty(bookmarkData);
}
};

Expand All @@ -120,13 +241,12 @@ private ReorderableList CreateBookmarkList()
/// <summary>
/// 要素の削除
/// </summary>
public void DoRemoveButton(ReorderableList list, int index)
static private void DoRemoveButton(ReorderableList list, int index)
{
EditorApplication.delayCall += () =>
{
list.list.RemoveAt(index);
this.Repaint();
EditorUtility.SetDirty(this.bookmarkData);
window.Repaint();
};
}

Expand All @@ -147,9 +267,7 @@ static void Open()
[MenuItem(Config.GUI_MENU_TEXT_REGISTER_PROJECT, false, 10001)]
static void RegisterSelection()
{
var data = DataLoader.LoadData();
data.Assets.AddRange(Selection.objects);
EditorUtility.SetDirty(data);
willRegisterAssets = Selection.objects.Where(obj => AssetDatabase.IsMainAsset(obj)).ToArray();
Open();
}

Expand Down
27 changes: 27 additions & 0 deletions Assets/AssetBookmarker/Project/Editor/Model/ExportConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
///-----------------------------------------
/// AssetBookmarker
/// @ 2016 RNGTM(https://github.com/rngtm)
///-----------------------------------------
namespace AssetBookmarker.Project
{
/// <summary>
/// Export設定
/// </summary>
public static class ExportConfig
{
/// <summary>
/// AssetBookmarkerのルートフォルダ名
/// </summary>
public const string RootFolderName = "AssetBookmarker";

/// <summary>
/// Bookmarkデータの保存先の相対パス
/// </summary>
public const string SaveFolderRelativePath = "Project/Data";

/// <summary>
/// 新規Bookmarkデータのデフォルトの名前
/// </summary>
public const string DefaultNewTodoName = "NewBookmarkData";
}
}
12 changes: 12 additions & 0 deletions Assets/AssetBookmarker/Project/Editor/Model/ExportConfig.cs.meta

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

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace AssetBookmarker.Project
/// </summary>
public class ProjectBookmarkData : ScriptableObject
{
[SerializeField] private List<Object> assets;
[SerializeField] private List<Object> assets = new List<Object>();

/// <summary>
/// ブックマークとして登録しているアセット
Expand Down
Loading

0 comments on commit bd92fbf

Please sign in to comment.