A Game's Own Files#

Helpers for the files that ship alongside your source. They take a bare name and work out where it actually lives, which differs between a cart served from play.onejs.com and a project on disk.

For everything else, including JSON, text, and the platform rules that make StreamingAssets a URL rather than a directory on Android and WebGL, use the general loaders on the assets guide. oj does not wrap those and does not need to.

assetUrl#

import { assetUrl } from "oj"

const url = assetUrl("pop.wav")

Resolves a bare name to a URL. Reach for it when an API outside oj wants a URL, for example loadImageAsync from onejs-unity/assets. oj's own loaders already take bare names.

useSound#

import { useSound, Button } from "oj"

function Pop() {
    const pop = useSound("pop.wav")
    return <Button text="Pop" onClick={() => pop?.play()} />
}

useSound(name) is null until the file arrives, then a sound that plays as often as you ask. It unloads with the component, which also stops anything still playing it. Outside a component, audio.load takes the same bare name and returns a promise.

loadTexture and useTexture#

import { useTexture, View } from "oj"

function Sprite() {
    const tex = useTexture("player.png")
    if (tex === null) return <View />
    return <View style={{ backgroundImage: tex }} />
}

loadTexture(name) returns a promise. useTexture(name) is the hook form, returning null until the texture resolves and the loaded texture afterwards, so render a placeholder for that first pass rather than assuming it is there.

Both take a bare name and resolve it for you, as useSound does. A full URL passes through unchanged, so there is rarely a reason to call assetUrl before one of these.