Sharing Variables Between Projects
I want to share/get variables from the other project when it runns, for an example. User opens the game, scene is loaded in the first one, the second game opens and gets variables from the first one(in void Update or IEnumarator). I couldn't find anything on the internet about this.
Now why would you want to do that? I don't even think that's possible through Unity by itself.
Take a look at X$$anonymous$$L serialization or other way to save data to disk.
Unity have an tutorial about serialization: http://unity3d.com/pt/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading?playlist=17117
You can change the path to save your data.
But, why are you trying to do that? I am really curious...
Thanks, but that's what I already have done, but I don't know how to save it somewhere where they both get the data from. For your curiosity sake, I'm gonna make a game where one screen is kind of an UI and the other is 3d micro management.
Answer by brunocoimbra · Feb 03, 2016 at 03:36 AM
Quick method: go under File > Build Settings > Player Settings and change the "Company Name" and "Product Name" of both projects so both have the same values and keep using "Application.persistentDataPath". OR.....
If you want to just keep the data between projects, but not between different projects' builds, you can use that:
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public class PlayerDataExtension { }
public class SaveLoad
{
string editorPath = "C:/idleExtension.dat";
string buildPath = Application.dataPath + "/idleExtension.dat";
public void SaveExtension()
{
#if UNITY_EDITOR
Save(editorPath);
#else
Save(buildPath);
#endif
}
public void LoadExtension()
{
#if UNITY_EDITOR
Load(editorPath);
#else
Load(buildPath);
#endif
}
private void Save(string dataPath)
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(dataPath);
PlayerDataExtension data = new PlayerDataExtension();
bf.Serialize(file, data);
file.Close();
}
private void Load(string dataPath)
{
if (File.Exists(dataPath))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(dataPath, FileMode.Open);
PlayerDataExtension data = (PlayerDataExtension)bf.Deserialize(file);
file.Close();
}
}
}
Else, if you want to keep your data everywhere, then you can define your own persistent path for each platform:
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public class PlayerDataExtension { }
public class SaveLoad
{
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
string folderPath = "C:/MyFolderNameThatUserShouldNotDeleteOrWillLoseItsSave";
#elif UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
string folderPath = "A folder structure relative to windows one.";
#elif UNITY_STANDALONE_LINUX
string folderPath = "I think that you already got that one.";
#endif
string fileName = "/idleExtension.dat";
public void SaveExtension()
{
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(folderPath + fileName);
PlayerDataExtension data = new PlayerDataExtension();
bf.Serialize(file, data);
file.Close();
}
public void LoadExtension()
{
if (Directory.Exists(folderPath) && File.Exists(folderPath + fileName))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(folderPath + fileName, FileMode.Open);
PlayerDataExtension data = (PlayerDataExtension)bf.Deserialize(file);
file.Close();
}
}
}
Answer by phil_me_up · Feb 01, 2016 at 10:09 PM
This depends on what you're trying to do.
If you're talking about having several people work on the same project, then you want to look at some sort of source control
If you're talking about trying to share variables between separate development projects, then you might want to look at using scriptableobjects
If your talking about sharing some sort of save data, then you'll need to save out or serialise your data in some location where both projects can read from
If you're talking about two people sharing variables when playing the same game then you'll need to syncronise the data somehow (some server setup depending on what you want)
One person, yes two completely different projects. I already have Load and Save function (both are being serialised). Thats the problem I couldn't really get how to save them in the same place, with persistent data path they go to the Appdata. And I couldn't really get how to use dataPath. No multiple players atm.
Here is my code from the extension (The second project who will take the data)
public void SaveExtension(){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.dataPath + "/idleExtension.dat");
PlayerDataExtension data = new PlayerDataExtension();
bf.Serialize(file, data);
file.Close();
}
public void LoadExtension(){
if(File.Exists(Application.persistentDataPath + "/idleExtension.dat")){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/idleExtension.dat", File$$anonymous$$ode.Open);
PlayerDataExtension data = (PlayerDataExtension)bf.Deserialize(file);
file.Close();
}
}
Your problem is with "Application.dataPath" and "Application.pesistentDataPath".
If you are using windows, you can try something like:
FileStream file = File.Create("C:/idleExtension.dat");
FileStream file = File.Open("C:/idleExtension.dat", File$$anonymous$$ode.Open);
$$anonymous$$hm, that's a solution, but the problem with this one is that what if the User doesn't install/extract it on C drive. And he's going to see the folder and delete it (deleting the save).
Your answer
Follow this Question
Related Questions
Best way to pick up objects in unity? 2 Answers
How to recreate a puzzle like this. 0 Answers
using UnityEngine.Flash; error 2 Answers
How to catch events from scripted buttons 1 Answer
How to perform the division and multiplication first? 2 Answers