Load detailed guidance based on context:
| Topic | Reference | Load When |
|---|---|---|
| Unity Development | references/unity-patterns.md |
Unity C#, MonoBehaviour, Scriptable Objects |
| Unreal Development | references/unreal-cpp.md |
Unreal C++, Blueprints, Actor components |
| ECS & Patterns | references/ecs-patterns.md |
Entity Component System, game patterns |
| Performance | references/performance-optimization.md |
FPS optimization, profiling, memory |
| Networking | references/multiplayer-networking.md |
Multiplayer, client-server, lag compensation |
When implementing game features, provide:
public class ObjectPool<T> where T : Component
{
private readonly Queue<T> _pool = new();
private readonly T _prefab;
private readonly Transform _parent;
public ObjectPool(T prefab, int initialSize, Transform parent = null)
{
_prefab = prefab;
_parent = parent;
for (int i = 0; i < initialSize; i++)
Release(Create());
}
public T Get()
{
T obj = _pool.Count > 0 ? _pool.Dequeue() : Create();
obj.gameObject.SetActive(true);
return obj;
}
public void Release(T obj)
{
obj.gameObject.SetActive(false);
_pool.Enqueue(obj);
}
private T Create() => Object.Instantiate(_prefab, _parent);
}
public class PlayerController : MonoBehaviour
{
// Cache all component references in Awake — never call GetComponent in Update
private Rigidbody _rb;
private Animator _animator;
private PlayerInput _input;
private void Awake()
{
_rb = GetComponent<Rigidbody>();
_animator = GetComponent<Animator>();
_input = GetComponent<PlayerInput>();
}
private void FixedUpdate()
{
// Use cached references; use deltaTime for frame-independence
Vector3 move = _input.MoveDirection * (speed * Time.fixedDeltaTime);
_rb.MovePosition(_rb.position + move);
}
}
public abstract class State
{
public abstract void Enter();
public abstract void Tick(float deltaTime);
public abstract void Exit();
}
public class StateMachine
{
private State _current;
public void TransitionTo(State next)
{
_current?.Exit();
_current = next;
_current.Enter();
}
public void Tick(float deltaTime) => _current?.Tick(deltaTime);
}
// Usage example
public class IdleState : State
{
private readonly Animator _animator;
public IdleState(Animator animator) => _animator = animator;
public override void Enter() => _animator.SetTrigger("Idle");
public override void Tick(float deltaTime) { /* poll transitions */ }
public override void Exit() { }
}