Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Darioszka · Oct 11, 2015 at 09:42 AM · loaddata

load data from other application

Hi, I have 2 games. In this games gamers can get coins. I want to total/sum up coins from this two games. eg. In first game someone get 10 coins and when open second game there loaded 10 coins.

Can I do this?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Statement · Oct 11, 2015 at 05:26 PM

Yes. Establish a protocol for how the games communicate. It could be a shared file on a computer, a database access on a database server and what not.

A simple local file could be something like:

 using UnityEngine;
 using System.IO;
 
 public class SimpleSharedFile : MonoBehaviour
 {
     void Start()
     {
         string path = Path.Combine(Application.persistentDataPath, "example.txt");
         print("saving to " + path);
 
         Save(path, 232);
         int score = Load(path);
         print(score);
     }
 
     void Save(string path, int score)
     {
         File.WriteAllText(path, score.ToString());
     }
 
     int Load(string path)
     {
         return int.Parse(File.ReadAllText(path));
     }
 }

You could also make a class that represents your shared data that handles load and save more effortlessly from the calling code side of things.

 using UnityEngine;
 using System.IO;
 
 public class SimpleSharedFile : MonoBehaviour
 {
     SharedVariables vars;
 
     void Start()
     {
         vars = new SharedVariables("shared.txt");
 
         vars.Load();
         log();
 
         vars.Coins += 300;
         vars.Score += 1000;
         vars.Dogtags += 10;
 
         vars.Save();
         log();
     }
 
     void log()
     {
         print(string.Format("Coins: {0}\nScore: {1}\nDogtags: {2}",
             vars.Coins,
             vars.Score,
             vars.Dogtags));
     }
 }
 
 public class SharedVariables
 {
     // Change version number as you change the file format.
     // Otherwise you could end up getting errors loading file
     // version 0.0 with code having rules for 4.2 etc. 
     public const string Version = "0.0";
 
     // Variables that are loaded/saved.
     #region The actual shared variables
     public int Coins;
     public int Score;
     public int Dogtags;
     #endregion
 
     public readonly string Path;
 
     public SharedVariables(string filename)
     {
         Path = System.IO.Path.Combine(Application.persistentDataPath, filename);
     }
 
     public void Save()
     {
         using (var stream = File.Create(Path))
         using (var writer = new StreamWriter(stream))
         {
             WriteHeader(writer);
 
             // Write variables here.
             writer.WriteLine(Coins);
             writer.WriteLine(Score);
             writer.WriteLine(Dogtags);
         }
     }
 
     public void Load()
     {
         if (!File.Exists(Path))
             return;
 
         using (var stream = File.OpenRead(Path))
         using (var reader = new StreamReader(stream))
         {
             ReadHeader(reader);
 
             // Read variables here.
             Coins = ReadInteger(reader);
             Score = ReadInteger(reader);
             Dogtags = ReadInteger(reader);
         }
     }
 
     private void WriteHeader(StreamWriter writer)
     {
         writer.WriteLine(Version);
     }
 
     private void ReadHeader(StreamReader reader)
     {
         string version = reader.ReadLine();
         if (version != Version)
             throw new System.Exception("Unsupported version: " + version);
     }
 
     private static int ReadInteger(StreamReader reader)
     {
         return int.Parse(reader.ReadLine());
     }
 }

It's very basic, not very flexible, but can be a stepping stone to toy with ideas.

Comment
Add comment · Show 2 · 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 Statement · Oct 11, 2015 at 05:37 PM 0
Share

In hindsight, creating the path with Application.persistentDataPath is likely not a good idea since the path will be different for different games.

avatar image Darioszka Statement · Oct 11, 2015 at 05:53 PM 0
Share

O$$anonymous$$ thank you a lot I will try second method

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

31 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 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

Loading data from File using TextAsset. Unable to output ÖÄÅ chars,Loading data from File using TextAsset, but unable ouput ÖÄÅ chars to UI.text 0 Answers

Save and Load data from a Scriptableobject 0 Answers

Save game - Tile based game 0 Answers

Two Player character select screen help please 0 Answers

Loading contents of a folder as AssetBundle 1 Answer


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