using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEditor;
|
using System.IO;
|
using UnityEngine.UI;
|
public static class ButtonAreaTool
|
{
|
static public void OnGUI()
|
{
|
ToolsHelper.DisplayFolderPath();
|
|
if (GUILayout.Button("查找"))
|
{
|
m_ButtonDict.Clear();
|
m_ButtonPaths.Clear();
|
m_SpreadDict.Clear();
|
StartFindButton();
|
}
|
|
GUILayout.BeginHorizontal();
|
GUILayout.Label(m_ExportPath);
|
if (GUILayout.Button("导出路径"))
|
{
|
m_ExportPath = EditorUtility.OpenFolderPanel("导出路径", "", "");
|
}
|
if (m_ButtonDict.Count > 0 && GUILayout.Button("导出"))
|
{
|
Export();
|
}
|
GUILayout.FlexibleSpace();
|
GUILayout.EndHorizontal();
|
|
m_ScrollPosition = GUILayout.BeginScrollView(m_ScrollPosition);
|
var _index = 0;
|
foreach (var _prefab in m_ButtonDict.Keys)
|
{
|
GUILayout.BeginHorizontal();
|
EditorGUILayout.ObjectField(_prefab, typeof(GameObject), false);
|
if(GUILayout.Button(m_SpreadDict[_prefab]? "收起" : "展开"))
|
{
|
m_SpreadDict[_prefab] = !m_SpreadDict[_prefab];
|
}
|
GUILayout.FlexibleSpace();
|
GUILayout.EndHorizontal();
|
if (!m_SpreadDict[_prefab])
|
{
|
continue;
|
}
|
var _list = m_ButtonDict[_prefab];
|
for (int i = 0; i < _list.Count; i++)
|
{
|
GUILayout.BeginHorizontal();
|
if (GUILayout.Button("Goto"))
|
{
|
var _go = GameObject.Find(m_ButtonPaths[_index]);
|
if (_go != null)
|
{
|
Selection.activeGameObject = _go;
|
}
|
}
|
EditorGUILayout.Vector2Field("按钮区域",_list[i].buttonArea);
|
if (!_list[i].Equals(default(Vector2)))
|
{
|
EditorGUILayout.Vector2Field("检测区域", _list[i].rayArea);
|
}
|
GUILayout.FlexibleSpace();
|
GUILayout.EndHorizontal();
|
_index++;
|
}
|
GUILayout.Space(10);
|
}
|
GUILayout.EndScrollView();
|
}
|
|
static Dictionary<GameObject, List<ButtonRayArea>> m_ButtonDict = new Dictionary<GameObject, List<ButtonRayArea>>();
|
static Dictionary<GameObject, bool> m_SpreadDict = new Dictionary<GameObject, bool>();
|
static List<string> m_ButtonPaths = new List<string>();
|
static Vector2 m_ScrollPosition = Vector2.zero;
|
|
static string m_ExportPath = string.Empty;
|
|
static void StartFindButton()
|
{
|
var _files = Directory.GetFiles(ToolsHelper.folderPath, "*.prefab", SearchOption.AllDirectories);
|
if (_files == null || _files.Length == 0)
|
{
|
return;
|
}
|
var _index = 0;
|
EditorApplication.update = delegate ()
|
{
|
var _file = _files[_index];
|
GameObject _prefab = AssetDatabase.LoadAssetAtPath(ToolsHelper.GetRelativeAssetsPath(_file), typeof(GameObject)) as GameObject;
|
if (_prefab != null)
|
{
|
var _buttons = _prefab.GetComponentsInChildren<Button>(true);
|
if (_buttons != null && _buttons.Length > 0)
|
{
|
List<ButtonRayArea> _list = null;
|
if (!m_ButtonDict.TryGetValue(_prefab, out _list))
|
{
|
_list = new List<ButtonRayArea>();
|
m_ButtonDict.Add(_prefab, _list);
|
m_SpreadDict.Add(_prefab, false);
|
}
|
for (int i = 0, imax = _buttons.Length; i < imax; i++)
|
{
|
m_ButtonPaths.Add(GetParentName(_buttons[i].transform));
|
var _buttonSize = (_buttons[i].transform as RectTransform).sizeDelta;
|
var _ray = _buttons[i].transform.GetComponentInChildren<RayAccepter>(true);
|
var _raySize = default(Vector2);
|
if (_ray != null)
|
{
|
_raySize = (_ray.transform as RectTransform).sizeDelta;
|
}
|
_list.Add(new ButtonRayArea()
|
{
|
buttonArea = _buttonSize,
|
rayArea = _raySize,
|
hasRay = _ray != null,
|
});
|
}
|
}
|
}
|
bool _isCancel = EditorUtility.DisplayCancelableProgressBar("查找按钮监听区域",
|
StringUtility.Contact(_index, "/", _files.Length), (float)_index / _files.Length);
|
_index++;
|
if (_isCancel || _index >= _files.Length)
|
{
|
EditorUtility.ClearProgressBar();
|
EditorApplication.update = null;
|
_index = 0;
|
}
|
};
|
}
|
|
static string GetParentName(Transform _parent)
|
{
|
if (_parent.parent != null)
|
{
|
return StringUtility.Contact(GetParentName(_parent.parent), "/", _parent.name);
|
}
|
return _parent.name;
|
}
|
|
static void Export()
|
{
|
using (FileStream fs = new FileStream(StringUtility.Contact(m_ExportPath, "/logger.txt"), FileMode.Create, FileAccess.Write))
|
{
|
using (StreamWriter sw = new StreamWriter(fs))
|
{
|
var _index = 0;
|
foreach (var _prefab in m_ButtonDict.Keys)
|
{
|
var _list = m_ButtonDict[_prefab];
|
for (int i = 0; i < _list.Count; i++)
|
{
|
sw.WriteLine(StringUtility.Contact("预制体名称:", "\t", _prefab.name, "\t",
|
"按钮路径:", "\t", m_ButtonPaths[_index], "\t",
|
"按钮区域:", "\t", _list[i].buttonArea.ToString(), "\t",
|
_list[i].hasRay ? StringUtility.Contact("检测区域:\t", _list[i].rayArea.ToString()) : string.Empty));
|
_index++;
|
}
|
}
|
}
|
}
|
}
|
|
struct ButtonRayArea
|
{
|
public Vector2 buttonArea;
|
public Vector2 rayArea;
|
public bool hasRay;
|
}
|
}
|