Upgrade to Pro — share decks privately, control downloads, hide ads and more …

俺はEditor拡張で行く ~ゼロから便利機能を作るまで

vailKAKO
October 09, 2020

俺はEditor拡張で行く ~ゼロから便利機能を作るまで

ゼロからEditor拡張をやるための足がかりになるかなと思いLTしました。

vailKAKO

October 09, 2020
Tweet

More Decks by vailKAKO

Other Decks in Programming

Transcript

  1. ᶄͱΓ͋͑ͣWindowΛग़͢ using UnityEditor; public class StartEditor : EditorWindow { //։͔Εํͱɺ։͔Εͨͱ͖ͷڍಈ

    [MenuItem("StartEditor/OpenDAZE")] private static void Open() { GetWindow<StartEditor>("Start editor DAZE"); } }
  2. ᶄϘλϯͰͳʹ͔͢Δ2 private void OnGUI() { if (GUILayout.Button("Nanikoe?")) { var objs

    = FindObjectsOfType<Transform>(); Debug.Log($"{objs.Length}"); foreach (var obj in objs) { Debug.Log(obj.name); } } }
  3. ᶄೖྗ૭Λ࡞Δ private string _targetComponentName; private void OnGUI() { //Editorʹ࿮Λग़ͯ͠ɺೖྗΛ_targetComponentNameʹ֨ೲ͢Δ EditorGUILayout.BeginHorizontal();

    _targetComponentName = EditorGUILayout.TextField("Target Component Name: ", _targetComponentName); EditorGUILayout.EndHorizontal(); . . .
  4. ᶄComponentࣗମΛऔಘ private static IEnumerable<Type> GetTypes() { //Unityඪ४ͷΫϥελΠϓΛऔಘ͢Δ var types =

    AppDomain.CurrentDomain.GetAssemblies() .SelectMany(asm => asm.GetTypes()) .Where(type => type != null && ! string.IsNullOrEmpty(type.Namespace)) .Where(type => type.Namespace.Contains("UnityEngine")); return types; }
  5. ᶅComponentΛؚΉ΋ͷΛग़͢ private static Dictionary<string, List<Type>> _typeDict; private static Type GetType(string

    className) { if (_typeDict == null) { // Dictionary࡞੒ _typeDict = new Dictionary<string, List<Type>>(); var components = GetTypes(); foreach (var type in components) { if (!_typeDict.ContainsKey(type.Name)) { _typeDict.Add(type.Name, new List<Type>()); } _typeDict[type.Name].Add(type); } } return _typeDict[className][0]; }
  6. ͏͓͓͓͓͓͓͓͓͓͓͓͓͓͓͓ using System; using System.Collections.Generic; using System.Linq; using UnityEditor; using

    UnityEngine; using UnityEngine.SceneManagement; public class ReferenceFinder : EditorWindow { private string _targetComponentName; private List<GameObject> _foundAssets = new List<GameObject>(); private Vector2 _currentScrollPosition; private static IEnumerable<Type> _cashedComponents; private static Dictionary<string, List<Type>> _typeDict; static MonoScript[] _monoScripts; private static bool _isFirstTime = true; //։͔Εํͱɺ։͔Εͨͱ͖ͷڍಈ [MenuItem("ReferenceFinder/Search")] private static void Open() { //։͔ΕΔࡍʹ͸ҰԠऔಘ _cashedComponents = GetAllTypes(); GetWindow<ReferenceFinder>("Components Reference Finder."); } private void OnGUI() { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Never change Scene before reset this window"); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("This window can search active scene objects and all prefabs"); EditorGUILayout.EndHorizontal(); //Editorʹ࿮Λग़ͯ͠ɺೖྗΛ_targetComponentNameʹ֨ೲ͢Δ EditorGUILayout.BeginHorizontal(); _targetComponentName = EditorGUILayout.TextField("Target Component Name: ", _targetComponentName); EditorGUILayout.EndHorizontal(); if (_foundAssets.Count > 0) { _currentScrollPosition = EditorGUILayout.BeginScrollView(_currentScrollPosition); foreach (var asset in _foundAssets) { EditorGUILayout.ObjectField(asset.name, asset, typeof(GameObject), false); } EditorGUILayout.EndScrollView(); } if (GUILayout.Button("Search")) { _foundAssets.Clear(); var guids = AssetDatabase.FindAssets("t:GameObject", null); foreach (var guid in guids) { string path = AssetDatabase.GUIDToAssetPath(guid); var loadAsset = AssetDatabase.LoadAssetAtPath<GameObject>(path); var typeCash = GetType(_targetComponentName) ?? null; if (typeCash == null) { Debug.Log("No Type Found."); return; } var tmp = loadAsset.GetComponentsInChildren(GetType(_targetComponentName) ?? null); foreach (var kari in tmp) { _foundAssets.Add(kari.gameObject); } } var allObj = GetObjectsInAllScene(); foreach (var obj in allObj) { var typeCash = GetType(_targetComponentName) ?? null; var tmp = obj.GetComponentsInChildren(GetType(_targetComponentName) ?? null); if (typeCash == null) { Debug.Log("No Type Found."); return; } foreach (var kari in tmp) { _foundAssets.Add(kari.gameObject);