30 lines
732 B
TypeScript
30 lines
732 B
TypeScript
|
import { Loader, ILoaderOptions } from "pixi.js";
|
||
|
import { useEffect, useState } from "react";
|
||
|
|
||
|
export type ResourceList = Record<
|
||
|
string,
|
||
|
string | { name: string; options: ILoaderOptions }
|
||
|
>;
|
||
|
|
||
|
export default function useLoader(resources: ResourceList) {
|
||
|
const loader = new Loader();
|
||
|
|
||
|
const [state, setState] = useState({ loaded: false, resources: null });
|
||
|
|
||
|
useEffect(() => {
|
||
|
for (const res in resources) {
|
||
|
const param = resources[res];
|
||
|
if (typeof param === "string") {
|
||
|
loader.add(res, param);
|
||
|
} else {
|
||
|
loader.add(res, param.name, param.options);
|
||
|
}
|
||
|
}
|
||
|
loader.load((loader, resources) => {
|
||
|
setState({ loaded: true, resources });
|
||
|
});
|
||
|
}, []);
|
||
|
|
||
|
return state;
|
||
|
}
|