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?
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?
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}
Score: {1}
Dogtags: {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.