Last active
August 29, 2015 14:07
-
-
Save toburger/c05db4283e9d824ab976 to your computer and use it in GitHub Desktop.
An experiment if Unity3D and (kind of) idiomatic F# can play nicely together.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r @"C:\Users\TBurger\Downloads\UnityEngine.dll" | |
open UnityEngine | |
module Unity = | |
[<AutoOpen>] | |
module Log = | |
let inline uprintfn fmt = | |
Printf.kprintf Debug.Log fmt | |
let inline uwprintfn fmt = | |
Printf.kprintf Debug.LogWarning fmt | |
let inline ueprintfn fmt = | |
Printf.kprintf Debug.LogError fmt | |
type Update = unit -> unit | |
type FixedUpdate = unit -> unit | |
type Updater = Update * FixedUpdate | |
type Awake = unit -> Updater | |
type Startup = unit -> Awake | |
let inline skipAwake update fixedUpdate: Awake = fun () -> update, fixedUpdate | |
let inline onlyUpdate update: Updater = update, id | |
let inline onlyFixedUpdate fixedUpdate: Updater = id, fixedUpdate | |
let skipUpdates: Updater = id, id | |
type [<AbstractClass>] FunBehaviour(startup: Startup) = | |
inherit MonoBehaviour() | |
let mutable awake, update, fixedUpdate = | |
Unchecked.defaultof<Awake>, | |
Unchecked.defaultof<Update>, | |
Unchecked.defaultof<FixedUpdate> | |
member __.Start() = awake <- startup() | |
member __.Awake() = | |
let u, fu = awake() | |
update <- u | |
fixedUpdate <- fu | |
member __.Update() = update() | |
member __.FixedUpate() = fixedUpdate() | |
(* USAGE *) | |
open Unity | |
type FSSimpleComponent() as self = | |
inherit FunBehaviour(fun () -> | |
uprintfn "in init of %O" self | |
let rigidbody = self.GetComponent<Rigidbody>() | |
let inline update () = | |
//uprintfn "in update of %O" self | |
rigidbody.transform.Rotate(Vector3(1.f, 1.f, 1.f) * self.speed * Time.deltaTime) | |
skipAwake update id) | |
member val speed = 2.0f with get, set | |
type FSOtherComponent() as self = | |
inherit FunBehaviour(fun () () -> | |
uprintfn "in awake of %O" self | |
let otherComponent = self.GetComponent<FSSimpleComponent>() | |
let otherComponentsRigidbody = otherComponent.GetComponent<Rigidbody>() | |
uprintfn "%f: %s" otherComponent.speed otherComponentsRigidbody.name | |
skipUpdates) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment