diff --git a/.gitignore b/.gitignore index 6ec3df2..a69d699 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ /node_modules/ -/Gen/ +/Gen diff --git a/rspack.config.ts b/rspack.config.ts index a9829b3..d065315 100644 --- a/rspack.config.ts +++ b/rspack.config.ts @@ -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"), diff --git a/src/index.ts b/src/index.ts index 43eddc3..5b2b72a 100644 --- a/src/index.ts +++ b/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(); diff --git a/src/loader.ts b/src/loader.ts new file mode 100644 index 0000000..d36f3ca --- /dev/null +++ b/src/loader.ts @@ -0,0 +1,53 @@ +import { $typeof } from "puerts"; +import { Awaitable, GameObject, Resources } from "unityengine"; +export async function loadPrefab

(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((resolve) => { + awaiter.GetAwaiter().OnCompleted(resolve); + }); +} +export type Constructor = { + new(...args: any[]): T; +}; +export type PrefabKeys = Exclude; +export type PrefabChildKeys = Exclude< + keyof UnityPrefabs[K], + number | symbol +>; +export type PrefabComponentKeys< + K extends PrefabKeys, + C extends PrefabChildKeys, +> = Exclude; +export type PrefabComponent< + K extends PrefabKeys, + C extends PrefabChildKeys, + CK extends PrefabComponentKeys, +> = UnityPrefabs[K][C][CK]; +export function getPrefabChild< + P extends PrefabKeys, + C extends PrefabChildKeys

, +>(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

, + B extends PrefabComponent>, +>( + p: GameObject & { PREFAB_KEY: P }, + c: C, + b: Constructor, +) { + const go = c === "" ? p : p.transform.Find(c).gameObject; + return go.GetComponent($typeof(b)) as B; +} diff --git a/src/types/modules.d.ts b/src/types/modules.d.ts index ae2b12d..237487e 100644 --- a/src/types/modules.d.ts +++ b/src/types/modules.d.ts @@ -7,3 +7,6 @@ declare module "unityengine/ui" { declare module "system" { export = CS.System; } +declare module "tmpro" { + export = CS.TMPro; +} diff --git a/src/types/puer.d.ts b/src/types/puer.d.ts new file mode 100644 index 0000000..6c3c09c --- /dev/null +++ b/src/types/puer.d.ts @@ -0,0 +1,47 @@ +declare enum __Puerts_CSharpEnum { } + +declare namespace puer { + function $ref(x?: T): CS.$Ref; + + function $unref(x: CS.$Ref): T; + + function $set(x: CS.$Ref, val: T): void; + + function $promise(x: CS.$Task): Promise; + + function $generic 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; +} +