mirror of
https://github.com/DCFApixels/DragonECS-Unity.git
synced 2025-09-19 02:24:35 +08:00
146 lines
5.0 KiB
C#
146 lines
5.0 KiB
C#
![]() |
#if UNITY_EDITOR
|
|||
|
using DCFApixels.DragonECS.Unity.Internal;
|
|||
|
using UnityEditor;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace DCFApixels.DragonECS.Unity.Editors
|
|||
|
{
|
|||
|
[CustomEditor(typeof(EcsEntityConnect))]
|
|||
|
[CanEditMultipleObjects]
|
|||
|
public class EcsEntityConnectEditor : Editor
|
|||
|
{
|
|||
|
private bool _isInit = false;
|
|||
|
private EcsEntityConnect Target => (EcsEntityConnect)target;
|
|||
|
private bool IsMultipleTargets => targets.Length > 1;
|
|||
|
|
|||
|
private void Init()
|
|||
|
{
|
|||
|
if (_isInit)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
_isInit = true;
|
|||
|
}
|
|||
|
|
|||
|
public override void OnInspectorGUI()
|
|||
|
{
|
|||
|
Init();
|
|||
|
EcsEntityConnect[] targets = new EcsEntityConnect[this.targets.Length];
|
|||
|
for (int i = 0; i < targets.Length; i++)
|
|||
|
{
|
|||
|
targets[i] = (EcsEntityConnect)this.targets[i];
|
|||
|
}
|
|||
|
DrawEntityInfo(targets);
|
|||
|
|
|||
|
DrawTemplates();
|
|||
|
|
|||
|
DrawButtons();
|
|||
|
DrawComponents(targets);
|
|||
|
}
|
|||
|
|
|||
|
private void DrawEntityInfo(EcsEntityConnect[] targets)
|
|||
|
{
|
|||
|
//TODO Отрефакторить
|
|||
|
float width = EditorGUIUtility.currentViewWidth;
|
|||
|
float height = EditorGUIUtility.singleLineHeight;
|
|||
|
Rect entityRect = GUILayoutUtility.GetRect(width, height + 3f);
|
|||
|
var (entityInfoRect, statusRect) = RectUtility.VerticalSliceBottom(entityRect, 3f);
|
|||
|
|
|||
|
Color w = Color.gray;
|
|||
|
w.a = 0.6f;
|
|||
|
Color b = Color.black;
|
|||
|
b.a = 0.55f;
|
|||
|
EditorGUI.DrawRect(entityInfoRect, w);
|
|||
|
|
|||
|
var (idRect, genWorldRect) = RectUtility.HorizontalSliceLerp(entityInfoRect, 0.5f);
|
|||
|
var (genRect, worldRect) = RectUtility.HorizontalSliceLerp(genWorldRect, 0.5f);
|
|||
|
|
|||
|
idRect = RectUtility.AddPadding(idRect, 2, 1, 0, 0);
|
|||
|
genRect = RectUtility.AddPadding(genRect, 1, 1, 0, 0);
|
|||
|
worldRect = RectUtility.AddPadding(worldRect, 1, 2, 0, 0);
|
|||
|
EditorGUI.DrawRect(idRect, b);
|
|||
|
EditorGUI.DrawRect(genRect, b);
|
|||
|
EditorGUI.DrawRect(worldRect, b);
|
|||
|
|
|||
|
bool isConnected = Target.Entity.TryUnpack(out int id, out short gen, out short world);
|
|||
|
|
|||
|
GUIStyle style = new GUIStyle(EditorStyles.numberField);
|
|||
|
style.alignment = TextAnchor.MiddleCenter;
|
|||
|
style.font = EditorStyles.boldFont;
|
|||
|
if (IsMultipleTargets == false && isConnected)
|
|||
|
{
|
|||
|
Color statusColor = EcsGUI.GreenColor;
|
|||
|
statusColor.a = 0.6f;
|
|||
|
EditorGUI.DrawRect(statusRect, statusColor);
|
|||
|
|
|||
|
EditorGUI.IntField(idRect, id, style);
|
|||
|
EditorGUI.IntField(genRect, gen, style);
|
|||
|
EditorGUI.IntField(worldRect, world, style);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Color statusColor = IsMultipleTargets ? new Color32(200, 200, 200, 255) : EcsGUI.RedColor;
|
|||
|
statusColor.a = 0.6f;
|
|||
|
EditorGUI.DrawRect(statusRect, statusColor);
|
|||
|
|
|||
|
using (new EditorGUI.DisabledScope(true))
|
|||
|
{
|
|||
|
GUI.Label(idRect, "Entity ID", style);
|
|||
|
GUI.Label(genRect, "Generation", style);
|
|||
|
GUI.Label(worldRect, "World ID", style);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void DrawTemplates()
|
|||
|
{
|
|||
|
var iterator = serializedObject.GetIterator();
|
|||
|
iterator.NextVisible(true);
|
|||
|
bool enterChildren = true;
|
|||
|
while (iterator.NextVisible(enterChildren))
|
|||
|
{
|
|||
|
EditorGUILayout.PropertyField(iterator, true);
|
|||
|
enterChildren = false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void DrawButtons()
|
|||
|
{
|
|||
|
GUILayout.BeginHorizontal();
|
|||
|
if (GUILayout.Button("Autoset"))
|
|||
|
{
|
|||
|
Target.SetTemplates_Editor(Target.GetComponents<MonoEntityTemplate>());
|
|||
|
EditorUtility.SetDirty(target);
|
|||
|
}
|
|||
|
if (GUILayout.Button("Autoset Cascade"))
|
|||
|
{
|
|||
|
foreach (var item in Target.GetComponentsInChildren<EcsEntityConnect>())
|
|||
|
{
|
|||
|
item.SetTemplates_Editor(item.GetComponents<MonoEntityTemplate>());
|
|||
|
EditorUtility.SetDirty(item);
|
|||
|
}
|
|||
|
}
|
|||
|
GUILayout.EndHorizontal();
|
|||
|
}
|
|||
|
|
|||
|
private void DrawComponents(EcsEntityConnect[] targets)
|
|||
|
{
|
|||
|
if (IsMultipleTargets)
|
|||
|
{
|
|||
|
for (int i = 0; i < targets.Length; i++)
|
|||
|
{
|
|||
|
if (targets[i].IsConected == true)
|
|||
|
{
|
|||
|
EditorGUILayout.HelpBox("Multiple component editing is not available.", MessageType.Warning);
|
|||
|
return;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
if (Target.Entity.TryUnpack(out int entityID, out EcsWorld world))
|
|||
|
{
|
|||
|
EcsGUI.Layout.DrawRuntimeComponents(entityID, world);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
#endif
|