unity3d - Unity 5: Can't implement animation saving (to file) -
i'm making project, contains player, terrain, directional light (sun) , misc mesh objects bridge, tent, etc. have implemented saving system, stores player's position in file. didn't store sun's animation (day, dusk, night , morning implemented using keyframe animation). have added animation saver main (player pos) saver , animation loader main loader (triggered on level load). launches dusk (sundown) animation. here's saver code:
#pragma strict import system; import system.io; var filename : string = "save.ini"; private var rwfile : streamwriter; private var x : float; private var y : float; private var z : float; private var dusk : boolean; private var night : boolean; private var morning : boolean; private var day : boolean; private var time : int; var player : gameobject; var sunanimator : animator; private var starttime : int; private var timer1 : int; function timerset() {starttime = time.time;} function timerupdate() { timer1 = time.time - starttime; if(timer1 > 60) { debug.log("saving"); write(); debug.log("saved"); timerset();}} function writersetup() { debug.log("file writer setup..."); if (!file.exists(filename)) { rwfile = file.createtext(filename);} debug.log("file writer configured sucsessfully");} function write() { rwfile = file.createtext(filename); x = player.transform.position.x; y = player.transform.position.y; z = player.transform.position.z; dusk = sunanimator.getbool("sundown"); night = sunanimator.getbool("night"); morning = sunanimator.getbool("sunrise"); day = sunanimator.getbool("day"); rwfile.writeline(x); rwfile.writeline(y); rwfile.writeline(z); if (dusk) {time = 0;} if (night) {time = 1;} if (morning) {time = 2;} if (day) {time = 3;} rwfile.writeline(time); rwfile.close(); }
here's loader code:
#pragma strict import system; import system.io; private var data = [1853.49, 104.636, 1730.0, 0]; var filename = "save.ini"; var player : gameobject; var sunanimator : animator; function start() { if(file.exists(filename)){ var sr = file.opentext(filename); data[0] = parsefloat(sr.readline()); data[1] = parsefloat(sr.readline()); data[2] = parsefloat(sr.readline()); data[3] = parseint(sr.readline()); sr.close(); } player.transform.position = vector3(data[0], data[1], data[2]); switch (data[3]) { case 0: sunanimator.setbool("sundown", true); sunanimator.setbool("night", false); sunanimator.setbool("sunrise", false); sunanimator.setbool("day", false); break; case 1: sunanimator.setbool("sundown", false); sunanimator.setbool("night", true); sunanimator.setbool("sunrise", false); sunanimator.setbool("day", false); break; case 2: sunanimator.setbool("sundown", false); sunanimator.setbool("night", false); sunanimator.setbool("sunrise", true); sunanimator.setbool("day", false); break; case 3: sunanimator.setbool("sundown", false); sunanimator.setbool("night", false); sunanimator.setbool("sunrise", false); sunanimator.setbool("day", true); break; } } function update () {}
and here's animator screenshot: screenshot
Comments
Post a Comment