This commit is contained in:
Mikhail 2024-05-16 19:59:57 +08:00
parent e9ccb3f48e
commit 90cda16424
6 changed files with 47 additions and 53 deletions

View File

@ -1,23 +1,27 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine; using UnityEngine;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
internal static class UnityComponentConsts internal static class UnityComponentConsts
{ {
private const string UNITY_COMPONENT_NAME = "UnityComponent"; internal const string UNITY_COMPONENT_NAME = "UnityComponent";
public static readonly MetaGroup BaseGroup = new MetaGroup(UNITY_COMPONENT_NAME); public static readonly MetaGroup BaseGroup = new MetaGroup(UNITY_COMPONENT_NAME);
public static readonly MetaGroup ColliderGroup = new MetaGroup($"{UNITY_COMPONENT_NAME}/Collider/"); public static readonly MetaGroup ColliderGroup = new MetaGroup($"{UNITY_COMPONENT_NAME}/Collider/");
public static readonly MetaGroup JointGroup = new MetaGroup($"{UNITY_COMPONENT_NAME}/Joint/"); public static readonly MetaGroup JointGroup = new MetaGroup($"{UNITY_COMPONENT_NAME}/Joint/");
} }
[Serializable] [Serializable]
[MetaColor(255 / 3, 255, 0)] [MetaColor(255 / 3, 255, 0)]
[MetaDescription("Component-reference to Unity object for EcsPool")]
[MetaGroup(UnityComponentConsts.UNITY_COMPONENT_NAME)]
public struct UnityComponent<T> : IEcsComponent, IEnumerable<T>//IntelliSense hack public struct UnityComponent<T> : IEcsComponent, IEnumerable<T>//IntelliSense hack
where T : Component where T : Component
{ {
public T obj; public T obj;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public UnityComponent(T obj) public UnityComponent(T obj)
{ {
this.obj = obj; this.obj = obj;
@ -30,6 +34,10 @@ namespace DCFApixels.DragonECS
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator T(UnityComponent<T> a) { return a.obj; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator UnityComponent<T>(T a) { return new UnityComponent<T>(a); }
} }
#region Unity Component Templates #region Unity Component Templates

View File

@ -9,11 +9,11 @@ namespace DCFApixels.DragonECS
{ {
[SerializeReference, ComponentTemplateReference] [SerializeReference, ComponentTemplateReference]
private IComponentTemplate _template; private IComponentTemplate _template;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ComponentTemplateProperty(IComponentTemplate template) public ComponentTemplateProperty(IComponentTemplate template)
{ {
_template = template; _template = template;
} }
public IComponentTemplate Template public IComponentTemplate Template
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -41,27 +41,26 @@ namespace DCFApixels.DragonECS
public void OnValidate(UnityEngine.Object obj) { _template.OnValidate(obj); } public void OnValidate(UnityEngine.Object obj) { _template.OnValidate(obj); }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetRaw(object raw) { _template.SetRaw(raw); } public void SetRaw(object raw) { _template.SetRaw(raw); }
public bool Equals(ComponentTemplateProperty other) public bool Equals(ComponentTemplateProperty other)
{ {
return _template == other._template; return _template == other._template;
} }
public override bool Equals(object obj)
{
return obj is ComponentTemplateProperty other && Equals(other);
}
public override int GetHashCode() public override int GetHashCode()
{ {
return _template.GetHashCode(); return _template.GetHashCode();
} }
public static bool operator ==(ComponentTemplateProperty a, ComponentTemplateProperty b) { return a._template == b._template; } public static bool operator ==(ComponentTemplateProperty a, ComponentTemplateProperty b) { return a._template == b._template; }
public static bool operator !=(ComponentTemplateProperty a, ComponentTemplateProperty b) { return a._template != b._template; } public static bool operator !=(ComponentTemplateProperty a, ComponentTemplateProperty b) { return a._template != b._template; }
public static bool operator ==(ComponentTemplateProperty a, Null? b) { return a.IsNull; } public static bool operator ==(ComponentTemplateProperty a, Null? b) { return a.IsNull; }
public static bool operator ==(Null? a, ComponentTemplateProperty b) { return b.IsNull; } public static bool operator ==(Null? a, ComponentTemplateProperty b) { return b.IsNull; }
public static bool operator !=(ComponentTemplateProperty a, Null? b) { return !a.IsNull; } public static bool operator !=(ComponentTemplateProperty a, Null? b) { return !a.IsNull; }
public static bool operator !=(Null? a, ComponentTemplateProperty b) { return !b.IsNull; } public static bool operator !=(Null? a, ComponentTemplateProperty b) { return !b.IsNull; }
public readonly struct Null { } public readonly struct Null { }
} }
public sealed class ComponentTemplateReferenceAttribute : PropertyAttribute { } public sealed class ComponentTemplateReferenceAttribute : PropertyAttribute { }
} }

View File

@ -7,9 +7,23 @@ using UnityEngine;
namespace DCFApixels.DragonECS.Unity.Editors namespace DCFApixels.DragonECS.Unity.Editors
{ {
[CustomPropertyDrawer(typeof(ComponentTemplateProperty), true)]
internal class ComponentTemplatePropertyDrawer : PropertyDrawer
{
private ComponentTemplateReferenceDrawer _drawer = new ComponentTemplateReferenceDrawer();
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
property.Next(true);
return _drawer.GetPropertyHeight(property, label);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
property.Next(true);
_drawer.OnGUI(position, property, label);
}
}
[CustomPropertyDrawer(typeof(ComponentTemplateReferenceAttribute), true)] [CustomPropertyDrawer(typeof(ComponentTemplateReferenceAttribute), true)]
public class ComponentTemplateReferenceDrawer : PropertyDrawer internal class ComponentTemplateReferenceDrawer : PropertyDrawer
{ {
private static readonly Rect HeadIconsRect = new Rect(0f, 0f, 19f, 19f); private static readonly Rect HeadIconsRect = new Rect(0f, 0f, 19f, 19f);
@ -19,6 +33,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
private static bool _isInit; private static bool _isInit;
private static GenericMenu _genericMenu; private static GenericMenu _genericMenu;
#region Init #region Init
private static void Init() private static void Init()
{ {
@ -64,7 +79,6 @@ namespace DCFApixels.DragonECS.Unity.Editors
} }
#endregion #endregion
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{ {
IComponentTemplate template = property.managedReferenceValue as IComponentTemplate; IComponentTemplate template = property.managedReferenceValue as IComponentTemplate;
@ -90,19 +104,9 @@ namespace DCFApixels.DragonECS.Unity.Editors
return DamagedComponentHeight; return DamagedComponentHeight;
} }
var propsCounter = property.Copy(); int propCount = EcsGUI.GetChildPropertiesCount(property);
int lastDepth = propsCounter.depth;
bool next = propsCounter.Next(true) && lastDepth < propsCounter.depth;
int propCount = next ? -1 : 0;
while (next)
{
propCount++;
next = propsCounter.Next(false);
}
bool isEmpty = propCount <= 0;
return (propCount <= 0 ? EditorGUIUtility.singleLineHeight : EditorGUI.GetPropertyHeight(property, label)) + Padding * 4f;
return (isEmpty ? EditorGUIUtility.singleLineHeight : EditorGUI.GetPropertyHeight(property, label)) + Padding * 4f;
} }
@ -167,7 +171,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
GUI.Box(position, "", UnityEditorUtility.GetStyle(alphaPanelColor)); EditorGUI.DrawRect(position, alphaPanelColor);
Rect paddingPosition = RectUtility.AddPadding(position, Padding * 2f); Rect paddingPosition = RectUtility.AddPadding(position, Padding * 2f);
@ -233,7 +237,6 @@ namespace DCFApixels.DragonECS.Unity.Editors
private void DrawSelectionPopup(Rect position, SerializedProperty componentRefProp, GUIContent label) private void DrawSelectionPopup(Rect position, SerializedProperty componentRefProp, GUIContent label)
{ {
EditorGUI.LabelField(position, label); EditorGUI.LabelField(position, label);
//Rect buttonRect = RectUtility.AddPadding(position, EditorGUIUtility.labelWidth, 20f, 0f, 0f);
Rect buttonRect = RectUtility.AddPadding(position, EditorGUIUtility.labelWidth, 0f, 0f, 0f); Rect buttonRect = RectUtility.AddPadding(position, EditorGUIUtility.labelWidth, 0f, 0f, 0f);
if (GUI.Button(buttonRect, "Select")) if (GUI.Button(buttonRect, "Select"))
{ {

View File

@ -4,6 +4,8 @@ using System;
using System.Reflection; using System.Reflection;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using UnityObject = UnityEngine.Object;
using UnityComponent = UnityEngine.Component;
namespace DCFApixels.DragonECS.Unity.Editors namespace DCFApixels.DragonECS.Unity.Editors
{ {
@ -339,6 +341,10 @@ namespace DCFApixels.DragonECS.Unity.Editors
public static class Layout public static class Layout
{ {
public static void DrawEmptyComponentProperty(bool isDisplayEmpty)
{
}
public static void DrawWorldBaseInfo(EcsWorld world) public static void DrawWorldBaseInfo(EcsWorld world)
{ {
bool isNull = world == null || world.IsDestroyed || world.id == 0; bool isNull = world == null || world.IsDestroyed || world.id == 0;
@ -436,30 +442,8 @@ namespace DCFApixels.DragonECS.Unity.Editors
if (meta.IsHidden == false || IsShowHidden) if (meta.IsHidden == false || IsShowHidden)
{ {
object data = pool.GetRaw(entityID); object data = pool.GetRaw(entityID);
//Color panelColor = meta.Color.ToUnityColor().Desaturate(EscEditorConsts.COMPONENT_DRAWER_DESATURATE);
Color panelColor; Color panelColor = SelectPanelColor(meta, index, total).Desaturate(EscEditorConsts.COMPONENT_DRAWER_DESATURATE);
if (meta.IsCustomColor)
{
panelColor = meta.Color.ToUnityColor();
}
else
{
switch (AutoColorMode)
{
case ComponentColorMode.Auto:
panelColor = meta.Color.ToUnityColor().Desaturate(0.48f) / 1.18f; //.Desaturate(0.48f) / 1.18f;
break;
case ComponentColorMode.Rainbow:
Color hsv = Color.HSVToRGB(1f / (Mathf.Max(total, EscEditorConsts.AUTO_COLOR_RAINBOW_MIN_RANGE)) * index, 1, 1);
panelColor = hsv.Desaturate(0.48f) / 1.18f;
break;
default:
panelColor = index % 2 == 0 ? new Color(0.40f, 0.40f, 0.40f) : new Color(0.54f, 0.54f, 0.54f);
break;
}
}
panelColor = panelColor.Desaturate(EscEditorConsts.COMPONENT_DRAWER_DESATURATE);
float padding = EditorGUIUtility.standardVerticalSpacing; float padding = EditorGUIUtility.standardVerticalSpacing;
Rect removeButtonRect = GUILayoutUtility.GetLastRect(); Rect removeButtonRect = GUILayoutUtility.GetLastRect();
@ -472,7 +456,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
removeButtonRect.yMax += RemoveButtonRect.height; removeButtonRect.yMax += RemoveButtonRect.height;
removeButtonRect.xMin = removeButtonRect.xMax - RemoveButtonRect.width; removeButtonRect.xMin = removeButtonRect.xMax - RemoveButtonRect.width;
removeButtonRect.center += Vector2.up * padding * 2f; removeButtonRect.center += Vector2.up * padding * 2f;
if (EcsGUI.CloseButton(removeButtonRect)) if (CloseButton(removeButtonRect))
{ {
isRemoveComponent = true; isRemoveComponent = true;
} }
@ -497,7 +481,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
Rect tooltipIconRect = TooltipIconRect; Rect tooltipIconRect = TooltipIconRect;
tooltipIconRect.center = removeButtonRect.center; tooltipIconRect.center = removeButtonRect.center;
tooltipIconRect.center -= Vector2.right * tooltipIconRect.width; tooltipIconRect.center -= Vector2.right * tooltipIconRect.width;
EcsGUI.DescriptionIcon(tooltipIconRect, meta.Description.Text); DescriptionIcon(tooltipIconRect, meta.Description.Text);
} }
GUILayout.EndVertical(); GUILayout.EndVertical();
@ -509,7 +493,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
outData = data; outData = data;
Type type = data == null ? typeof(void) : data.GetType(); Type type = data == null ? typeof(void) : data.GetType();
bool isUnityObject = typeof(UnityEngine.Object).IsAssignableFrom(fieldType); bool isUnityObject = typeof(UnityObject).IsAssignableFrom(fieldType);
if (isUnityObject == false && data == null) if (isUnityObject == false && data == null)
{ {
@ -546,12 +530,12 @@ namespace DCFApixels.DragonECS.Unity.Editors
if (isUnityObject) if (isUnityObject)
{ {
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
var uobj = (UnityEngine.Object)data; var uobj = (UnityObject)data;
bool isComponent = (typeof(UnityEngine.Component)).IsAssignableFrom(fieldType); bool isComponent = typeof(UnityComponent).IsAssignableFrom(fieldType);
if (isComponent) if (isComponent)
{ {
uobj = EditorGUILayout.ObjectField(label, uobj, typeof(UnityEngine.Object), true); uobj = EditorGUILayout.ObjectField(label, uobj, typeof(UnityObject), true);
} }
else else
{ {