Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 quakeman00 · Mar 15, 2012 at 08:47 PM · c#saveloadxmlprofile

SaveLoad Profile c# to xml

welp , i changed the stone system for a bit more gems

here what im trying to do , i just want to bein able to save and load the number of gems of the player , if he have the equipment on , and how mutch shield , his name , and his code

i did read the tutorial about the monsters in xml but the fact im french dont realy help in the translation . i have limited vocabulary and i have no idea who you must use the script to load and save

here what i got so far

the GameSystem.cs


 using UnityEngine;
 using System.Collections;
 using System;
 using System.Text;
 using System.IO;
 using System.Collections.Generic;
 using System.Xml;
 using System.Xml.Serialization;
 
 
 
 
 public class GameSystem : MonoBehaviour
 {
 
 
 
 //--------------------------------------------------------------//
 
 [XmlAttribute("Profile")]
 
 
 public string Name = "Name";
 public int emerald = 0;
 public int sapphire = 0;
 public int ruby = 0;
 public int hellstone = 0;
 public int Token = 0;
 
 public int shields = 0;
 
 public int equips = 0;
 
 public bool god = false;
 
 public bool jetpack = false;
 
 public int code = 0;
 
 
 
 
 //-------------------------------------------------------------//
 
 
 
 
 
 // Use this for initializationname
 void Start ()
 {
 
 
 
 }
 
 // Update is called once per frame
 void Update ()
 {
 
 
 }
 
 
 }
 
 
 __________________________________________________ ____________
 
 and the ProfileContainer.Cs
 
 __________________________________________________ ____________
 
 
 using System.Collections.Generic;
 
 using System.Xml;
 
 using System.Xml.Serialization;
 
 using System.IO;
 
 
 
 [XmlRoot("MonsterCollection")]
 
 public class ProfileContainer
 
 {
 
 [XmlArray("Gems"),XmlArrayItem("Gem")]
 
 public Gem[] Gems;
 
 
 
 public void Save(string path)
 
 {
 
 var serializer = new XmlSerializer(typeof(ProfileContainer));
 
 using(var stream = new FileStream(path, FileMode.Create))
 
 {
 
 serializer.Serialize(stream, this);
 
 }
 
 }
 
 
 
 public static ProfileContainer Load(string path)
 
 {
 
 var serializer = new XmlSerializer(typeof(ProfileContainer));
 
 using(var stream = new FileStream(path, FileMode.Open))
 
 {
 
 return serializer.Deserialize(stream) as ProfileContainer;
 
 }
 
 }
 
 public static ProfileContainer LoadFromText(string text)
 
 {
 
 var serializer = new XmlSerializer(typeof(ProfileContainer));
 
 return serializer.Deserialize(new StringReader(text)) as ProfileContainer;
 
 }
 
 }


i did more than asking for everything like the last question but since here , i know there still problems and i dont know how to save them things and how to use the save function and load function from the container

if someone can just explain me how it work so i understand for when ill be to saving the unlocked level (they will be bool)

Comment
Add comment · Show 7
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 DaveA · Mar 15, 2012 at 09:19 PM 0
Share

Just curious as why you wouldn't use PlayerPrefs for this?

avatar image GameFreak · Mar 15, 2012 at 09:26 PM 0
Share

Exactly Just Use PlayerPrefs

Example: PlayerPrefs.SetInt("Gems", numberOfGems);

and whenever you want them back PlayerPrefs.GetInt("Gems");

avatar image quakeman00 · Mar 15, 2012 at 09:27 PM 0
Share

i have no idea how it work , im not familliar with sending things from script to another and due to my primary language , looking trough huge totorials whitout valid translation of some things just aint helping me.

i kind of need help for that part so i can learn and bein able to do it my self remembering what do what .. for the moment all i have is the core of my game working and the shop and the menu , all i need to do is save , characther selection and level selection + adding models and animations ... its a simple game including only 1 button to play so the codding was kind of simple for the system but now im at the part where my knowledge aint visited yet

avatar image quakeman00 · Mar 15, 2012 at 09:29 PM 0
Share

@GameFreak ... tank you but where do i put this code ... and how i do for the name and for the booleans?

avatar image GameFreak · Mar 15, 2012 at 09:40 PM 0
Share

See its really really simple.

http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html

This is the documentation.

Now when you need to save something. for example you have

private int gems;

//gems are the number of gems so after all gems have been added use

if (gems> PlayerPrefs.GetInt("GE$$anonymous$$")) { PlayerPrefs.SetInt("GE$$anonymous$$", gems);

     PlayerPrefs.Save();
     }

and whenever you need the number of gems use

private var gems1 = PlayerPrefs.GetInt ("GE$$anonymous$$");

So you ll get the number of gems you stored earlier

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Mar 15, 2012 at 09:53 PM

Props to GameFreak. More info: You can do booleans by using this script: http://unifycommunity.com/wiki/index.php?title=BoolPrefs

