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 /
avatar image
0
Question by NovaDrox · Jul 28, 2017 at 05:30 AM · scripting problemfilesharingscript reference

1 script, but apply to be applied to many others

I have this basic script that will convert a basic File and turn it into a .txt file and back again. My problem is that in my Save and Load methods, I want to be able to put any file reference into it without having to make several copies of that same script.

 using UnityEngine;
 using System.IO;
 
 [System.Serializable]
 public class SaveLoadSystem
 {

 public string FileLocation;
 public string Filename;

 public void GetPath()
 {
     FileLocation = Path.Combine(Application.dataPath + "/" + FileLocation, Filename + ".txt");
 }

 public void Save(MonoBehaviour Data)
 {
     string DataInfo = JsonUtility.ToJson(Data);
     File.WriteAllText(FileLocation, DataInfo);
 }

 public void Load(ScriptableObject Data)
 {
     string DataInfo = File.ReadAllText(FileLocation);
     JsonUtility.FromJsonOverwrite(DataInfo, Data);
 }
 }

I want to be able to put this into another script and say, save Obj1 file, but not have to put (Obj1 Data) in the save and load methods. I want to make them universal~ So I can say Save Obj2 file from one script and then maybe say save Obj3 file from another one, but not have a separated script for each Obj I want to save.

Comment
Add comment · Show 2
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 ShadyProductions · Jul 28, 2017 at 07:18 AM 0
Share

$$anonymous$$ake everything static?

avatar image NovaDrox ShadyProductions · Jul 28, 2017 at 05:36 PM 0
Share

How would that work?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ShadyProductions · Jul 28, 2017 at 05:40 PM

You can make an extension of monobehaviour

  public static class SaveLoadSystem
 {
 public static string FileLocation; //you'll have to set this inside the save & load
 public static void Save(this MonoBehaviour Data) {
      string DataInfo = JsonUtility.ToJson(Data);
      File.WriteAllText(FileLocation, DataInfo);
 }
 
  public static void Load(this ScriptableObject Data)
  {
      string DataInfo = File.ReadAllText(FileLocation);
      JsonUtility.FromJsonOverwrite(DataInfo, Data);
  }
 }

then you can just do on any MonoBehaviour or ScriptableObject you can do .Save(); or .Load()

You will have to set FileLocation inside the save & load you could probably do the path combine with the Data.name

Comment
Add comment · Show 3 · 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 NovaDrox · Jul 28, 2017 at 11:42 PM 0
Share

I got around to making the changes and I am getting lots of errors~

using UnityEngine; using System.IO;

[System.Serializable] public class SaveLoadSystem { public string FileLocation; public string Filename;

 public string FileGetting;

 public void GetPath()
 {
     FileLocation = Path.Combine(Application.dataPath + "/" + FileLocation, Filename + ".txt");
 }

 //Want to be able to make this get any refernce from any Serializable script
 public void Save(LobbyData Data)
 {
     string DataInfo = JsonUtility.ToJson(Data);
     File.WriteAllText(FileLocation, DataInfo);
 }

 //Want to be able to make this get any refernce from any Serializable script
 public void Load(LobbyData Data)
 {
     string DataInfo = File.ReadAllText(FileLocation);
     JsonUtility.FromJsonOverwrite(DataInfo, Data);
 }

}

and say I have 2 other scirpts being a serailizable, I would need the Save load script to be able to take any kind of script refrence into it. So like

[Serializable] public class Data1 { public float Name } and then call the save function from the save load system.

private void Start() { saveload.Save(Data1) }

and same goes as the other serializable sricpt (ex. Data2) without having to create serveral scripts saying in the Save(Data1 Data) and Load(Data1 Data) methods

avatar image ShadyProductions NovaDrox · Jul 29, 2017 at 11:33 AM 0
Share

I'm not sure what you are trying to do man..

avatar image NovaDrox ShadyProductions · Jul 29, 2017 at 04:17 PM 0
Share

I know and I figured it out on my own time. I forgot all scripts are just Objects and there is a method that called Object that I can put into the method field. So, thank you for your help and efforts ;)

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

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

Adding jump animation in script editor [2D Project] 0 Answers

Can't Create Directory 1 Answer

How can I save data to a text/json file and read it back and then assign the values back to the variables ? 2 Answers

Weird Mac %FileName 0 Answers

Move file from streaming assets to other directory 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