@Singtaa: Please consider leaving a review on the Asset Store if you enjoy using OneJS. It really helps us a lot! And, I promise it'll take less time than debugging your last script.😊 Oh also, OneJS V2 is now out of preview!
Menu

useUnityUpdate Hook

About

useUnityUpdate() is a hook that allows you to perform simple operations in the Unity's LateUpdate() life cycle.

When not to use it

  • Performing critical operations
  • Reading input during gameplay or intense scenes
  • Performing heavy operations

So... When can I use it?

If performance isn't a problem, you can use to read simple inputs to perform actions (i.e: "Press enter to continue" scene). Should be noticed, that if you're using the new input system, subscribing to callbacks will be more performant than this method.

Steps to implement the hook

1- Create a file called unity-hooks.tsx

2- Copy the following code:

TYPESCRIPT
const update = signal({});

export const useUnityUpdate = (callback: () => void) => {
    return update.subscribe(() => callback());
};

function Update(time) {
    requestAnimationFrame(Update);
    update.value = {};
}
requestAnimationFrame(Update);

How to use it:

TYPESCRIPT

export const SplashScreen = () => {

    // Callback once per LateUpdate()
    const unsubscribe = useUnityUpdate(() => {
        if(Input.GetKeyDown(KeyCode.A) {
              // Do something
        }
    });

    // always, always, always (ALWAYS!!!) unsubscribe, else you'll have memory leak / bleed
    useEffect(() => {
        return () => {
            unsubscribe();
        }
    }, []);
}