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.

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();
        }
    }
}

This depends on what you’re trying to do.

  1. If you’re talking about having several people work on the same project, then you want to look at some sort of source control
  2. If you’re talking about trying to share variables between separate development projects, then you might want to look at using scriptableobjects
  3. 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
  4. 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)

Share Data between two or more unity apps