Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Daddy_vincent · 5 days ago · save dataloading filesave-to-file

how to have save and load system. With this programing,How to do a save and load system on mobile. With this programing

using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using static System.Math;

[System.Serializable] public class Data { public double trx;

 // Upgrade 1
 public double u1Cost
 {
     get
     {
         return 5 * Pow(1.15, u1Level);
     }
 }
 public double u1Level;
 public float u1Timer;
 public float u1TimeCap;
 public double u1Mult;
 public double u1Power
 {
   get 
     {
      return u1Level * u1Mult;
     }
 }
 public bool u1Auto;

 // Upgrade 2
 public double u2Cost
 {
     get
     {
         return 250 * Pow(1.16, u2Level);
     }
 }
 public double u2Level;
 public float u2Timer;
 public float u2TimeCap;
 public double u2Mult;
 public double u2Power
 {
     get
     {
         return 10 * u2Level * u2Mult;
     }
 }
 public bool u2Auto;

 // Upgrade 3
 public double u3Cost
 {
     get
     {
         return 18750 * Pow(1.17, u3Level);
     }
 }
 public double u3Level;
 public float u3Timer;
 public float u3TimeCap;
 public double u3Mult;
 public double u3Power
 {
     get
     {
         return 150 * u3Level * u3Mult;
     }
 }
 public bool u3Auto;

 // Upgrade 4
 public double u4Cost
 {
     get
     {
         return 1406250 * Pow(1.18, u4Level);
     }
 }
 public double u4Level;
 public float u4Timer;
 public float u4TimeCap;
 public double u4Mult;
 public double u4Power
 {
     get
     {
         return 400 * u4Level * u4Mult;
     }
 }
 public bool u4Auto;

 // Upgrade 5
 public double u5Cost
 {
     get
     {
         return 105468750 * Pow(1.19, u5Level);
     }
 }
 public double u5Level;
 public float u5Timer;
 public float u5TimeCap;
 public double u5Mult;
 public double u5Power
 {
     get
     {
         return 1000 * u5Level * u5Mult;
     }
 }
 public bool u5Auto;

 public Data()
 {
     FullReset();
 }
 
 public void FullReset()
 {
     trx = 5;
     u1TimeCap = 1;
     u1Timer = 0;
     u1Mult = 1;
     trx = 5;
     u2TimeCap = 4;
     u2Timer = 0;
     u2Mult = 1;
     trx = 5;
     u3TimeCap = 15;
     u3Timer = 0;
     u3Mult = 1;
     trx = 5;
     u4TimeCap = 40;
     u4Timer = 0;
     u4Mult = 1;
     trx = 5;
     u5TimeCap = 90;
     u5Timer = 0;
     u5Mult = 1;

 }

}

public class GameController : MonoBehaviour { public Data data; public Text trxText;

 public Text u1TitleText;
 public Text u1CostText;
 public Text u1ProductionText;
 public Text u1PrecentText;
 public Image u1Fill;

 public Text u2TitleText;
 public Text u2CostText;
 public Text u2ProductionText;
 public Text u2PrecentText;
 public Image u2Fill;

 public Text u3TitleText;
 public Text u3CostText;
 public Text u3ProductionText;
 public Text u3PrecentText;
 public Image u3Fill;

 public Text u4TitleText;
 public Text u4CostText;
 public Text u4ProductionText;
 public Text u4PrecentText;
 public Image u4Fill;

 public Text u5TitleText;
 public Text u5CostText;
 public Text u5ProductionText;
 public Text u5PrecentText;
 public Image u5Fill;

 public void RunUs()
 {
     RunU(1, "Knega i bruket", u1TitleText, u1CostText, u1ProductionText, u1PrecentText, u1Fill, data.u1Level, data.u1Cost, data.u1Power, data.u1Mult, ref data.u1Timer, data.u1TimeCap, data.u1Auto);
     RunU(2, "Gymma och bli stark", u2TitleText, u2CostText, u2ProductionText, u2PrecentText, u2Fill, data.u2Level, data.u2Cost, data.u2Power, data.u2Mult, ref data.u2Timer, data.u2TimeCap, data.u2Auto);
     RunU(3, "Misshandla frugan", u3TitleText, u3CostText, u3ProductionText, u3PrecentText, u3Fill, data.u3Level, data.u3Cost, data.u3Power, data.u3Mult, ref data.u3Timer, data.u3TimeCap, data.u3Auto);
     RunU(4, "Vinn runka bulle", u4TitleText, u4CostText, u4ProductionText, u4PrecentText, u4Fill, data.u4Level, data.u4Cost, data.u4Power, data.u4Mult, ref data.u4Timer, data.u4TimeCap, data.u4Auto);
     RunU(5, "Starta Indumate", u5TitleText, u5CostText, u5ProductionText, u5PrecentText, u5Fill, data.u5Level, data.u5Cost, data.u5Power, data.u5Mult, ref data.u5Timer, data.u5TimeCap, data.u5Auto);
 }

 public void RunU(int id, string title, Text titleT, Text costT, Text prod, Text per, Image fill, double level, double cost, double power, double mult, ref float timer, float timeCap, bool auto)
 {

 titleT.text = title + " - " + level;
 costT.text = "UPPGRADERA" + Notation(cost) + "₿";
 prod.text = Notation(power) + "₿";
 per.text = (timer / timeCap * 100).ToString("F0") + "%";
 fill.fillAmount = timer / timeCap;

     if (level != 0)
     if (timer < timeCap) timer += Time.deltaTime * (float)mult;
     else if (auto)
         CollectU(id);
     else
         timer = timeCap;
 }

