目录
简介:
通过UUID搜索资源
1.X
2.x
3.x
API
旧版
uuid压缩
uuid解压
新版本
uuid 压缩
uuid 解压
算法:
相关变量声明
uuid生成
uuid验证
uuid压缩
uuid 解压
算法验证
验证前准备与环境
uuidTest.ts.meata
场景中脚本压缩uuid
使用上面算法验证
先压缩uuid
拿到压缩结果在解压
使用cocos自带 API 验证
Editor.Utils.UuidUtils.compressUuid 压缩
Editor.Utils.UuidUtils.decompressUuid 解压
完整代码
简介:
本篇主要记录cocos uuid 相关API,以及uuid的算法实现,并给出基本的实践结果,3.x 可直接输入uuid即可搜索,1.x、2.x特殊需要加上 u:xxxxxxxx-xxxx-xxxx-xxxxxxxx
通过UUID搜索资源
1.X
2.x

3.x
可直接输入uuid并打印,还可以选中资源右键复制并打印uuid、压缩uuid,打印在cocos编辑器界面的控制界面上

API
旧版
uuid压缩
Editor.UuidUtils.compressUuid
uuid解压
Editor.UuidUtils.decompressUuid
新版本
uuid 压缩
Editor.Utils.UuidUtils.compressUuid
uuid 解压
Editor.Utils.UuidUtils.decompressUuid
算法:
相关变量声明
import Uuid from "node-uuid";
let Base64KeyChars ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for (var AsciiTo64 = new Array(128), i = 0; i < 128; ++i) {AsciiTo64[i] = 0;
}
for (i = 0; i < 64; ++i) {AsciiTo64[Base64KeyChars.charCodeAt(i)] = i;
}
let Reg_Dash = /-/g;
let Reg_Uuid = /^[0-9a-fA-F-]{36}$/;
let Reg_NormalizedUuid = /^[0-9a-fA-F]{32}$/;
let Reg_CompressedUuid = /^[0-9a-zA-Z+/]{22,23}$/;
uuid生成
static uuid() {var uuid = Uuid.v4();return UuidUtils.compressUuid(uuid, !0);}
static generateUuid() {return Uuid.v4();}
uuid验证
static isUuid(uuid: string) {return (Reg_CompressedUuid.test(uuid) ||Reg_NormalizedUuid.test(uuid) ||Reg_Uuid.test(uuid));}
uuid压缩
static compressUuid(str: string, bool: boolean): string {if (Reg_Uuid.test(str)) {str = str.replace(Reg_Dash, "");} else if (!Reg_NormalizedUuid.test(str)) {return str;}var s = !0 === bool ? 2 : 5;return UuidUtils.compressHex(str, s);}static compressHex(str: string, nu: number): string {var s,r = str.length;s = void 0 !== nu ? nu : r % 3;for (var t = str.slice(0, s), o = []; s < r; ) {var u = parseInt(str[s], 16),d = parseInt(str[s + 1], 16),n = parseInt(str[s + 2], 16);o.push(Base64KeyChars[(u << 2) | (d >> 2)]),o.push(Base64KeyChars[((3 & d) << 4) | n]),(s += 3);}return t + o.join("");}
uuid 解压
static decompressUuid(uuid: string): string {if (23 === uuid.length) {for (var e = [], s = 5; s < 23; s += 2) {var r = AsciiTo64[uuid.charCodeAt(s)],t = AsciiTo64[uuid.charCodeAt(s + 1)];e.push((r >> 2).toString(16)),e.push((((3 & r) << 2) | (t >> 4)).toString(16)),e.push((15 & t).toString(16));}uuid = uuid.slice(0, 5) + e.join("");} else if (22 === uuid.length) {for (e = [], s = 2; s < 22; s += 2) {(r = AsciiTo64[uuid.charCodeAt(s)]),(t = AsciiTo64[uuid.charCodeAt(s + 1)]);e.push((r >> 2).toString(16)),e.push((((3 & r) << 2) | (t >> 4)).toString(16)),e.push((15 & t).toString(16));}uuid = uuid.slice(0, 2) + e.join("");}
算法验证
验证前准备与环境
验证方式,在cocos编辑器中创建一个脚本uuidTest.ts,把脚本挂载到场景中。从脚本的meta中拿到uuid,从场景中找到脚本的压缩uuid __type__
uuidTest.ts.meata
uuidTest.ts的uuid:3544b289-5268-4630-9adf-aee75ec297ea

场景中脚本压缩uuid
压缩后的:3544bKJUmhGMJrfrudewpfq

使用上面算法验证
-
先压缩uuid
-
拿到压缩结果在解压

使用cocos自带 API 验证
Editor.Utils.UuidUtils.compressUuid 压缩
Editor.Utils.UuidUtils.decompressUuid 解压

结果可以看到与算法得出结果一致,算法可用。
完整代码
import Uuid from "node-uuid";
let Base64KeyChars ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for (var AsciiTo64 = new Array(128), i = 0; i < 128; ++i) {AsciiTo64[i] = 0;
}
for (i = 0; i < 64; ++i) {AsciiTo64[Base64KeyChars.charCodeAt(i)] = i;
}
let Reg_Dash = /-/g;
let Reg_Uuid = /^[0-9a-fA-F-]{36}$/;
let Reg_NormalizedUuid = /^[0-9a-fA-F]{32}$/;
let Reg_CompressedUuid = /^[0-9a-zA-Z+/]{22,23}$/;export default class UuidUtils {static NonUuidMark: ".";static compressUuid(str: string, bool: boolean): string {if (Reg_Uuid.test(str)) {str = str.replace(Reg_Dash, "");} else if (!Reg_NormalizedUuid.test(str)) {return str;}var s = !0 === bool ? 2 : 5;return UuidUtils.compressHex(str, s);}static compressHex(str: string, nu: number): string {var s,r = str.length;s = void 0 !== nu ? nu : r % 3;for (var t = str.slice(0, s), o = []; s < r; ) {var u = parseInt(str[s], 16),d = parseInt(str[s + 1], 16),n = parseInt(str[s + 2], 16);o.push(Base64KeyChars[(u << 2) | (d >> 2)]),o.push(Base64KeyChars[((3 & d) << 4) | n]),(s += 3);}return t + o.join("");}static decompressUuid(uuid: string): string {if (23 === uuid.length) {for (var e = [], s = 5; s < 23; s += 2) {var r = AsciiTo64[uuid.charCodeAt(s)],t = AsciiTo64[uuid.charCodeAt(s + 1)];e.push((r >> 2).toString(16)),e.push((((3 & r) << 2) | (t >> 4)).toString(16)),e.push((15 & t).toString(16));}uuid = uuid.slice(0, 5) + e.join("");} else if (22 === uuid.length) {for (e = [], s = 2; s < 22; s += 2) {(r = AsciiTo64[uuid.charCodeAt(s)]),(t = AsciiTo64[uuid.charCodeAt(s + 1)]);e.push((r >> 2).toString(16)),e.push((((3 & r) << 2) | (t >> 4)).toString(16)),e.push((15 & t).toString(16));}uuid = uuid.slice(0, 2) + e.join("");}return [uuid.slice(0, 8),uuid.slice(8, 12),uuid.slice(12, 16),uuid.slice(16, 20),uuid.slice(20),].join("-");}static isUuid(uuid: string) {return (Reg_CompressedUuid.test(uuid) ||Reg_NormalizedUuid.test(uuid) ||Reg_Uuid.test(uuid));}static uuid() {var uuid = Uuid.v4();return UuidUtils.compressUuid(uuid, !0);}static generateUuid() {return Uuid.v4();}
}
参考资料:
uuid是怎么转__type__的值的 - Creator 2.x - Cocos中文社区
2个uuid是什么对应关系? - Creator 2.x - Cocos中文社区
creater编译后的文件规则是怎样的? - Creator 2.x - Cocos中文社区
《creator插件开发常用API命令列表》备忘录 - 扩展插件 - Cocos中文社区

