feat: loader script & typecheck
This commit is contained in:
parent
3796714238
commit
71b5d545bb
|
|
@ -1,2 +1,2 @@
|
|||
/node_modules/
|
||||
/Gen/
|
||||
/Gen
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ export default defineConfig({
|
|||
unityengine: "CS.UnityEngine",
|
||||
"unityengine/ui": "CS.UnityEngine.UI",
|
||||
"system": "CS.System",
|
||||
"tmpro": "CS.TMPro",
|
||||
},
|
||||
output: {
|
||||
path: resolve(__dirname, "Gen/Resources"),
|
||||
|
|
|
|||
10
src/index.ts
10
src/index.ts
|
|
@ -1 +1,9 @@
|
|||
console.log("bler");
|
||||
import { Debug, Transform } from "unityengine";
|
||||
import { getPrefabChild, getPrefabComponent, loadPrefab } from "./loader";
|
||||
async function main() {
|
||||
const control = await loadPrefab("Control");
|
||||
const child = getPrefabChild(control, "");
|
||||
const tr = getPrefabComponent(control, "a", Transform);
|
||||
Debug.Log(tr.localScale);
|
||||
}
|
||||
main();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,53 @@
|
|||
import { $typeof } from "puerts";
|
||||
import { Awaitable, GameObject, Resources } from "unityengine";
|
||||
export async function loadPrefab<P extends keyof UnityPrefabs>(path: P) {
|
||||
return await loadResource(path) as GameObject & { PREFAB_KEY: P };
|
||||
}
|
||||
export async function loadResource(path: string) {
|
||||
const req = Resources.LoadAsync(path);
|
||||
await asPromise(req);
|
||||
return req.asset;
|
||||
}
|
||||
export function asPromise(awaiter: { GetAwaiter(): Awaitable.Awaiter }) {
|
||||
return new Promise<void>((resolve) => {
|
||||
awaiter.GetAwaiter().OnCompleted(resolve);
|
||||
});
|
||||
}
|
||||
export type Constructor<T> = {
|
||||
new(...args: any[]): T;
|
||||
};
|
||||
export type PrefabKeys = Exclude<keyof UnityPrefabs, number | symbol>;
|
||||
export type PrefabChildKeys<K extends PrefabKeys> = Exclude<
|
||||
keyof UnityPrefabs[K],
|
||||
number | symbol
|
||||
>;
|
||||
export type PrefabComponentKeys<
|
||||
K extends PrefabKeys,
|
||||
C extends PrefabChildKeys<K>,
|
||||
> = Exclude<keyof UnityPrefabs[K][C], string | symbol>;
|
||||
export type PrefabComponent<
|
||||
K extends PrefabKeys,
|
||||
C extends PrefabChildKeys<K>,
|
||||
CK extends PrefabComponentKeys<K, C>,
|
||||
> = UnityPrefabs[K][C][CK];
|
||||
export function getPrefabChild<
|
||||
P extends PrefabKeys,
|
||||
C extends PrefabChildKeys<P>,
|
||||
>(p: GameObject & { PREFAB_KEY: P }, c: C) {
|
||||
return c === "" ? p : p.transform.Find(c).gameObject as GameObject & {
|
||||
PREFAB_KEY: P;
|
||||
CHILD_KEY: C;
|
||||
};
|
||||
}
|
||||
export function getPrefabComponent<
|
||||
P extends PrefabKeys,
|
||||
C extends PrefabChildKeys<P>,
|
||||
B extends PrefabComponent<P, C, PrefabComponentKeys<P, C>>,
|
||||
>(
|
||||
p: GameObject & { PREFAB_KEY: P },
|
||||
c: C,
|
||||
b: Constructor<B>,
|
||||
) {
|
||||
const go = c === "" ? p : p.transform.Find(c).gameObject;
|
||||
return go.GetComponent($typeof(b)) as B;
|
||||
}
|
||||
|
|
@ -7,3 +7,6 @@ declare module "unityengine/ui" {
|
|||
declare module "system" {
|
||||
export = CS.System;
|
||||
}
|
||||
declare module "tmpro" {
|
||||
export = CS.TMPro;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
declare enum __Puerts_CSharpEnum { }
|
||||
|
||||
declare namespace puer {
|
||||
function $ref<T>(x?: T): CS.$Ref<T>;
|
||||
|
||||
function $unref<T>(x: CS.$Ref<T>): T;
|
||||
|
||||
function $set<T>(x: CS.$Ref<T>, val: T): void;
|
||||
|
||||
function $promise<T>(x: CS.$Task<T>): Promise<T>;
|
||||
|
||||
function $generic<T extends new (...args: any[]) => any>(
|
||||
genericType: T,
|
||||
...genericArguments:
|
||||
(typeof __Puerts_CSharpEnum | (new (...args: any[]) => any))[]
|
||||
): T;
|
||||
|
||||
function $genericMethod(
|
||||
genericType: new (...args: any[]) => any,
|
||||
methodName: string,
|
||||
...genericArguments:
|
||||
(typeof __Puerts_CSharpEnum | (new (...args: any[]) => any))[]
|
||||
): (...args: any[]) => any;
|
||||
|
||||
function $typeof(x: new (...args: any[]) => any): CS.System.Type;
|
||||
|
||||
function $extension(c: Function, e: Function): void;
|
||||
|
||||
function on(eventType: string, listener: Function, prepend?: boolean): void;
|
||||
|
||||
function off(eventType: string, listener: Function): void;
|
||||
|
||||
function emit(eventType: string, ...args: any[]): boolean;
|
||||
|
||||
function loadFile(name: string): { content: string; debugpath: string };
|
||||
|
||||
function evalScript(name: string): void;
|
||||
|
||||
function require(name: string): any;
|
||||
}
|
||||
|
||||
import puerts = puer;
|
||||
|
||||
declare module "puerts" {
|
||||
export = puerts;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue