Search Unity

problem with Auto save script . . . . .

Discussion in 'Scripting' started by sushanta1991, May 18, 2011.

  1. sushanta1991

    sushanta1991

    Joined:
    Apr 3, 2011
    Posts:
    305
    I find this script in Unity wiki. but this script is showing me an error
    error is : the type or namespace name 'Unity Editor' could not be found. are you missing a using directive or an assembly reference.
    this code save the scene in the interval of 1 to 10 minutes

    Code (csharp):
    1.  
    2. [COLOR="blue"]using[/COLOR] UnityEngine;
    3. [COLOR="blue"]using[/COLOR] Unity Editor;
    4. [COLOR="blue"]using[/COLOR] System;
    5.  
    6. [COLOR="blue"]public class[/COLOR] AutoSave : [COLOR="blue"]EditorWindow[/COLOR] {
    7.  
    8.     [COLOR="blue"]private bool[/COLOR] autoSaveScene = [COLOR="blue"]true[/COLOR];
    9.     [COLOR="blue"]private bool[/COLOR] showMessage =[COLOR="blue"] true[/COLOR];
    10.     [COLOR="blue"]private bool[/COLOR] isStarted = [COLOR="blue"]false[/COLOR];
    11.    [COLOR="blue"] private int[/COLOR] intervalScene;
    12.   [COLOR="blue"]  private[/COLOR] DateTime lastSaveTimeScene = DateTime.Now;
    13.    
    14.   [COLOR="blue"]  private string[/COLOR] projectPath = [COLOR="blue"]Application.dataPath[/COLOR];
    15.    [COLOR="blue"] private string[/COLOR] scenePath;
    16.    
    17.     [[COLOR="blue"]MenuItem[/COLOR] ([COLOR="orange"][COLOR="red"]"Window/AutoSave"[/COLOR][/COLOR])]
    18.     [COLOR="blue"]static void[/COLOR] Init () {
    19.         AutoSave saveWindow = (AutoSave)[COLOR="blue"]EditorWindow.GetWindow[/COLOR] ([COLOR="blue"]typeof[/COLOR] (AutoSave));
    20.         saveWindow.[COLOR="blue"]Show[/COLOR]();
    21.     }
    22.    
    23.     [COLOR="blue"]void OnGUI[/COLOR] () {
    24.        [COLOR="blue"] GUILayout.Label [/COLOR]("Info:",[COLOR="blue"] EditorStyles.boldLabel[/COLOR]);
    25.        [COLOR="blue"] EditorGUILayout.LabelField [/COLOR]("Saving to", ""+projectPath);
    26.       [COLOR="blue"]  EditorGUILayout.LabelField[/COLOR] ("Saving scene:", ""+scenePath);
    27.       [COLOR="blue"]  GUILayout.Label[/COLOR] ("Options:",[COLOR="blue"] EditorStyles.boldLabel[/COLOR]);
    28.         autoSaveScene =[COLOR="blue"] EditorGUILayout.BeginToggleGroup[/COLOR] ("Auto save", autoSaveScene);
    29.         intervalScene = [COLOR="blue"]EditorGUILayout.IntSlider[/COLOR] ("Interval (minutes)", intervalScene, 1, 10);
    30.        [COLOR="blue"] if[/COLOR](isStarted) {
    31.             [COLOR="blue"]EditorGUILayout.LabelField[/COLOR] ("Last save:", ""+lastSaveTimeScene);
    32.         }
    33.  [COLOR="#0000ff"]       EditorGUILayout.EndToggleGroup[/COLOR]();
    34.         showMessage =[COLOR="#0000ff"] EditorGUILayout.BeginToggleGroup[/COLOR] ("[COLOR="#ff0000"]Show Message"[/COLOR], showMessage);
    35.        [COLOR="#0000ff"] EditorGUILayout.EndToggleGroup[/COLOR] ();
    36.     }
    37.    
    38.    
    39.    [COLOR="#0000ff"] void Update()[/COLOR]{
    40.         scenePath = [COLOR="#0000ff"]EditorApplication.currentScene[/COLOR];
    41.        [COLOR="#0000ff"] if[/COLOR](autoSaveScene) {
    42.            [COLOR="#0000ff"] if[/COLOR](DateTime.Now.Minute >= (lastSaveTimeScene.Minute+intervalScene) || DateTime.Now.Minute == [COLOR="#00ffff"]59[/COLOR] [B][/B] DateTime.Now.Second == [COLOR="#00ffff"]59[/COLOR]){
    43.                 saveScene();
    44.             }
    45.         } [COLOR="#0000ff"]else[/COLOR] {
    46.             isStarted = [COLOR="#0000ff"]false[/COLOR];
    47.         }
    48.        
    49.     }
    50.    
    51.    [COLOR="#0000ff"] void [/COLOR]saveScene() {
    52.        [COLOR="#0000ff"] EditorApplication[/COLOR].Save(scenePath);
    53.         lastSaveTimeScene = DateTime.Now;
    54.         isStarted = [COLOR="#0000ff"]true[/COLOR];
    55.        [COLOR="#0000ff"] if[/COLOR](showMessage){
    56.            [COLOR="#0000ff"] Debug.Log[/COLOR]([COLOR="#ff0000"]"AutoSave saved: "[/COLOR]+scenePath+[COLOR="#ff0000"]" on "[/COLOR]+lastSaveTimeScene);
    57.         }
    58.         AutoSave repaintSaveWindow = (AutoSave)[COLOR="#0000ff"]EditorWindow.GetWindow[/COLOR] ([COLOR="#0000ff"][B]typeof[/B][/COLOR] (AutoSave));
    59.         repaintSaveWindow.[COLOR="#0000ff"]Repaint[/COLOR]();
    60.     }
    61. }
    62.  
     
    Last edited: May 18, 2011
  2. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    using UnityEditor;

    not

    using Unity Editor;
     
  3. sushanta1991

    sushanta1991

    Joined:
    Apr 3, 2011
    Posts:
    305
    thanks Marrrk but it is showing another error that is : identifier expected
     
  4. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    It'd help if you listed the Line (and even wrote it out as well so no-one needs to count lines).

    Double click the error and post the line it was.
     
  5. sushanta1991

    sushanta1991

    Joined:
    Apr 3, 2011
    Posts:
    305
    @ Ntero i have double clicked on the error identifier expected it's showing the 2nd line using Unity Editor;
    i am not understanding what is the problem with the code.
     
  6. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    Can you post the first 5 lines of the file as it's currently written.

    It should be "using UnityEditor;" And double check there are semi colons on all lines.

    Identifier Expected is primarily an issue with unsual syntax. Missing semi colons, forgotten end brackets, colons replacing semi colons, missing commas etc... So it's probably a super fast fix, but it's tough to decipher without seeing the current code as is.
     
  7. sushanta1991

    sushanta1991

    Joined:
    Apr 3, 2011
    Posts:
    305
    here is first 10 to 11 lines:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using Unity Editor;
    4. using System;
    5.  
    6. public class AutoSave : EditorWindow {
    7.  
    8.     private bool autoSaveScene = true;
    9.     private bool showMessage = true;
    10.     private bool isStarted = false;
    11.     private int intervalScene;
    12.     private DateTime lastSaveTimeScene = DateTime.Now;
    13.    
    14.     private string projectPath = Application.dataPath;
    15.     private string scenePath;
    16.  

    i am editing the first code that i have given in my first post for th current code which i have you can see the whole code there
     
  8. sushanta1991

    sushanta1991

    Joined:
    Apr 3, 2011
    Posts:
    305
    Sorry Sorry Sorry . . . . .
    The script is just need to save in a folder called editor and its just work fine ........
    all the error is coming for not saving the script in editor folder but now the problem is where should i attached the script in the game to auto save it.
     
  9. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    ah yeah,
    I really think Unity needs to do something internally to better manage all the special folders (Streaming Assets, Resources, Plugins, Native Plugins, Editor, Standard Assets).

    Once that code is in the Editor Folder, all you need to do is go to Window/Auto Save at the top bar and dock that somewhere visible (I'm assuming the logic inside works already).
     
  10. sushanta1991

    sushanta1991

    Joined:
    Apr 3, 2011
    Posts:
    305
    I want to ask that what this Auto save script will do? is this script save the editing which i am doing in the unity or it will save the game scene when i will run it.
     
  11. CodeCody

    CodeCody

    Joined:
    Apr 27, 2010
    Posts:
    440
    I believe it save's the scene after every "IntervalScene" amount of time has passed.