Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sp28 · Oct 06, 2013 at 01:09 PM · external filesgame save

Saving informations from game

Hi, hope someone can help me. I have created a game for Webplayer that works so far. For saving things like highscores I've used Playerprefs. But additonally I need to save all kinds of data, e.g. how often the player is game over, how often the player moves left or right,... But this kinds of data I only need to be saved and not loaded when starting the game again. This data should be stored in one file (one plays the game and all data is stored in this file. When someone else plays the game all the data from this game should be stored below the existing data.) Is it possible to save all this information to a csv file and have access to this file

Thanks

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Rabbit · Oct 06, 2013 at 02:16 PM

You can easily save data to a CSV using c#, this is pretty self explanatory :)

 using UnityEngine;
 using System.Collections;
 using System.Text;
 using System.IO;
 
 
 public class Save : MonoBehaviour {
 
     void Start () {
         Savecsv();
     }
     
     void Savecsv() {
         string filePath = @"/Saved_data.csv";  
         string delimiter = ",";  
 
         string[][] output = new string[][]{  
             new string[]{"Col 1 Row 1", "Col 2 Row 1", "Col 3 Row 1"},  
             new string[]{"Col1 Row 2", "Col2 Row 2", "Col3 Row 2"}  
         };  
         int length = output.GetLength(0);  
         StringBuilder sb = new StringBuilder();  
         for (int index = 0; index < length; index++)  
             sb.AppendLine(string.Join(delimiter, output[index]));  
 
         File.WriteAllText(filePath, sb.ToString());                 
     }
 }


Also another approach since your using the webplayer you could keep it on your server using PHP

 <?php
 
     $allowed = @$_GET["key"];
     $data_to_save = $_GET["data"];
     
     if($allowed == "secretkey") {        
         $myFile = "saved_data.txt";
         $fh = fopen($myFile, 'w') or die("can't open file");
         fwrite($fh,$data_to_save);
         fclose($fh);
     } else {
         return;
     }
 ?>





Peace, Love, Happiness. Wazza.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sp28 · Oct 06, 2013 at 03:49 PM 0
Share

is this also possible in javascript? and the script below gives me an error: System.IO.File' does not contain a definition for `WriteAllText'

avatar image tw1st3d · Oct 06, 2013 at 05:14 PM 0
Share

To avoid mistakes with PHP (since it's clunky and likes to break for no reason sometimes) you should add in a check if the file exists, and if it does, unset($file) and then write to $file.

avatar image Rabbit · Oct 06, 2013 at 05:37 PM 0
Share

You forgot to put

 using System.Text;
 using System.IO;

at the top of the c# script

avatar image sp28 · Oct 06, 2013 at 05:45 PM 0
Share

using System.Text; using System.IO; is at the top of the script an i always get the error error CS0117: System.IO.File' does not contain a definition for WriteAllText'

avatar image tw1st3d · Oct 06, 2013 at 06:52 PM 0
Share

There is no reason that error should be showing up if you have them included, so you must have done something wrong with copying it over.

Show more comments
avatar image
0

Answer by tw1st3d · Oct 06, 2013 at 04:08 PM

So this is a little something I threw together that creates an un-encrypted file with saved data. Not fully finished, but it reads a line and returns it from the file you specify, so it should be a great starting point.

 using UnityEngine;
 using System.Collections;
 using System;
 using System.IO;
 
 namespace Load_Save
 {
     
     public class saveGame
     {
 
         public String fileName, saveName, Town;
         public int level, health, mana, xp;
         public Hashtable vars = new Hashtable();
         
         public void setFileName(String name)
         {
             fileName = name;
             setSaveName();
         }
         
         public void setSaveName()
         {
             String[] lines = File.ReadAllLines(fileName);
             String[] vars = new String[250];
             int count = 0;
             
             foreach(String i in lines)
             {
                 String[] theVars = i.Split('=');
                 String theData = theVars[0] + " " + theVars[1];
                 
                 vars[count] = theData;
                 count++;
             }
             
             saveName = vars[0].ToString();
                 
         }
         
         public String getSaveName()
         {
             return saveName;    
         }
         
         public int getLevel()
         {
             return level;    
         }
         
     }
     
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Rabbit · Oct 07, 2013 at 04:21 AM

OK webplayer cannot access the clients computer it's sandboxed, your going to have to do it using php -> create a file - append data to it. I have just tested this and works fine, of course you have to add security to the php but this will get you going.

This php script will create a text file named by the data received.

 <?php
     $name_to_save = $_GET["name"];
     $data_to_save = $_GET["data"];
     
     $myFile = $name_to_save . ".txt";
     $fh = fopen($myFile, 'w') or die("can't open file");
     fwrite($fh,$name_to_save . "," . $data_to_save . ",");
     fclose($fh);
 ?>

Now the C# script (sorry i only know c#)

 using UnityEngine;
 using System.Collections;
 
 public class SaveOnline : MonoBehaviour {
 
     public string postDataURL = "http://YOUR URL HERE/index.php?"; //add a ? to your url
      
     public string Playername;
     public string Playerdata;
     
     void Start()
     {
         Playername = "PlayerName";
         Playerdata = "SomeData";
         StartCoroutine(PostData(Playername,Playerdata));
     }
  
     IEnumerator PostData(string name, string data)
     {
         //This connects to a server side php script that will write the data
          string post_url = postDataURL + "name=" + WWW.EscapeURL(name) + "&data=" + data ;
  
         // Post the URL to the site and create a download object to get the result.
         WWW data_post = new WWW(post_url);
         yield return data_post; // Wait until the download is done
  
         if (data_post.error != null)
         {
             print("There was an error saving data: " + data_post.error);
         }
     }
 }


If your having cut and paste problems PM me :) Peace. Love. Happiness. Wazza.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I save a unity web player game for offline play? 3 Answers

Getting file names from a remote directory 1 Answer

Saved Android app files get automatically recovered from nowhere even after re-installing app. 3 Answers

Saving GridBased map Best Practices 0 Answers

Save & Load function for Instantiated objects 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges