Nanfeng

Notes on software development, code, and curious ideas

Finding Unreferenced Images in Cocos Creator

Old projects often accumulate images that are no longer referenced. Removing confirmed unused files reduces project size and simplifies asset management.

For a single image, right-click it in Cocos Creator and inspect its UUID references. Also search code for dynamic references before deleting it.

For larger projects, this Node.js script compares image .meta UUIDs with UUIDs found in prefab files:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const fs = require('fs');
const path = require('path');
const projectDir = '/path/to/project';
const assetsDir = path.join(projectDir, 'assets');

function collect(dir, extension, output = []) {
for (const name of fs.readdirSync(dir)) {
const full = path.join(dir, name);
if (fs.statSync(full).isDirectory()) collect(full, extension, output);
else if (extension.some(ext => full.toLowerCase().endsWith(ext))) output.push(full);
}
return output;
}

const images = collect(assetsDir, ['.png', '.jpg', '.jpeg']);
const prefabs = collect(assetsDir, ['.prefab']);
const referenced = new Set();
const uuidPattern = /"__uuid__"\s*:\s*"([a-zA-Z0-9@_-]+)"/g;

for (const prefab of prefabs) {
const content = fs.readFileSync(prefab, 'utf8');
let match;
while ((match = uuidPattern.exec(content))) referenced.add(match[1]);
}

const unused = images.filter(image => {
const meta = JSON.parse(fs.readFileSync(image + '.meta', 'utf8'));
return ![meta.uuid, meta.uuid + '@f9941'].some(uuid => referenced.has(uuid));
});

fs.writeFileSync(path.join(projectDir, 'unreferenced_images.txt'), unused.join('\n'));

Adapt the suffix and UUID format to your Creator version. Scan .scene files as well as prefabs when scenes can reference assets. Treat the output as candidates for manual review—dynamic loading and code-generated paths will not necessarily appear in serialized references.

+