1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Unity Editor拡張テンプレート

Posted at

コピペ用

using UnityEditor;
using UnityEngine;

public class EditorTemplate : EditorWindow
{
    private Editor _editor;
    public /*ScriptableObjectのクラス名*/ sObject;

    public /*enumの型*/ eObject;
        
    private Vector2 boxSizeOffset = new Vector2(100, 350);

    private Vector2 _scrollPosition = Vector2.zero;

    [MenuItem("Hoge/Edit")]
    static void Open()
    {
        var window = GetWindow<EditorTemplate>();
        window.titleContent = new GUIContent("/*windowの名前*/");
    }

    /// <Summary>
    /// ウィンドウのパーツを表示する
    /// </Summary>
    void OnGUI()
    {
        boxSizeOffset = EditorGUILayout.Vector2Field("windowOffset", boxSizeOffset);
        
        //ScriptableObjectの取得
        var guids = UnityEditor.AssetDatabase.FindAssets("t:/*ScriptableObjectのクラス名*/");
        if (guids.Length == 0)
        {
            throw new System.IO.FileNotFoundException("/*ScriptableObjectのクラス名*/ does not found");
        }

        var path = AssetDatabase.GUIDToAssetPath(guids[0]);
        sObject = AssetDatabase.LoadAssetAtPath</*ScriptableObjectのクラス名*/>(path);

        //ボタン
        if (GUILayout.Button("ボタン"))
        {
            //ボタンが押されたときの処理
        }


        //Editorの要素が更新されたかチェックする
        if (EditorGUI.EndChangeCheck())
        {
            if (sObject != null)
            {
                //要素を更新
                _editor = Editor.CreateEditor(sObject);
                EditorUtility.SetDirty(sObject);
            }
        }

        //レイアウトを調整
        EditorGUILayout.Space();

        
        //ポップアップのGUI
        string[] strings = new[] { "Hoge", "Fuga", "Foo" };
        index = EditorGUILayout.Popup("ステージセレクト", index, strings);

        //enumのポップアップGUI
        eObject = (/*enumの型*/)EditorGUILayout.EnumPopup(eObject);

        
        
        //特定のクラスの要素をEditorWindowで指定する
        var hoge = (/*クラス名*/)EditorGUILayout.ObjectField("", /*クラス名*/,
            typeof(/*クラス名*/), true);

        
        //Editorの更新
        if (_editor != null)
        {
            //特定のクラスの要素の表示・変更処理
            var database = (/*クラス名*/)_editor.target;
            
            
            //スクロールの開始処理
            _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition);

            /* EditorではBegin ~ Endに囲まれた範囲のものに処理が適応される
             * ここではScrollViewの処理がBegin ~ Endに囲まれたオブジェクトに適応される
             */
            
            //スクロールの終了処理
            EditorGUILayout.EndScrollView();

            //縦揃え
            EditorGUILayout.BeginVertical();
            EditorGUILayout.EndVertical();

            //横揃え
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.EndHorizontal();

            //中央揃え
            GUILayout.FlexibleSpace();
            GUILayout.FlexibleSpace();
                

            //EditorWindowで更新したScriptableObjectを確実に保存する
            if (GUILayout.Button("保存", GUILayout.Height(30), GUILayout.Width(60)))
            {
                // ScriptableObjectのパスは 'Assets/' から始まる相対パス
                ScriptableObject obj =
                    AssetDatabase.LoadAssetAtPath("Assets/Resources//*ScriptableObjectの名前*/.asset", typeof(ScriptableObject)) as
                        ScriptableObject;
                
                EditorUtility.SetDirty(obj);

                AssetDatabase.SaveAssets();
            }
        }
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?