Skip to content

Instantly share code, notes, and snippets.

@daimahou
daimahou / HybridComponents.cs
Last active August 11, 2024 10:00
Simple monobehaviour - entity hybrid sample
using System;
using Unity.Entities;
using UnityEngine;
public sealed class EntityBehaviour : MonoBehaviour {
public Entity Entity { get; private set; }
void Awake() {
World world = World.DefaultGameObjectInjectionWorld;
@daimahou
daimahou / CompanionObjectAuthoring.cs
Last active July 27, 2026 01:35
Simple implementation of hybrid game objects - entity in unity ECS
// Spawns a game object and link it to the entity this component is attached to.
// The linked game object will have the local transform of the entity copied to its transform automatically by the system
// Any additional monobehaviour can be made accessible from the ECS world by implementing the ICompanionComponent interface
//
// Note: Game Object / Entity destruction is not handled in this gist
public class CompanionObjectAuthoring : MonoBehaviour {
[SerializeField] GameObject prefab;
class Baker : Baker<CompanionObjectAuthoring> {
public override void Bake(CompanionObjectAuthoring authoring) {
@daimahou
daimahou / MVCVM.cs
Created May 15, 2024 02:51
Demonstrates a clean architectural pattern for Unity using MVCVM principles
/// <summary>
/// Immutable interface unity components interact with, delegate the IFeature implementation to a concrete class.
/// FeatureComponent acts as a bridge between the unity eco-system and the IFeature implementation.
/// Additionally, it also implements a factory pattern to instantiate the IFeature implementation.
///
/// Goal: support change without breaking prefabs and scene references.
/// </summary>
class FeatureComponent : MonoBehaviour, IFeature
{
// Dependencies: ------------------------------------------------------------------------------