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<int, bool> m_IdToCopy = new Dictionary<int, bool>();
|
private Dictionary<int, string> m_IdToPath = new Dictionary<int, string>();
|
|
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<Image>();
|
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<Text>();
|
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<Button>();
|
foreach (var _component in _components) {
|
_content.Append("private ");
|
_content.Append(typeof(Button).Name);
|
_content.Append(" m_Btn");
|
_content.Append(_child.name);
|
_content.Append(";\r\n");
|
}
|
}
|
|
if (_child.childCount > 0) {
|
CopyDefineChildren(_child, _content);
|
}
|
}
|
|
TextEditor t = new TextEditor();
|
t.text = _content.ToString();
|
t.OnFocus();
|
t.Copy();
|
_content = null;
|
}
|
|
private void CopyGet() {
|
// UGUITool.GetChildControl<Button>(gameObject, "Img_Background/Btn_Cancel");
|
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<Image>();
|
foreach (var _component in _components) {
|
_content.Append("m_Img");
|
_content.Append(_child.name);
|
_content.Append(" = UGUITool.GetChildControl<");
|
_content.Append(typeof(Image).Name);
|
_content.Append(">(gameObject, \"");
|
_content.Append(m_IdToPath[_child.GetInstanceID()]);
|
_content.Append("\");\r\n");
|
}
|
|
_components = _child.GetComponents<Text>();
|
foreach (var _component in _components) {
|
_content.Append("m_Img");
|
_content.Append(_child.name);
|
_content.Append(" = UGUITool.GetChildControl<");
|
_content.Append(typeof(Text).Name);
|
_content.Append(">(gameObject, \"");
|
_content.Append(m_IdToPath[_child.GetInstanceID()]);
|
_content.Append("\");\r\n");
|
}
|
|
_components = _child.GetComponents<Button>();
|
foreach (var _component in _components) {
|
_content.Append("m_Img");
|
_content.Append(_child.name);
|
_content.Append(" = UGUITool.GetChildControl<");
|
_content.Append(typeof(Button).Name);
|
_content.Append(">(gameObject, \"");
|
_content.Append(m_IdToPath[_child.GetInstanceID()]);
|
_content.Append("\");\r\n");
|
}
|
}
|
|
if (_child.childCount > 0) {
|
CopyGetChildren(_child, _content);
|
}
|
}
|
|
TextEditor t = new TextEditor();
|
t.text = _content.ToString();
|
t.OnFocus();
|
t.Copy();
|
_content = null;
|
}
|
|
private void CopyDefineChildren(Transform parent, StringBuilder content) {
|
Transform _child;
|
for (int i = 0; i < parent.transform.childCount; ++i) {
|
_child = parent.transform.GetChild(i);
|
if (m_IdToCopy[_child.GetInstanceID()]) {
|
Component[] _components = _child.GetComponents<Image>();
|
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<Text>();
|
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<Button>();
|
foreach (var _component in _components) {
|
content.Append("private ");
|
content.Append(typeof(Button).Name);
|
content.Append(" m_Btn");
|
content.Append(_child.name);
|
content.Append(";\r\n");
|
}
|
}
|
|
if (_child.childCount > 0) {
|
CopyDefineChildren(_child, content);
|
}
|
}
|
}
|
|
private void CopyGetChildren(Transform parent, StringBuilder content) {
|
Transform _child;
|
for (int i = 0; i < parent.transform.childCount; ++i) {
|
_child = parent.transform.GetChild(i);
|
if (m_IdToCopy[_child.GetInstanceID()]) {
|
Component[] _components = _child.GetComponents<Image>();
|
foreach (var _component in _components) {
|
content.Append("m_Img");
|
content.Append(_child.name);
|
content.Append(" = UGUITool.GetChildControl<");
|
content.Append(typeof(Image).Name);
|
content.Append(">(gameObject, \"");
|
content.Append(m_IdToPath[_child.GetInstanceID()]);
|
content.Append("\");\r\n");
|
}
|
|
_components = _child.GetComponents<Text>();
|
foreach (var _component in _components) {
|
content.Append("m_Img");
|
content.Append(_child.name);
|
content.Append(" = UGUITool.GetChildControl<");
|
content.Append(typeof(Text).Name);
|
content.Append(">(gameObject, \"");
|
content.Append(m_IdToPath[_child.GetInstanceID()]);
|
content.Append("\");\r\n");
|
}
|
|
_components = _child.GetComponents<Button>();
|
foreach (var _component in _components) {
|
content.Append("m_Img");
|
content.Append(_child.name);
|
content.Append(" = UGUITool.GetChildControl<");
|
content.Append(typeof(Button).Name);
|
content.Append(">(gameObject, \"");
|
content.Append(m_IdToPath[_child.GetInstanceID()]);
|
content.Append("\");\r\n");
|
}
|
}
|
|
if (_child.childCount > 0) {
|
CopyGetChildren(_child, content);
|
}
|
}
|
}
|
}
|
#endif
|