Unity Scene Auto-Save

Source: Internet
Author: User
Tags datetime

Recently found that unity has an old auto crash bug. The relationship between the hierarchy view game object and the game resource will be lost each time it crashes because the item is not saved. So think of auto save scene.

Originally wanted to write a script like this, but found that Wikipedia has ...

Using Unityengine; Using Unityeditor; Using System;    public class Autosave:editorwindow {       private bool Autosavescene =&nbs P;true;     private BOOL ShowMessage = true;     private BOOL IsStarted = false;     private int intervalscene;      private DateTime lastSaveTimeScene = DateTime.Now;        private String projectpath = Application.datapath;     private string Scenepath;        [menuitem ("Window/autosave")]     static void Init () {         autosave Savewindow = (AutoSave) Editorwindow.getwindow (typeof   (AutoSave));         savewindow.show ();     }        void Ongui () {        guilayout.label ("Info:", Editorstyles.boldlabel);         editorguilayout.labelfield ("Saving to:",  "" +projectPath);         editorguilayout.labelfield ("Saving scene:",  "" +scenePath ");         guilayout.label ("Options:", Editorstyles.boldlabel);         autosavescene = Editorguilayout.begintogglegroup ("Auto Save", Autosavescene);         intervalscene = Editorguilayout.intslider ("Interval (minutes)", Intervalscene, 1, 10);         if (isstarted) {             editorguilayout.labelfield ("Last Save:",  "" +lastsavetimescene);         }          Editorguilayout.endtogglegroup ();  &nbSp;      showmessage = Editorguilayout.begintogglegroup ("Show Message", ShowMessage) ;         editorguilayout.endtogglegroup ();     }           void Update () {         scenepath = Editorapplication.currentscene;         if (autosavescene) {             if (DateTime.Now.Minute >= (lastsavetimescene.minute+intervalscene) | | DateTime.Now.Minute = = && DateTime.Now.Second = =) {                 savescene ();             }          } else {             IsStarted = False         }        }         void Savescene () {        editorapplication.savescene ( Scenepath);         lastsavetimescene = DateTime.Now;         isstarted = true;         if (showmessage) {             debug.log ("AutoSave Saved:" +scenepath+ "on" +lastsavetimescene);         }         autosave Repaintsavewindow = (AutoSave) Editorwindow.getwindow (typeof  (AutoSave));         repaintsavewindow.repaint ();     }}

Because this editing window must be active, you can have it attached to a window such as Project view.

For your convenience you can also save this layout for your next use.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.