DragonECS-Unity/src/Connectors/GameObjectConnect.cs

120 lines
3.8 KiB
C#
Raw Normal View History

2025-03-14 17:02:34 +08:00
#if DISABLE_DEBUG
#undef DEBUG
#endif
using System.Runtime.CompilerServices;
using UnityEngine;
2024-06-13 18:04:47 +08:00
using DCFApixels.DragonECS.Unity;
2025-03-17 16:03:55 +08:00
using DCFApixels.DragonECS.Core;
#if UNITY_EDITOR
using UnityEditor;
#endif
2023-03-29 19:58:58 +08:00
namespace DCFApixels.DragonECS
{
2024-09-16 19:30:49 +08:00
[MetaColor(MetaColor.DragonCyan)]
2024-06-13 18:04:47 +08:00
[MetaGroup(EcsUnityConsts.PACK_GROUP, EcsConsts.COMPONENTS_GROUP)]
[MetaDescription(EcsConsts.AUTHOR, "This component is automatically added if an entity is connected to one of the EcsEntityConnect. It also contains a reference to the connected EcsEntityConnect.")]
2025-03-19 16:31:32 +08:00
[MetaID("DragonECS_14AC6B239201C6A60337AF3384D237E7")]
2024-10-11 23:29:48 +08:00
[MetaTags(MetaTags.ENGINE_MEMBER)]
2024-11-08 15:21:13 +08:00
[System.Serializable]
2024-03-10 22:27:17 +08:00
public readonly struct GameObjectConnect : IEcsComponent, IEcsComponentLifecycle<GameObjectConnect>
{
2024-03-10 22:27:17 +08:00
public readonly EcsEntityConnect Connect;
2024-03-10 10:20:44 +08:00
public bool IsConnected
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-10 22:27:17 +08:00
get { return Connect != null; }
2024-03-10 10:20:44 +08:00
}
2024-03-10 22:27:17 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal GameObjectConnect(EcsEntityConnect connect)
{
2024-03-10 22:27:17 +08:00
Connect = connect;
}
2024-03-10 19:23:05 +08:00
2024-03-10 22:27:17 +08:00
void IEcsComponentLifecycle<GameObjectConnect>.Enable(ref GameObjectConnect component)
2024-03-10 19:23:05 +08:00
{
component = default;
}
2024-03-10 22:27:17 +08:00
void IEcsComponentLifecycle<GameObjectConnect>.Disable(ref GameObjectConnect component)
2024-03-10 19:23:05 +08:00
{
2024-03-10 22:27:17 +08:00
if (component.Connect != null)
2024-03-10 19:23:05 +08:00
{
2024-03-10 22:27:17 +08:00
component.Connect.Disconnect();
2024-03-10 19:23:05 +08:00
}
component = default;
}
2025-03-10 13:08:18 +08:00
public override string ToString()
{
return $"GO({(Connect == null ? "NULL" : Connect.name)})";
}
}
public enum GameObjectIcon : byte
{
NONE,
Label_Gray,
Label_Blue,
Label_Teal,
Label_Green,
Label_Yellow,
Label_Orange,
Label_Red,
Label_Purple,
Circle_Gray,
Circle_Blue,
Circle_Teal,
Circle_Green,
Circle_Yellow,
Circle_Orange,
Circle_Red,
Circle_Purple,
Diamond_Gray,
Diamond_Blue,
Diamond_Teal,
Diamond_Green,
Diamond_Yellow,
Diamond_Orange,
Diamond_Red,
Diamond_Purple
}
2024-03-10 22:27:17 +08:00
internal static class GameObjectIconConsts
{
public const int RAW_LABEL_ICON_LAST = (int)GameObjectIcon.Label_Purple;
}
public static class GameObjectRefExt
{
2026-03-02 17:01:46 +08:00
public static entlong NewEntityWithGameObject(this EcsWorld world, ITemplateNode template, string name = "Entity", GameObjectIcon icon = GameObjectIcon.NONE)
{
entlong e = world.NewEntityWithGameObject(name, icon);
template.Apply(world.ID, e.ID);
return e;
}
2024-03-03 03:51:49 +08:00
public static entlong NewEntityWithGameObject(this EcsWorld self, string name = "Entity", GameObjectIcon icon = GameObjectIcon.NONE)
{
2024-03-03 03:51:49 +08:00
entlong result = self.NewEntityLong();
GameObject newGameObject = new GameObject(name);
2024-03-10 09:50:20 +08:00
newGameObject.AddComponent<EcsEntityConnect>().ConnectWith(result, false);
#if UNITY_EDITOR
if (icon != GameObjectIcon.NONE)
{
string contentName;
int number = (int)icon - 1;
if (number < GameObjectIconConsts.RAW_LABEL_ICON_LAST)
{
contentName = $"sv_label_{number}";
}
else
{
number -= GameObjectIconConsts.RAW_LABEL_ICON_LAST;
contentName = $"sv_icon_dot{number}_pix16_gizmo";
}
GUIContent iconContent = EditorGUIUtility.IconContent(contentName);
EditorGUIUtility.SetIconForObject(newGameObject, (Texture2D)iconContent.image);
}
#endif
return result;
}
}
}