unity-find-fault
Version:
A tool to find fault in unity project.
57 lines • 2.41 kB
JavaScript
import fs from "fs-extra";
import fg from "fast-glob";
import path from "path";
import { UnityHelper } from "./UnityHelper.js";
export class RmButton {
async replaceWithUIButtonScale(option) {
// 先读取Button.cs的guid
const buttonCS = path.join(option.projectRoot, 'Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs.meta');
const buttonGUID = await UnityHelper.readGUID(buttonCS);
const prefabRoot = path.join(option.projectRoot, 'Assets');
const prefabs = await fg('**/*.prefab', { cwd: prefabRoot });
let modifiedCnt = 0;
for (const prefab of prefabs) {
const f = path.join(prefabRoot, prefab);
const content = await fs.readFile(f, 'utf-8');
const lines = content.split(/\r?\n/);
let section = { start: 0, end: 0, fileID: '' };
const sections = [section];
for (let i = 0, len = lines.length; i < len; i++) {
const mch = lines[i].match(/--- !u!\d+ &(\d+)/);
if (mch != null) {
section = { start: i, end: 0, fileID: mch[1] };
sections.push(section);
}
else {
section.end = i;
if (lines[i].match(new RegExp(`m_Script: \\{fileID: \\d+, guid: ${buttonGUID}, type: 3\\}`))) {
section.remove = true;
}
}
}
let modified = false;
for (let i = sections.length - 1; i >= 0; i--) {
const section = sections[i];
if (section.remove) {
modified = true;
lines.splice(section.start, section.end - section.start + 1);
}
}
for (const section of sections) {
if (section.remove) {
for (let i = lines.length - 1; i >= 0; i--) {
if (lines[i] == ` - component: {fileID: ${section.fileID}}`) {
lines.splice(i, 1);
}
}
}
}
if (modified) {
modifiedCnt++;
await fs.writeFile(f, lines.join('\n'), 'utf-8');
}
}
console.log(`${modifiedCnt} prefab modified`);
}
}
//# sourceMappingURL=RmButton.js.map