[Unity 확장] Windows 환경 변수를 표시하는 편집기 확장

입문


Windows의 환경 변수 값을 확인하려는 경우가 있기 때문입니다.
Windows 환경 변수를 표시하는 Editor Windows를 만들어 보았습니다.

소스 코드


Unity 프로젝트에 편집기 폴더를 만들고 다음 스크립트를 배치합니다.
EnvironmentVariablesViewer.cs
namespace EnvironmentVariablesViewer
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using UnityEngine;
    using UnityEditor;
    using UnityEditorInternal;

    /// <summary>
    /// 環境変数を表示するEditorWindow
    /// </summary>
    public class EnvironmentVariablesViewer : EditorWindow
    {
        const float LabelWidth = 180f;
        const float LabelSpace = 32f;
        private List<ReorderableList> reorderableLists;
        private Vector2 scrollPos = Vector2.zero;


        [MenuItem("Tools/Environment Variables Viewer")]
        static void Open()
        {
            GetWindow<EnvironmentVariablesViewer>();
        }

        void OnGUI()
        {
            GUILayout.Space(1f);
            if (this.reorderableLists == null)
            {
                this.reorderableLists = new List<ReorderableList>();
                this.reorderableLists.Add(CreateReorderableList("環境変数(Process)", EnvironmentVariableTarget.Process));
                this.reorderableLists.Add(CreateReorderableList("環境変数(User)", EnvironmentVariableTarget.User));
                this.reorderableLists.Add(CreateReorderableList("環境変数(Machine)", EnvironmentVariableTarget.Machine));
            }

            this.scrollPos = EditorGUILayout.BeginScrollView(this.scrollPos);
            foreach (var list in this.reorderableLists)
            {
                list.DoLayoutList();
            }
            EditorGUILayout.EndScrollView();
        }

        /// <summary>
        /// ReorderableListの作成
        /// </summary>
        static ReorderableList CreateReorderableList(string headerText, EnvironmentVariableTarget target)
        {
            var list = new List<EnvironmentVariable>();
            foreach (System.Collections.DictionaryEntry item in Environment.GetEnvironmentVariables(target)) // ユーザー環境変数
            {
                list.Add(new EnvironmentVariable { Key = item.Key, Value = item.Value });
            }

            var reorderableList = new ReorderableList(list, typeof(EnvironmentVariable));

            // ヘッダー
            Rect headerRect = default(Rect);
            reorderableList.drawHeaderCallback = (rect) =>
            {
                headerRect = rect;
                EditorGUI.LabelField(rect, headerText);
            };

            // フッター
            reorderableList.drawFooterCallback = (rect) =>
            {
                rect.y = headerRect.y + 3f;
                ReorderableList.defaultBehaviours.DrawFooter(rect, reorderableList);
            };

            // リスト要素
            reorderableList.drawElementCallback = (rect, index, isActive, isFocused) =>
            {
                rect.y += 3f;
                rect.height -= 6f;

                var kRect = new Rect(rect); // key用ラベル
                kRect.width = LabelWidth;

                var vRect = new Rect(rect); // Value用ラベル
                vRect.width = vRect.width - kRect.width - LabelSpace;
                vRect.x += kRect.width + LabelSpace;

                EditorGUI.TextField(kRect, (string)list[index].Key);
                EditorGUI.TextField(vRect, (string)list[index].Value);
            };
            return reorderableList;
        }

        private class EnvironmentVariable
        {
            public object Key;
            public object Value;
        }
    }
}

활용단어참조


메뉴에서 도구/환경 변수 뷰어 선택

창이 열리고 환경 변수의 목록이 표시됩니다.

좋은 웹페이지 즐겨찾기