Skip to content

Commit

Permalink
ResourceManager support findResourcesByType (#1700)
Browse files Browse the repository at this point in the history
* feat: ResourceManager support findResourcesByType
  • Loading branch information
GuoLei1990 committed Aug 16, 2023
1 parent a933568 commit ef443a3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 49 deletions.
17 changes: 17 additions & 0 deletions packages/core/src/asset/ResourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ export class ResourceManager {
return (this._assetUrlPool[url] as T) ?? null;
}

/**
* Find the resource by type.
* @param type - Resource type
* @returns - Resource collection
*/
findResourcesByType<T extends EngineObject>(type: new (...args) => T): T[] {
const resources = new Array<T>();
const referResourcePool = this._referResourcePool;
for (const k in referResourcePool) {
const resource = referResourcePool[k];
if (resource instanceof type) {
resources.push(resource);
}
}
return resources;
}

/**
* Get asset url from instanceId.
* @param instanceId - Engine instance id
Expand Down
36 changes: 0 additions & 36 deletions packages/shader-lab/src/parser/tokens/GlslTypes.ts

This file was deleted.

12 changes: 0 additions & 12 deletions packages/shader-lab/src/parser/tokens/value.ts

This file was deleted.

9 changes: 8 additions & 1 deletion tests/src/core/resource/ResourceManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe("ResourceManager", () => {
engine = await WebGLEngine.create({ canvas: document.createElement("canvas") });
engine.run();
});

beforeEach(() => {
engine.sceneManager.activeScene.createRootEntity("root");
});
Expand All @@ -28,4 +28,11 @@ describe("ResourceManager", () => {
expect(getResource).equal(null);
});
});

describe("findResourcesByType", () => {
it("findResourcesByType", () => {
const textures = engine.resourceManager.findResourcesByType(Texture2D);
expect(textures.length).equal(4);
});
});
});

0 comments on commit ef443a3

Please sign in to comment.