using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; using System; using System.Text; #if UNITY_EDITOR using UnityEditor; #endif public class GenerateUICode : MonoBehaviour { } #if UNITY_EDITOR [CustomEditor(typeof(GenerateUICode))] public class GenerateUICodeEditor : Editor { private Dictionary m_IdToCopy = new Dictionary(); private Dictionary m_IdToPath = new Dictionary(); private void OnEnable() { m_IdToCopy.Clear(); m_IdToPath.Clear(); GenerateUICode _target = target as GenerateUICode; Transform _child; for (int i = 0; i < _target.transform.childCount; ++i) { _child = _target.transform.GetChild(i); m_IdToCopy.Add(_child.GetInstanceID(), false); m_IdToPath.Add(_child.GetInstanceID(), GetPath(_child)); if (_child.childCount > 0) { GenerateToggle(_child); } } } private string GetPath(Transform tr) { GenerateUICode _target = target as GenerateUICode; string path = tr.name; while (tr.parent != null && tr.parent.GetInstanceID() != _target.transform.GetInstanceID()) { path = tr.parent.name + "/" + path; tr = tr.parent; } return path; } public override void OnInspectorGUI() { EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("复制定义")) { CopyDefine(); } if (GUILayout.Button("复制获取")) { CopyGet(); } EditorGUILayout.EndHorizontal(); GenerateUICode _target = target as GenerateUICode; if (_target.transform.childCount > 0) { Transform _child; for (int i = 0; i < _target.transform.childCount; ++i) { _child = _target.transform.GetChild(i); EditorGUILayout.BeginHorizontal(); m_IdToCopy[_child.GetInstanceID()] = EditorGUILayout.ToggleLeft(_child.name, m_IdToCopy[_child.GetInstanceID()]); EditorGUILayout.EndHorizontal(); if (_child.childCount > 0) { DrawChildren(_child, 1); } } } } private void GenerateToggle(Transform parent) { Transform _child; for (int i = 0; i < parent.transform.childCount; ++i) { _child = parent.transform.GetChild(i); m_IdToCopy.Add(_child.GetInstanceID(), false); m_IdToPath.Add(_child.GetInstanceID(), GetPath(_child)); if (_child.childCount > 0) { GenerateToggle(_child); } } } private void DrawChildren(Transform parent, int lineSpace) { Transform _child; EditorGUI.indentLevel += lineSpace; for (int i = 0; i < parent.transform.childCount; ++i) { _child = parent.transform.GetChild(i); EditorGUILayout.BeginHorizontal(); m_IdToCopy[_child.GetInstanceID()] = EditorGUILayout.ToggleLeft(_child.name, m_IdToCopy[_child.GetInstanceID()]); EditorGUILayout.EndHorizontal(); if (_child.childCount > 0) { DrawChildren(_child, lineSpace); } } EditorGUI.indentLevel -= lineSpace; } private void CopyDefine() { StringBuilder _content = new StringBuilder(); GenerateUICode _target = target as GenerateUICode; Transform _child; for (int i = 0; i < _target.transform.childCount; ++i) { _child = _target.transform.GetChild(i); if (m_IdToCopy[_child.GetInstanceID()]) { Component[] _components = _child.GetComponents(); foreach (var _component in _components) { _content.Append("private "); _content.Append(typeof(Image).Name); _content.Append(" m_Img"); _content.Append(_child.name); _content.Append(";\r\n"); } _components = _child.GetComponents(); foreach (var _component in _components) { _content.Append("private "); _content.Append(typeof(Text).Name); _content.Append(" m_Txt"); _content.Append(_child.name); _content.Append(";\r\n"); } _components = _child.GetComponents