Names like PlayerPrefs.GetString ("PlayerName");

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
avatar image
0
Wiki

Answer by GameFreak · Mar 15, 2012 at 10:04 PM

Here. See its really really simple.

http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html

This is the documentation.

Now when you need to save something. for example you have

private int gems;

//gems are the number of gems so after all gems have been added use

if (gems> PlayerPrefs.GetInt("GEM")) { PlayerPrefs.SetInt("GEM", gems);

 PlayerPrefs.Save();
 }

and whenever you need the number of gems use

private var gems1 = PlayerPrefs.GetInt ("GEM");

So you ll get the number of gems you stored earlier

About the position. Just place it wherever the gems are? Like exactly if you are using this you wont use xml right? So whenever you want to save gems use SetInt and whenever you want them use getInt

As said above bools can be saved by SetString And GetString

For new script use something like-

var gems= Script1.gems1;

if (something happens)

{

var gems +=1;

}

if (gems> PlayerPrefs.GetInt("GEM")) { PlayerPrefs.SetInt("GEM", gems);

PlayerPrefs.Save();

}

Now in the first scirpt (script1)of the game use void Start () {

private var gems1 = PlayerPrefs.GetInt ("GEM");

}

Comment
Add comment · Show 2 · 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 quakeman00 · Mar 15, 2012 at 11:27 PM 0
Share

sorry to be redunant and anoying but i still got a bunch of errors :/ i know i should understand all that but .. well maybe im just having a too low comprehention of english and programation of variable sending to get this done properly.

could it be posible you give me an exemple using all 3 gems (emeral ruby and sapphire , and for the jetpack and for the name in what script.cs and another one who call them .. if it need another script to call them .. i dont even get that part right too D:

very sorry to bother you to do that mutch for me but i realy need that save and load system that load automaticly at each time the player open the game

avatar image quakeman00 · Mar 16, 2012 at 01:00 AM 0
Share

still not working , after plenty of test ... i have no idea what i do wrong in the first place ,:c

avatar image
0

Answer by quakeman00 · Mar 16, 2012 at 12:00 AM

learned some more .. tank you , but once again lost with some translation and aint working how i would expect ... maybe im just too retarded to understand

can you please make some premade of what i have as variable up there because when try it it gime me errors about variable assignment D:

sorry to be anoying and redundant ... i just need more secificated and almost premade code so i can understand what do what :c

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
avatar image
0

Answer by GameFreak · Mar 16, 2012 at 09:58 AM

Its tough posting it as a comment so I ll post it as a new answer

Script.cs private int ruby = script1.ruby1;

 private int saffire = script1.saffire1;
 
 private int emarald = script1.emerald1;
 
 private int jetpack = script 1.jetpack1;
 
 void OnCollisionEnter(col:Collision)
 {

 If (col.gameObject.tag == "Ruby"){
 
 ruby += 1;
 }
 Else If (col.gameObject.tag == "Saffire") {
 saffire += 1;
 }
 Else If (col.gameObject.tag == "Emerald") {
 
 Emerald += 1;
 
 }
 Else if (col.gameObject.tag == "Jetpack"
 {
 Jetpack += 1;
 }

 void Update ()
 {
 If (ruby != PlayerPrefs.GetInt("Ruby") {
 PlayerPrefs.SetInt ("Ruby",ruby);
 }
   If (ruby != PlayerPrefs.GetInt("Saffire") {
 PlayerPrefs.SetInt ("Saffire",saffire);
 }
   If (ruby != PlayerPrefs.GetInt("Emerald") {
 PlayerPrefs.SetInt ("Emerald",emerald);
 }
   If (ruby != PlayerPrefs.GetInt("Jetpack") {
 PlayerPrefs.SetInt ("Jetpack",jetpack);
 }



Now script1.cs (To be placed wherever the game begins)

 private int ruby1;
 private int saffire1;
 private int emerald1;
 private int jetpack1;

 void Start ()
 {
 

 ruby1 = PlayerPrefs.GetInt ("Ruby"); 
 saffire1 = PlayerPrefs.GetInt ("Saffire");
 emerald1 = PlayerPrefs.GetInt ("Emerald"); 
 jetpack1 = PlayerPrefs.GetInt ("Jetpack");
 }


Untested but should work

Comment
Add comment · Show 2 · 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 quakeman00 · Mar 16, 2012 at 08:31 PM 0
Share

didnt worked at first , had to make all variables static and public

avatar image quakeman00 · Mar 16, 2012 at 08:40 PM 0
Share

also this

http://desmond.imageshack.us/Himg684/scaled.php?server=684&filename=51401904.png&res=medium

and jetpack is a bool D:

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

6 People are following this question.

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

Related Questions

Save and Load data from Local XML file won't work 1 Answer

Save and Load from XML help 1 Answer

Saving Game Problem 1 Answer

Questions about XML save/load 0 Answers

Editing an XML File 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