From 90cda16424bcf8d89e4f803dff06025f6d68fa03 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Thu, 16 May 2024 19:59:57 +0800 Subject: [PATCH] update --- src/Buildin/UnityComponents.cs | 10 ++++- ...ribute.cs => ComponentTemplateProperty.cs} | 11 +++-- ...meta => ComponentTemplateProperty.cs.meta} | 0 ....cs => ComponentTemplatePropertyDrawer.cs} | 37 ++++++++-------- ...> ComponentTemplatePropertyDrawer.cs.meta} | 0 src/Internal/Editor/EcsGUI.cs | 42 ++++++------------- 6 files changed, 47 insertions(+), 53 deletions(-) rename src/EntityTemplate/{ComponentTemplateReferenceAttribute.cs => ComponentTemplateProperty.cs} (93%) rename src/EntityTemplate/{ComponentTemplateReferenceAttribute.cs.meta => ComponentTemplateProperty.cs.meta} (100%) rename src/EntityTemplate/Editor/{ComponentTemplateReferenceDrawer.cs => ComponentTemplatePropertyDrawer.cs} (89%) rename src/EntityTemplate/Editor/{ComponentTemplateReferenceDrawer.cs.meta => ComponentTemplatePropertyDrawer.cs.meta} (100%) diff --git a/src/Buildin/UnityComponents.cs b/src/Buildin/UnityComponents.cs index e6a17f3..f71ad55 100644 --- a/src/Buildin/UnityComponents.cs +++ b/src/Buildin/UnityComponents.cs @@ -1,23 +1,27 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Runtime.CompilerServices; using UnityEngine; namespace DCFApixels.DragonECS { 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 ColliderGroup = new MetaGroup($"{UNITY_COMPONENT_NAME}/Collider/"); public static readonly MetaGroup JointGroup = new MetaGroup($"{UNITY_COMPONENT_NAME}/Joint/"); } [Serializable] [MetaColor(255 / 3, 255, 0)] + [MetaDescription("Component-reference to Unity object for EcsPool")] + [MetaGroup(UnityComponentConsts.UNITY_COMPONENT_NAME)] public struct UnityComponent : IEcsComponent, IEnumerable//IntelliSense hack where T : Component { public T obj; + [MethodImpl(MethodImplOptions.AggressiveInlining)] public UnityComponent(T obj) { this.obj = obj; @@ -30,6 +34,10 @@ namespace DCFApixels.DragonECS { throw new NotImplementedException(); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator T(UnityComponent a) { return a.obj; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator UnityComponent(T a) { return new UnityComponent(a); } } #region Unity Component Templates diff --git a/src/EntityTemplate/ComponentTemplateReferenceAttribute.cs b/src/EntityTemplate/ComponentTemplateProperty.cs similarity index 93% rename from src/EntityTemplate/ComponentTemplateReferenceAttribute.cs rename to src/EntityTemplate/ComponentTemplateProperty.cs index 3a3c372..c467896 100644 --- a/src/EntityTemplate/ComponentTemplateReferenceAttribute.cs +++ b/src/EntityTemplate/ComponentTemplateProperty.cs @@ -9,11 +9,11 @@ namespace DCFApixels.DragonECS { [SerializeReference, ComponentTemplateReference] private IComponentTemplate _template; + [MethodImpl(MethodImplOptions.AggressiveInlining)] public ComponentTemplateProperty(IComponentTemplate template) { _template = template; } - public IComponentTemplate Template { [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -41,27 +41,26 @@ namespace DCFApixels.DragonECS public void OnValidate(UnityEngine.Object obj) { _template.OnValidate(obj); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void SetRaw(object raw) { _template.SetRaw(raw); } - public bool Equals(ComponentTemplateProperty other) { return _template == other._template; } + public override bool Equals(object obj) + { + return obj is ComponentTemplateProperty other && Equals(other); + } public override int 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, Null? b) { return a.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 !=(Null? a, ComponentTemplateProperty b) { return !b.IsNull; } - public readonly struct Null { } } - public sealed class ComponentTemplateReferenceAttribute : PropertyAttribute { } } \ No newline at end of file diff --git a/src/EntityTemplate/ComponentTemplateReferenceAttribute.cs.meta b/src/EntityTemplate/ComponentTemplateProperty.cs.meta similarity index 100% rename from src/EntityTemplate/ComponentTemplateReferenceAttribute.cs.meta rename to src/EntityTemplate/ComponentTemplateProperty.cs.meta diff --git a/src/EntityTemplate/Editor/ComponentTemplateReferenceDrawer.cs b/src/EntityTemplate/Editor/ComponentTemplatePropertyDrawer.cs similarity index 89% rename from src/EntityTemplate/Editor/ComponentTemplateReferenceDrawer.cs rename to src/EntityTemplate/Editor/ComponentTemplatePropertyDrawer.cs index 88afa19..2552384 100644 --- a/src/EntityTemplate/Editor/ComponentTemplateReferenceDrawer.cs +++ b/src/EntityTemplate/Editor/ComponentTemplatePropertyDrawer.cs @@ -7,9 +7,23 @@ using UnityEngine; 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)] - public class ComponentTemplateReferenceDrawer : PropertyDrawer + internal class ComponentTemplateReferenceDrawer : PropertyDrawer { 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 GenericMenu _genericMenu; + #region Init private static void Init() { @@ -64,7 +79,6 @@ namespace DCFApixels.DragonECS.Unity.Editors } #endregion - public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { IComponentTemplate template = property.managedReferenceValue as IComponentTemplate; @@ -90,19 +104,9 @@ namespace DCFApixels.DragonECS.Unity.Editors return DamagedComponentHeight; } - var propsCounter = property.Copy(); - 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; + int propCount = EcsGUI.GetChildPropertiesCount(property); - - return (isEmpty ? EditorGUIUtility.singleLineHeight : EditorGUI.GetPropertyHeight(property, label)) + Padding * 4f; + return (propCount <= 0 ? EditorGUIUtility.singleLineHeight : EditorGUI.GetPropertyHeight(property, label)) + Padding * 4f; } @@ -167,7 +171,7 @@ namespace DCFApixels.DragonECS.Unity.Editors EditorGUI.BeginChangeCheck(); - GUI.Box(position, "", UnityEditorUtility.GetStyle(alphaPanelColor)); + EditorGUI.DrawRect(position, alphaPanelColor); 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) { EditorGUI.LabelField(position, label); - //Rect buttonRect = RectUtility.AddPadding(position, EditorGUIUtility.labelWidth, 20f, 0f, 0f); Rect buttonRect = RectUtility.AddPadding(position, EditorGUIUtility.labelWidth, 0f, 0f, 0f); if (GUI.Button(buttonRect, "Select")) { diff --git a/src/EntityTemplate/Editor/ComponentTemplateReferenceDrawer.cs.meta b/src/EntityTemplate/Editor/ComponentTemplatePropertyDrawer.cs.meta similarity index 100% rename from src/EntityTemplate/Editor/ComponentTemplateReferenceDrawer.cs.meta rename to src/EntityTemplate/Editor/ComponentTemplatePropertyDrawer.cs.meta diff --git a/src/Internal/Editor/EcsGUI.cs b/src/Internal/Editor/EcsGUI.cs index 4f5938e..cc9fbe0 100644 --- a/src/Internal/Editor/EcsGUI.cs +++ b/src/Internal/Editor/EcsGUI.cs @@ -4,6 +4,8 @@ using System; using System.Reflection; using UnityEditor; using UnityEngine; +using UnityObject = UnityEngine.Object; +using UnityComponent = UnityEngine.Component; namespace DCFApixels.DragonECS.Unity.Editors { @@ -339,6 +341,10 @@ namespace DCFApixels.DragonECS.Unity.Editors public static class Layout { + public static void DrawEmptyComponentProperty(bool isDisplayEmpty) + { + + } public static void DrawWorldBaseInfo(EcsWorld world) { bool isNull = world == null || world.IsDestroyed || world.id == 0; @@ -436,30 +442,8 @@ namespace DCFApixels.DragonECS.Unity.Editors if (meta.IsHidden == false || IsShowHidden) { object data = pool.GetRaw(entityID); - //Color panelColor = meta.Color.ToUnityColor().Desaturate(EscEditorConsts.COMPONENT_DRAWER_DESATURATE); - Color panelColor; - 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); + Color panelColor = SelectPanelColor(meta, index, total).Desaturate(EscEditorConsts.COMPONENT_DRAWER_DESATURATE); float padding = EditorGUIUtility.standardVerticalSpacing; Rect removeButtonRect = GUILayoutUtility.GetLastRect(); @@ -472,7 +456,7 @@ namespace DCFApixels.DragonECS.Unity.Editors removeButtonRect.yMax += RemoveButtonRect.height; removeButtonRect.xMin = removeButtonRect.xMax - RemoveButtonRect.width; removeButtonRect.center += Vector2.up * padding * 2f; - if (EcsGUI.CloseButton(removeButtonRect)) + if (CloseButton(removeButtonRect)) { isRemoveComponent = true; } @@ -497,7 +481,7 @@ namespace DCFApixels.DragonECS.Unity.Editors Rect tooltipIconRect = TooltipIconRect; tooltipIconRect.center = removeButtonRect.center; tooltipIconRect.center -= Vector2.right * tooltipIconRect.width; - EcsGUI.DescriptionIcon(tooltipIconRect, meta.Description.Text); + DescriptionIcon(tooltipIconRect, meta.Description.Text); } GUILayout.EndVertical(); @@ -509,7 +493,7 @@ namespace DCFApixels.DragonECS.Unity.Editors outData = data; 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) { @@ -546,12 +530,12 @@ namespace DCFApixels.DragonECS.Unity.Editors if (isUnityObject) { 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) { - uobj = EditorGUILayout.ObjectField(label, uobj, typeof(UnityEngine.Object), true); + uobj = EditorGUILayout.ObjectField(label, uobj, typeof(UnityObject), true); } else {