Douglas P. Fields, Jr.
symbolics at lisp.engineer
-
Create a new, blank Unity 2D game, using Unity 5.4.0f3.
-
Unity menu: Edit -> Project Settings -> Player
- Resolution & Presentation -> Run in Background* -> CHECK
- Other Settings -> Api Compatibility Level -> .NET 2.0
-
Clone Arcadia Github into
Assets
directory and let Unity load it (be patient) -
cd Assets/Arcadia/Infrastructure
and run the./repl
program there, then(require '[arcadia.core :refer :all])
- Leave the REPL open for later
-
Drag a (random, small) image into your Unity
Assets
pane/window -
Drag that image from the
Assets
window into theScene
pane/window, on top of the camera icon.- Confirm that the
Hierarchy
pane shows your image (it will be aSprite
now)
- Confirm that the
-
Rename that in the
Hierarchy
window toobject-1
-
Create a new Clojure file in pathname
Assets/minimal/core.clj
with the contents below. -
In the REPL, run this:
(require '[minimal.core :refer :all])
to load it into the REPL -
Now the magic happens: We're going to have the function
first-callback
get called every frame by linking (hooking) it to theobject-1
object in the scene. This can only be done in the REPL for now, but the hook will show up in the Scene in the inspector. This will, for now, just spam the UnityConsole
window with boring messages. -
We can access game objects by name using the Arcadia function
objects-named
. This returns a list. REPL:user=> (objects-named "object-1")
->(#unity/GameObject -22008)
-
Add the hook for the function in our
minimal/core.clj
file to the sprite we namedobject-1
in the REPL:user=> (hook+ (first (objects-named "object-1")) :update #'minimal.core/first-callback)
->#unity/GameObject -22008
-
Confirm this by clicking on
object-1
in the UnityHierarchy
window and note in theInspector
that it saysUpdate Hook (Script)
with#'minimal.core/first-callback
. -
Make sure the
Console
window is showing. ClickPlay
icon in Unity. Be patient. After a short time, your first Arcadia app will start running and theConsole
window will start showing a lot of messages like:Hello, Arcadia
fromUnityEngine.Debug:Log(Object)
. -
Save your scene (whatever name you want). Quit Unity. Restart Unity and load your project. Hit Play. Everything should still work.
(ns minimal.core
(:use arcadia.core arcadia.linear))
(defn first-callback [o]
(arcadia.core/log "Hello, Arcadia"))
-
We need to enhance the namespace to use some Unity objects. Modify the
ns
in the top as per theminimal/core.clj
v2 code below. -
Write a function which reads keys and prints something.
Input/GetKey
returns true everyupdate
when a key is held down.Input/GetKeyDown
returns true only the firstupdate
a key has been pressed- See code below for
move-when-arrow-pressed
-
Remove our old Hello World hook to
first-callback
in the Unity GUI by clicking the gear icon next toUpdate Hook (Script)
whenobject-1
is showing in theInspector
window. ChooseRemove Component
. -
Tell Arcadia that our
.clj
file has changed by using this command in the REPL:(require 'minimal.core :reload)
. The Arcadia team will probably have a file watcher in the future that may obviate this step. -
In the REPL, add our new hook:
(hook+ (first (objects-named "object-1")) :update #'minimal.core/move-when-arrow-pressed)
- Confirm this by checking the
Inspector
forobject-1
in Unity
- Confirm this by checking the
-
Run the game, and tap & hold various arrow keys.
(ns minimal.core
(:import [UnityEngine Input KeyCode Camera Physics Time Camera])
(:use arcadia.core arcadia.linear))
;; Log a message when arrow keys are first pushed
(defn move-when-arrow-pressed [o]
(when (Input/GetKeyDown KeyCode/UpArrow)
(arcadia.core/log "Up pushed"))
(when (Input/GetKeyDown KeyCode/DownArrow)
(arcadia.core/log "Down pushed"))
(when (Input/GetKeyDown KeyCode/RightArrow)
(arcadia.core/log "Right pushed"))
(when (Input/GetKeyDown KeyCode/LeftArrow)
(arcadia.core/log "Left pushed")))
;; Hook our new move callback
#_
(hook+ (first (objects-named "object-1")) :update #'minimal.core/move-when-arrow-pressed)
TODO: Use the input to modify the location of the object.
TODO: Generate and remove game objects, and change the sprite they display, etc.