Skip to content

Instantly share code, notes, and snippets.

View malj's full-sized avatar

Luka Maljic malj

View GitHub Profile
@malj
malj / GameObjectEntity.cs
Created March 19, 2023 09:17
Unity MonoBehaviour to ECS component converter
using Unity.Entities;
using UnityEngine;
[DisallowMultipleComponent]
public class GameObjectEntity : MonoBehaviour
{
Entity entity;
EntityManager manager;
void Awake()
@malj
malj / World.cs
Created March 19, 2023 09:16
Unity MonoBehaviour ECS
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
/// <summary>
/// Enumerable component cache with methods for querying components based on their types.
/// </summary>
class World : IEnumerable<MonoBehaviour>
@malj
malj / html-import.js
Created December 7, 2022 13:25
<html-import src="path/to/file.html"></html-import>
export default class HTMLImport extends HTMLElement {
#abortController = new AbortController()
async connectedCallback() {
let src = this.getAttribute("src")
if (src === null) {
console.warn("<html-import> element requires attribute \"src\"")
this.remove()
return
}
@malj
malj / component.gd
Created January 29, 2021 17:06
Godot ECS
"""
Base class for components.
Components are nodes which only store state data. They belong to an entity, and
can optionally provide accessor methods, but don't contain any behavior logic.
"""
class_name Component extends "./ecs_node.gd"
func _enter_tree() -> void:
if owner != null:
# Register entity
@malj
malj / Singleton.cs
Last active October 20, 2020 12:57
Unity singleton
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
/*
using UnityEngine;
// Instantiated automatically each scene
[Autoload]
@malj
malj / RuntimeScriptableObject.cs
Last active November 6, 2021 05:38
Unity runtime scriptable object
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class RuntimeScriptableObject : ScriptableObject
{
#if UNITY_EDITOR
string serializedEditorState;
#endif
@malj
malj / create-singleton-hook.ts
Last active June 5, 2023 17:11
Singleton React hook (via Context API)
import { ComponentType, createElement, createContext, useContext } from "react"
export const createSingletonHook = <P, S>(
useHook: (props: P) => S
): [() => S, ComponentType<P>] => {
const Context = createContext<S | undefined>(undefined)
const SingletonHookProvider: ComponentType<P> = ({
children,
...props