• 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
Question by Chilluminatler · Feb 01, 2016 at 10:00 PM · c#unity 5sharingadvancedprojects

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.

Comment

People who like this

0 Show 3
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 allenallenallen · Feb 01, 2016 at 10:03 PM 1
Share

Now why would you want to do that? I don't even think that's possible through Unity by itself.

avatar image brunocoimbra · Feb 01, 2016 at 10:16 PM 0
Share

Take a look at XML 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...

avatar image Chilluminatler brunocoimbra · Feb 02, 2016 at 02:42 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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

People who like this

2 Show 0 · 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

Answer by phil_me_up · Feb 01, 2016 at 10:09 PM

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)

Comment

People who like this

0 Show 4 · 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 Chilluminatler · Feb 02, 2016 at 02:22 PM 0
Share

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.

avatar image Chilluminatler · Feb 02, 2016 at 03:08 PM 0
Share

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", FileMode.Open);
             PlayerDataExtension data = (PlayerDataExtension)bf.Deserialize(file);
         
             file.Close();
         }
     }


avatar image brunocoimbra Chilluminatler · Feb 02, 2016 at 04:16 PM 0
Share

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", FileMode.Open);
avatar image Chilluminatler brunocoimbra · Feb 02, 2016 at 04:33 PM 0
Share

Mhm, 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).

avatar image

Answer by pankaj_varma · Mar 24, 2019 at 06:48 AM

Share Data between two or more unity apps

https://github.com/PankajVermaGit/CentralDatabase

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

How to create saving slots that can be loaded? 2 Answers

unity 5 car tutorial 3D 1 Answer

I'm having a few issues with (Catmull)Splines that i need help with. 1 Answer

Survival Shooter Error CS0120 1 Answer

How to remove a specific area of a gameobject? 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