【m】1111
This commit is contained in:
252
Assets/06.UIFarme/Editor/UIControlBinding/ControlItemDrawer.cs
Normal file
252
Assets/06.UIFarme/Editor/UIControlBinding/ControlItemDrawer.cs
Normal file
@@ -0,0 +1,252 @@
|
||||
#if UNITY_EDITOR
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Linq;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
public class ControlItemDrawer
|
||||
{
|
||||
private UIControlDataEditor _container;
|
||||
private CtrlItemData _itemData;
|
||||
private bool _foldout = true;
|
||||
private int _controlTypeIdx = 0;
|
||||
|
||||
public ControlItemDrawer(UIControlDataEditor container, CtrlItemData item)
|
||||
{
|
||||
_container = container;
|
||||
_itemData = item;
|
||||
}
|
||||
|
||||
public bool Draw()
|
||||
{
|
||||
EditorGUI.BeginChangeCheck();
|
||||
Rect rect = EditorGUILayout.BeginVertical();
|
||||
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
{
|
||||
EditorGUILayout.LabelField("变量名 ", UIControlDataEditor.skin.label, GUILayout.Width(60f));
|
||||
string newName = EditorGUILayout.TextField(_itemData.name, UIControlDataEditor.skin.textField).Trim();
|
||||
|
||||
if (newName != _itemData.name)
|
||||
{
|
||||
_itemData.name = newName;
|
||||
(_container.target as UIControlData).SetDirty();
|
||||
}
|
||||
|
||||
EditorGUILayout.Space();
|
||||
_foldout = EditorGUILayout.Foldout(_foldout, _foldout ? "收起" : "展开", true);
|
||||
|
||||
if (GUILayout.Button("+", EditorStyles.miniButton))
|
||||
{
|
||||
_container.AddControlAfter(this);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (GUILayout.Button("-", EditorStyles.miniButton))
|
||||
{
|
||||
_container.RemoveControl(this);
|
||||
return false;
|
||||
}
|
||||
GUILayout.FlexibleSpace();
|
||||
}
|
||||
EditorGUILayout.EndHorizontal();
|
||||
|
||||
// 控件列表
|
||||
if (_foldout)
|
||||
{
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
{
|
||||
EditorGUILayout.LabelField("变量类型 ", UIControlDataEditor.skin.label, GUILayout.Width(60f));
|
||||
|
||||
if (_controlTypeIdx == 0 && !string.IsNullOrEmpty(_itemData.type))
|
||||
_controlTypeIdx = FindTypeIdx(_itemData.type);
|
||||
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
(_container.target as UIControlData).SetDirty();
|
||||
}
|
||||
|
||||
EditorGUI.BeginChangeCheck();
|
||||
_controlTypeIdx = EditorGUILayout.Popup(_controlTypeIdx, _container.allTypeNames, UIControlDataEditor.popupAlignLeft);
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
if(_controlTypeIdx != 0)
|
||||
{
|
||||
if (!ChangeControlsTypeTo(_controlTypeIdx))
|
||||
_controlTypeIdx = 0; // 切换失败,重置回自动
|
||||
}
|
||||
else // 被主动设置为了自动
|
||||
_itemData.type = string.Empty;
|
||||
|
||||
(_container.target as UIControlData).SetDirty();
|
||||
return false;
|
||||
}
|
||||
|
||||
EditorGUI.BeginChangeCheck();
|
||||
GUILayout.FlexibleSpace();
|
||||
}
|
||||
EditorGUILayout.EndHorizontal();
|
||||
|
||||
|
||||
EditorGUILayout.Space();
|
||||
for (int i = 0, imax = _itemData.targets.Length; i < imax; i++)
|
||||
{
|
||||
Object obj = _itemData.targets[i];
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
|
||||
EditorGUI.BeginChangeCheck();
|
||||
_itemData.targets[i] = EditorGUILayout.ObjectField(obj, typeof(Object), true);
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
(_container.target as UIControlData).SetDirty();
|
||||
}
|
||||
|
||||
EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space();
|
||||
if (GUILayout.Button("+", EditorStyles.miniButton))
|
||||
{
|
||||
InsertItem(i + 1);
|
||||
return false;
|
||||
}
|
||||
if (GUILayout.Button("-", EditorStyles.miniButton))
|
||||
{
|
||||
if(_itemData.targets.Length == 1)
|
||||
{
|
||||
Debug.LogError("至少应保留一个控件");
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
RemoveItem(i);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
EditorGUILayout.EndHorizontal();
|
||||
EditorGUILayout.Space();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
EditorGUILayout.EndVertical();
|
||||
|
||||
if (EditorGUIUtility.isProSkin)
|
||||
GUI.Box(new Rect(rect.x - 10f, rect.y - 5f, rect.width + 20f, rect.height + 15f), "");
|
||||
else
|
||||
GUI.Box(new Rect(rect.x - 10f, rect.y - 5f, rect.width + 20f, rect.height + 15f), "", UIControlDataEditor.skin.box);
|
||||
|
||||
if (EditorGUI.EndChangeCheck())
|
||||
{
|
||||
(_container.target as UIControlData).SetDirty();
|
||||
}
|
||||
|
||||
PostProcess();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void PostProcess()
|
||||
{
|
||||
// 默认将新添加的第一个控件的名字作为变量名
|
||||
if (_itemData.targets.Length > 0 && _itemData.targets[0] != null && string.IsNullOrEmpty(_itemData.name))
|
||||
_itemData.name = _itemData.targets[0].name.Trim();
|
||||
}
|
||||
|
||||
private int FindTypeIdx(string typeName)
|
||||
{
|
||||
string[] allTypeNames = _container.allTypeNames;
|
||||
for (int i = 0, imax = allTypeNames.Length; i < imax; i++)
|
||||
{
|
||||
if(allTypeNames[i] == typeName)
|
||||
return i;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void InsertItem(int idx)
|
||||
{
|
||||
Object[] newArr = new Object[_itemData.targets.Length + 1];
|
||||
for(int i = 0; i < idx; i++)
|
||||
{
|
||||
newArr[i] = _itemData.targets[i];
|
||||
}
|
||||
newArr[idx] = null;
|
||||
for(int i = idx + 1; i < newArr.Length; i++)
|
||||
{
|
||||
newArr[i] = _itemData.targets[i - 1];
|
||||
}
|
||||
|
||||
_itemData.targets = newArr;
|
||||
}
|
||||
|
||||
private void RemoveItem(int idx)
|
||||
{
|
||||
Object[] newArr = new Object[_itemData.targets.Length - 1];
|
||||
for(int i = 0; i < idx; i++)
|
||||
{
|
||||
newArr[i] = _itemData.targets[i];
|
||||
}
|
||||
|
||||
for(int i = idx; i < newArr.Length; i++)
|
||||
{
|
||||
newArr[idx] = _itemData.targets[i + 1];
|
||||
}
|
||||
|
||||
_itemData.targets = newArr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将控件切换到指定类型
|
||||
/// </summary>
|
||||
/// <param name="typeIdx"></param>
|
||||
private bool ChangeControlsTypeTo(int typeIdx)
|
||||
{
|
||||
System.Type targetType = _container.allTypes[typeIdx];
|
||||
string targetTypeName = _container.allTypeNames[typeIdx];
|
||||
bool isGameObject = targetType == typeof(GameObject);
|
||||
|
||||
|
||||
for(int i = 0, imax = _itemData.targets.Length; i < imax; i++)
|
||||
{
|
||||
Object obj = _itemData.targets[i];
|
||||
if (obj == null)
|
||||
{
|
||||
Debug.LogErrorFormat("[{0}.{1}] control[{2}] is null"
|
||||
, _container.target.name, _itemData.name, i);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(obj.GetType() != typeof(GameObject))
|
||||
{
|
||||
if((obj as Component) == null)
|
||||
{
|
||||
Debug.LogErrorFormat("[{0}.{1}] control[{2}] [{3}] must be GameObject or a Component"
|
||||
, _container.target.name, _itemData.name, i, obj.name);
|
||||
return false;
|
||||
}
|
||||
obj = (obj as Component).gameObject;
|
||||
}
|
||||
|
||||
GameObject go = obj as GameObject;
|
||||
if (isGameObject)
|
||||
_itemData.targets[i] = go;
|
||||
else
|
||||
{
|
||||
Component comp = go.GetComponent(targetType);
|
||||
if(comp == null)
|
||||
{
|
||||
Debug.LogErrorFormat("[{0}.{1}] control[{2}] [{3}] isn't a {4}"
|
||||
, _container.target.name, _itemData.name, i, go.name, targetTypeName);
|
||||
return false;
|
||||
}
|
||||
_itemData.targets[i] = comp;
|
||||
}
|
||||
}
|
||||
|
||||
_itemData.type = targetTypeName;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5a19fd525bc43e2439c14dac7ed67295
|
||||
timeCreated: 1521794417
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
73
Assets/06.UIFarme/Editor/UIControlBinding/SubUIItemDrawer.cs
Normal file
73
Assets/06.UIFarme/Editor/UIControlBinding/SubUIItemDrawer.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
#if UNITY_EDITOR
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
public class SubUIItemDrawer
|
||||
{
|
||||
private UIControlDataEditor _container;
|
||||
private SubUIItemData _itemData;
|
||||
private bool _foldout = true;
|
||||
|
||||
public SubUIItemDrawer(UIControlDataEditor container, SubUIItemData itemData)
|
||||
{
|
||||
_container = container;
|
||||
_itemData = itemData;
|
||||
}
|
||||
|
||||
public bool Draw()
|
||||
{
|
||||
Rect rect = EditorGUILayout.BeginVertical();
|
||||
{
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
{
|
||||
EditorGUILayout.LabelField("子UI名 ", UIControlDataEditor.skin.label);
|
||||
_itemData.name = EditorGUILayout.TextField(_itemData.name, UIControlDataEditor.skin.textField).Trim();
|
||||
EditorGUILayout.Space();
|
||||
_foldout = EditorGUILayout.Foldout(_foldout, _foldout ? "收起" : "展开", true);
|
||||
|
||||
if (GUILayout.Button("+", EditorStyles.miniButton))
|
||||
{
|
||||
_container.AddSubUIAfter(this);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (GUILayout.Button("-", EditorStyles.miniButton))
|
||||
{
|
||||
_container.RemoveSubUI(this);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
EditorGUILayout.EndHorizontal();
|
||||
|
||||
|
||||
if (_foldout)
|
||||
{
|
||||
EditorGUILayout.Space();
|
||||
_itemData.subUIData = EditorGUILayout.ObjectField(_itemData.subUIData as Object, typeof(UIControlData), true) as UIControlData;
|
||||
}
|
||||
}
|
||||
EditorGUILayout.EndVertical();
|
||||
|
||||
if (EditorGUIUtility.isProSkin)
|
||||
GUI.Box(new Rect(rect.x - 10f, rect.y - 5f, rect.width + 20f, rect.height + 15f), "");
|
||||
else
|
||||
GUI.Box(new Rect(rect.x - 10f, rect.y - 5f, rect.width + 20f, rect.height + 15f), "", UIControlDataEditor.skin.box);
|
||||
|
||||
PostProcess();
|
||||
return true;
|
||||
}
|
||||
|
||||
private void PostProcess()
|
||||
{
|
||||
// 默认将控件的名字作为变量名
|
||||
if (_itemData.subUIData != null && string.IsNullOrEmpty(_itemData.name))
|
||||
_itemData.name = _itemData.subUIData.name.Trim();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2db1a302384342148abb76d0508f2944
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
234
Assets/06.UIFarme/Editor/UIControlBinding/UIControlDataEditor.cs
Normal file
234
Assets/06.UIFarme/Editor/UIControlBinding/UIControlDataEditor.cs
Normal file
@@ -0,0 +1,234 @@
|
||||
#if UNITY_EDITOR
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
[CustomEditor(typeof(UIControlData))]
|
||||
public class UIControlDataEditor : UnityEditor.Editor
|
||||
{
|
||||
public static GUISkin skin;
|
||||
public static GUIStyle popupAlignLeft; // TODO 挪出去一个 SkinManager
|
||||
|
||||
public string[] allTypeNames;
|
||||
public Type[] allTypes;
|
||||
|
||||
private List<CtrlItemData> _ctrlItemDatas;
|
||||
private List<SubUIItemData> _subUIItemDatas;
|
||||
|
||||
private List<ControlItemDrawer> _ctrlItemDrawers;
|
||||
private List<SubUIItemDrawer> _subUIItemDrawers;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if(skin == null)
|
||||
{
|
||||
if (EditorGUIUtility.isProSkin)
|
||||
skin = Resources.Load("Editor/UIControlDataSkinPro") as GUISkin;
|
||||
else
|
||||
skin = Resources.Load("Editor/UIControlDataSkinPersonal") as GUISkin;
|
||||
}
|
||||
|
||||
if(popupAlignLeft == null)
|
||||
{
|
||||
popupAlignLeft = new GUIStyle("Popup");
|
||||
popupAlignLeft.alignment = TextAnchor.MiddleLeft;
|
||||
}
|
||||
|
||||
allTypeNames = UIControlData.GetAllTypeNames();
|
||||
allTypes = UIControlData.GetAllTypes();
|
||||
|
||||
UIControlData uIControlData = target as UIControlData;
|
||||
if (uIControlData != null)
|
||||
{
|
||||
uIControlData.CorrectComponents();
|
||||
uIControlData.CheckSubUIs();
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
if (skin == null || skin.customStyles == null || skin.customStyles.Length == 0)
|
||||
{
|
||||
base.OnInspectorGUI();
|
||||
return;
|
||||
}
|
||||
|
||||
UIControlData data = target as UIControlData;
|
||||
if(data.ctrlItemDatas == null)
|
||||
data.ctrlItemDatas = new List<CtrlItemData>();
|
||||
|
||||
if(data.subUIItemDatas == null)
|
||||
data.subUIItemDatas = new List<SubUIItemData>();
|
||||
|
||||
_ctrlItemDatas = data.ctrlItemDatas;
|
||||
_subUIItemDatas = data.subUIItemDatas;
|
||||
CheckDrawers();
|
||||
|
||||
EditorGUILayout.BeginVertical();
|
||||
EditorGUILayout.Space();
|
||||
|
||||
|
||||
// 绘制控件绑定
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
EditorGUILayout.LabelField("控件绑定", skin.customStyles[0]);
|
||||
if(_ctrlItemDrawers.Count == 0)
|
||||
{
|
||||
if(GUILayout.Button("+", EditorStyles.miniButton))
|
||||
{
|
||||
AddControlAfter(-1);
|
||||
Repaint();
|
||||
return;
|
||||
}
|
||||
}
|
||||
EditorGUILayout.EndHorizontal();
|
||||
foreach (var drawer in _ctrlItemDrawers)
|
||||
{
|
||||
GUILayout.Space(10f);
|
||||
if (!drawer.Draw())
|
||||
{
|
||||
Repaint();
|
||||
return;
|
||||
}
|
||||
GUILayout.Space(10f);
|
||||
}
|
||||
|
||||
GUILayout.Space(10f);
|
||||
|
||||
// 绘制子UI
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
EditorGUILayout.LabelField("子UI绑定", skin.customStyles[0]);
|
||||
if(_subUIItemDrawers.Count == 0)
|
||||
{
|
||||
if (GUILayout.Button("+", EditorStyles.miniButton))
|
||||
{
|
||||
AddSubUIAfter(-1);
|
||||
Repaint();
|
||||
return;
|
||||
}
|
||||
}
|
||||
EditorGUILayout.EndHorizontal();
|
||||
foreach(var drawer in _subUIItemDrawers)
|
||||
{
|
||||
GUILayout.Space(10f);
|
||||
if (!drawer.Draw())
|
||||
{
|
||||
Repaint();
|
||||
return;
|
||||
}
|
||||
GUILayout.Space(10f);
|
||||
}
|
||||
|
||||
EditorGUILayout.Space(); EditorGUILayout.Space();
|
||||
EditorGUILayout.EndVertical();
|
||||
|
||||
this.Repaint();
|
||||
}
|
||||
|
||||
public void AddControlAfter(ControlItemDrawer drawer)
|
||||
{
|
||||
int idx = _ctrlItemDrawers.IndexOf(drawer);
|
||||
Debug.Assert(idx != -1);
|
||||
|
||||
AddControlAfter(idx);
|
||||
}
|
||||
|
||||
public void AddSubUIAfter(SubUIItemDrawer drawer)
|
||||
{
|
||||
int idx = _subUIItemDrawers.IndexOf(drawer);
|
||||
Debug.Assert(idx != -1);
|
||||
|
||||
AddSubUIAfter(idx);
|
||||
}
|
||||
|
||||
public void RemoveControl(ControlItemDrawer drawer)
|
||||
{
|
||||
int idx = _ctrlItemDrawers.IndexOf(drawer);
|
||||
Debug.Assert(idx != -1);
|
||||
|
||||
RemoveControl(idx);
|
||||
}
|
||||
|
||||
public void RemoveSubUI(SubUIItemDrawer drawer)
|
||||
{
|
||||
int idx = _subUIItemDrawers.IndexOf(drawer);
|
||||
Debug.Assert(idx != -1);
|
||||
|
||||
RemoveSubUI(idx);
|
||||
}
|
||||
|
||||
#region Private
|
||||
private void CheckDrawers()
|
||||
{
|
||||
if (_ctrlItemDrawers == null)
|
||||
{
|
||||
_ctrlItemDrawers = new List<ControlItemDrawer>(100);
|
||||
foreach(var item in _ctrlItemDatas)
|
||||
{
|
||||
ControlItemDrawer drawer = new ControlItemDrawer(this, item);
|
||||
_ctrlItemDrawers.Add(drawer);
|
||||
}
|
||||
}
|
||||
|
||||
if(_subUIItemDrawers == null)
|
||||
{
|
||||
_subUIItemDrawers = new List<SubUIItemDrawer>(100);
|
||||
foreach(var item in _subUIItemDatas)
|
||||
{
|
||||
SubUIItemDrawer drawer = new SubUIItemDrawer(this, item);
|
||||
_subUIItemDrawers.Add(drawer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void AddControlAfter(int idx)
|
||||
{
|
||||
CtrlItemData itemData = new CtrlItemData();
|
||||
_ctrlItemDatas.Insert(idx + 1, itemData);
|
||||
|
||||
ControlItemDrawer drawer = new ControlItemDrawer(this, itemData);
|
||||
_ctrlItemDrawers.Insert(idx + 1, drawer);
|
||||
|
||||
SetPrefabDirty();
|
||||
}
|
||||
|
||||
private void AddSubUIAfter(int idx)
|
||||
{
|
||||
SubUIItemData itemData = new SubUIItemData();
|
||||
_subUIItemDatas.Insert(idx + 1, itemData);
|
||||
|
||||
SubUIItemDrawer drawer = new SubUIItemDrawer(this, itemData);
|
||||
_subUIItemDrawers.Insert(idx + 1, drawer);
|
||||
|
||||
SetPrefabDirty();
|
||||
}
|
||||
|
||||
private void RemoveControl(int idx)
|
||||
{
|
||||
_ctrlItemDatas.RemoveAt(idx);
|
||||
_ctrlItemDrawers.RemoveAt(idx);
|
||||
|
||||
SetPrefabDirty();
|
||||
}
|
||||
|
||||
private void RemoveSubUI(int idx)
|
||||
{
|
||||
_subUIItemDatas.RemoveAt(idx);
|
||||
_subUIItemDrawers.RemoveAt(idx);
|
||||
|
||||
SetPrefabDirty();
|
||||
}
|
||||
|
||||
private void SetPrefabDirty()
|
||||
{
|
||||
UIControlData uIControlData = target as UIControlData;
|
||||
uIControlData.SetDirty();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 27c41d595322d554a9afea0d2b516863
|
||||
timeCreated: 1521786373
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user