DragonECS-Unity/src/UnityGameObject.cs

166 lines
4.5 KiB
C#
Raw Normal View History

using System.Runtime.CompilerServices;
using UnityEngine;
2023-04-26 16:54:27 +08:00
using System.Linq;
using UnityEditor.ShortcutManagement;
#if UNITY_EDITOR
using UnityEditor;
#endif
2023-03-29 19:58:58 +08:00
namespace DCFApixels.DragonECS
{
[DebugColor(DebugColor.Cyan)]
2023-04-24 16:48:26 +08:00
public struct UnityGameObject : IEcsComponent
{
public GameObject gameObject;
public Transform transform;
public string Name
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => gameObject.name;
}
public UnityGameObject(GameObject gameObject)
{
this.gameObject = gameObject;
transform = gameObject.transform;
}
}
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
}
public static class GameObjectIconConsts
{
public const int RAW_LABEL_ICON_LAST = (int)GameObjectIcon.Label_Purple;
}
public static class GameObjectRefExt
{
2023-04-24 16:48:26 +08:00
public static EcsEntity NewEntityWithGameObject(this EcsWorld self, string name = "EcsEntity", GameObjectIcon icon = GameObjectIcon.NONE)
{
2023-04-24 16:48:26 +08:00
EcsEntity result = self.NewEntity();
GameObject newGameObject = new GameObject(name);
2023-04-26 16:54:27 +08:00
newGameObject.AddComponent<EcsEntityConnect>().ConectWith(result);
// self.GetPool<UnityGameObject>().Add(result.id) =
#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;
}
}
2023-04-26 16:54:27 +08:00
2023-04-24 16:48:26 +08:00
public class EcsEntityConnect : MonoBehaviour
{
2023-04-26 16:54:27 +08:00
private sealed class Query : EcsQuery
{
public readonly EcsPool<UnityGameObject> unityGameObjects;
public Query(Builder b)
{
unityGameObjects = b.Include<UnityGameObject>();
}
}
private EcsEntity _entity;
private EcsWorld _world;
#region Properties
public EcsEntity Entity
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _entity;
}
2023-04-26 16:54:27 +08:00
public EcsWorld World
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _world;
}
public bool IsAlive
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2023-04-24 16:48:26 +08:00
get => _entity.IsAlive;
}
2023-04-26 16:54:27 +08:00
#endregion
public void ConectWith(EcsEntity entity)
{
int e = _entity.id;
if (_world != null && _entity.IsNotNull)
{
var q = _world.Select<Query>();
q.unityGameObjects.Del(e);
}
_world = null;
_entity = entity;
if (_entity.IsNotNull)
{
_world = _entity.GetWorld();
var q = _world.Select<Query>();
if (!q.unityGameObjects.Has(e)) q.unityGameObjects.Add(e) = new UnityGameObject(gameObject);
}
}
}
2023-04-07 17:16:22 +08:00
#if UNITY_EDITOR
namespace Editors
{
using UnityEditor;
2023-04-24 16:48:26 +08:00
[CustomEditor(typeof(EcsEntityConnect))]
2023-04-07 17:16:22 +08:00
public class EcsEntityEditor : Editor
{
2023-04-24 16:48:26 +08:00
private EcsEntityConnect Target => (EcsEntityConnect)target;
2023-04-07 17:16:22 +08:00
public override void OnInspectorGUI()
{
2023-04-26 16:54:27 +08:00
EditorGUILayout.IntField(Target.Entity.id);
GUILayout.Label(Target.Entity.ToString());
2023-04-07 17:16:22 +08:00
}
}
}
#endif
}