private saveData(){
if(sys.isNative){
//create folder and file just working on native platform.
let f = native.fileUtils;
let base = f.getWritablePath();
let folder = base + "folder/subfolder/";
let success = f.createDirectory(folder);
if(success){
f.writeStringToFile("game data", folder + "gamedata");
}
}else{
//web platform not support this way.
}
}
It not work with Editor. it works with “Preview in Simulator”. The writeablepath in my computer is “C:\ProgramData\cocos\editors\Creator\3.8.2\resources\resources\3d\engine\native\simulator\Release”.
Browser not allow create folder and file.If you want save data in browser, you should save them as string. like this.
function saveData() {
let gamedata = {
folder1: {
subfolder1: "gamedata1",
subfolder2: "gamedata2",
},
folder2: {
subfolder3: "gamedata3",
}
}
localStorage.setItem("GAMEDATA", JSON.stringify(gamedata));
}
function readData(){
let gameData;
try {
gameData = JSON.parse(localStorage.getItem("GAMEDATA"));
} catch (error) {
gameData = {} //default data
}
let data1 = gameData["folder1"]["subfolder1"];
}