概要
Unityで作業していると異なるディレクトリにあるファイルを複数選択したい時がよくあります。
結構手間だったので複数パス貼り付けて一気にオブジェクトを選択するウインドウを作りました。
1.SourceTree等でクリップボードへのパスをコピー
2.カスタムウインドウにペースト
3.選択ボタンを押す
この操作でUnity上で簡単に複数オブジェクトの選択ができます。
コード
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// パスからUnity上でまとめてオブジェクトを選択するセレクトするウインドウ.
/// </summary>
public class CustomSelectWindow : EditorWindow {
string text;
[MenuItem ("Custom/PathSelector")]
static void Init () {
EditorWindow.GetWindow (typeof (CustomSelectWindow));
}
void OnGUI() {
EditorGUILayout.HelpBox ("アセットのパスを改行かカンマで区切った文字列をペーストしてください", MessageType.Info);
string prev = text;
text = EditorGUILayout.TextArea (text, GUILayout.Height (120), GUILayout.ExpandWidth (true));
if (GUILayout.Button ("選択", GUILayout.MaxWidth (100))) {
Select(text);
}
}
public static void Select(string paths)
{
paths = paths.Trim ();
string[] split = paths.Split (new char[]{ '\n', ',' });
List<Object> objs = new List<Object>();
foreach (string path in split) {
// Assetsから前は削除
string fromAssets = path.Substring(path.IndexOf("Assets"));
Object obj = AssetDatabase.LoadAssetAtPath (fromAssets, typeof(Object));
if (obj) {
Debug.Log ("Selection Add : " + fromAssets);
objs.Add (obj);
}
}
// 選択
Selection.objects = objs.ToArray();
}
}