 public void CollectU(int id)
 {
     switch (id)
     {
         case 1:
             Collect(ref data.u1Timer, data.u1TimeCap, data.u1Power);
             break;

         case 2:
             Collect(ref data.u2Timer, data.u2TimeCap, data.u2Power);
             break;

         case 3:
             Collect(ref data.u3Timer, data.u3TimeCap, data.u3Power);
             break;

         case 4:
             Collect(ref data.u4Timer, data.u4TimeCap, data.u4Power);
             break;

         case 5:
             Collect(ref data.u5Timer, data.u5TimeCap, data.u5Power);
             break;
     }
 }

 public void Collect(ref float timer, float timeCap, double power)
 {
             if (timer >= timeCap)
             {
                 data.trx += power;
                 timer = 0;
             }
 }

 public void BuyU(int id)
 {
     switch(id)  
     {
         case 1:
             Buy(data.u1Cost, ref data.u1Level, ref data.u1Mult, ref data.u1Auto);
             break;
         case 2:
             Buy(data.u2Cost, ref data.u2Level, ref data.u2Mult, ref data.u2Auto);
             break;
         case 3:
             Buy(data.u3Cost, ref data.u3Level, ref data.u3Mult, ref data.u3Auto);
             break;
         case 4:
             Buy(data.u4Cost, ref data.u4Level, ref data.u4Mult, ref data.u4Auto);
             break;
         case 5:
             Buy(data.u5Cost, ref data.u5Level, ref data.u5Mult, ref data.u5Auto);
             break;
     }

 }

 public void Buy(double cost, ref double level, ref double mult, ref bool auto)
 {
     if (data.trx >= cost)
     {
         data.trx -= cost;
         level += 1;
         if (level == 10)
             mult *= 2;
         if (level == 25)
             mult *= 2;
         else if (level % 50 == 0)
             mult *= 2;
         if (level == 100) auto = true;
     }
 }

 public void ButMaxAll()
 {
     BuyMax(1058750, 1.19, ref data.u5Level);
     BuyMax(140250, 1.18, ref data.u4Level);
     BuyMax(18750, 1.17, ref data.u3Level);
     BuyMax(250, 1.16, ref data.u2Level);
     BuyMax(5, 1.15, ref data.u1Level);
 }

 public void BuyMax(double b, double r, ref double k)
 {
     var c = data.trx;
     var n = Floor(Log(c * (r - 1) / (b * Pow(r, k)) + 1, r));

     var cost = b * (Pow(r, k) * (Pow(r, n) - 1) / (r - 1));

     if (data.trx >= cost)
     {
         k += n;
         data.trx -= cost;
     }
 }

 public string Notation(double x)
 {
     String result;
     if (x > 1000)
     {
         var exponent = Floor(Log10(Abs(x)));
         var mantissa = x / Pow(10, exponent);
         result = mantissa.ToString("F2") + "e" + exponent;
     }
     else
         result = x.ToString("F2");
     return result;
 }

 public void Start()
 {
     data.FullReset();
 }

 public void Update() 
 {
     trxText.text = Notation(data.trx) + "";
     RunUs();
 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Deuce2008 · 5 days ago

If what you is a save load system, then I suggest Json. Here's an example script that saves a score:

     private void Start()
     {
         //A class with a bunch of static vars
         if(!gameManager.loaded)
             Load();
         //Checks if the curret high score is greater then the previus high score 
        //and saves it
         if (gameManager.CurHighScore > gameManager.HighScore)
             Save();
     }
 
     void Save()
     {
         //Creates a var of the score class created below
         Score scoreSaved = new Score
         {
             score = gameManager.CurHighScore
         };
 
         //Converts the scoreSaved var created above, to json
         string json = JsonUtility.ToJson(scoreSaved);
 
         //Saves the json to a text file called "saveScore" in the same folder
         File.WriteAllText(Application.dataPath + "/saveScore.txt", json);
         Debug.Log("SAVED");
     }
     void Load()
     { 
         //Tells the gameManager that we have loaded the high score to avoid 
         //bugs
         gameManager.loaded = true;
 
         //Converts the text file to Json
         string json = File.ReadAllText(Application.dataPath + "/saveScore.txt");
 
         //Converts the Json file to a variable of type Score
         Score scoreSaved = JsonUtility.FromJson<Score>(json);
 
         //Updates the gameManager's high scores
         gameManager.HighScore = scoreSaved.score;
         gameManager.CurHighScore = scoreSaved.score;
 
         Debug.Log("LOADED");
     }
 
     //The class that stores score
     //Must add System.Serializable to the class so we can save it
     [System.Serializable]
     public class Score
     {
         public int score;
     }

Make sure you add this to the top of the script so thta the game knows how to read the script

 using System.IO;

And heres the gameManager:

 public static class gameManager
 {
     public static bool loaded = false;
 
     public static int score = 0;
     public static int HighScore = 0;
     public static int CurHighScore = 0;
 }


I hope you can decipher this and that it is of use to you. (:

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

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

How to Save and Load a list of Scriptable Objects from a File? 2 Answers

How do I save and load big procedural generated terrains? 1 Answer

Save and (later) read scriptable objects 1 Answer

How to save an instance of a class(Make a custom component?) from the editor window 0 Answers

Saving or Reading text file works on Mac but not on iOS 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