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 Conect11 · Sep 12, 2013 at 04:45 PM · guiloadingxml

Placing a pre existing script into a pause menu

EDIT: Sometimes it just pays to know your limits. I had hoped to use the following code via my main menu, but that's not working out, so I'm hoping that someone might point me in the right direction for a much simpler question. The following js contains options to both save and load. Works great. Right now, I have it attached to my main camera. (Also had it attached directly to my player) Obviously, with it attached to the camera it is on the screen constantly, which I don't necessarily want. Am thinking of either attaching it to a pause menu, such as the one found here, or making the gui included my pause menu, but don't quite know how to do that. (strange, making a pause menu from scratch seems easy enough from the tutorial, but I don't know enough yet to incorporate what I've already found into a menu. Any help, as always, is appreciated. God bless.

 import System;
 import System.Collections;
 import System.Xml;
 import System.Xml.Serialization;
 import System.IO;
 import System.Text;
 
 
 
 class DemoData
 {
     var x : float;
     var y : float;
     var z : float;
     var name : String;
     var experience : float;
 }
 
 class UserData
 {
     
    public var _iUser : DemoData = new DemoData();
  
    function UserData() { }
 }
 
 //var target: Transform;
 
 //var generalWidth = 50;
 //var userHeight = 50;
 
 //var playerHealth = 1000;
 //var MaxPlayerHealth = 1000;
 //var playerMana = 1000;
 //var playerEnergy = 100;
 //public var playerDamage = 10;
 
 
 //var playerCurrency = 5000;
 
 var playerPositionX = 0;
 var playerPositionY = 0;
 var playerPositionZ = 0;
 
 //var damageNpc = 1;
 
 
 
 
 //Variable For Experience *******************************************************
 public static var curExperience = 1;
 var ExpBarEmtey = 0;
 var ExpBarCur : float;
 var screenHeight : float;
 static var ExpPart = 25; 
 var curLevel = 1;
 var LvMax = 0;
 
 var Lv0 = 12;
 var Lv1 = 18;
 var Lv2 = 28;
 var Lv3 = 42;
 var Lv4 = 78;
 var Lv5 = 156;
 var Lv6 = 330;
 var Lv7 = 800;
 var Lv8 = 1760;
 var Lv9 = 4110;
 var Lv10 = 8670;
 
 //EndofExpVariables *************************************************************
 
 //Saving Variables **************************************************************
 private var _Save : Rect;
 private var _Load : Rect;
 
 private var _SaveMSG : Rect;
 private var _LoadMSG : Rect;
 
 //var _ShouldSave : boolean;
 //var _ShouldLoad : boolean;
 //var _SwitchSave : boolean;
 //var _SwitchLoad : boolean;
 private var _FileLocation : String;
 private var _FileName : String = "SaveData.xml";
 
 //public GameObject player;
 var player : GameObject;
 var playerName : String = "Default";
 
 var MyStyle : GUIStyle;
 
 private var myData : UserData;
 private var _data : String;
 
 private var VPosition : Vector3;
 //EndOfSaving Variables *********************************************************
 
 
 function Awake () { 
       // GUI for the Save/Load
       _Save=new Rect(50,180,100,20);
       _Load=new Rect(50,210,100,20);
       _Name=new Rect(50,240,200,20);
       _SaveMSG=new Rect(50,120,200,40);
       _LoadMSG=new Rect(50,140,200,40);
       _NameMSG=new Rect(50,180,200,40);
        
       // XML Save/Load path
       _FileLocation=Application.dataPath;
       
           
       // Creating something to store information into.
      myData=new UserData();
     }
 
 function OnGUI ()
 {
 
 //    playerName = GUI.TextArea (Rect(generalWidth,userHeight,100,25),playerName);
     
     //Health ********************************************************************
 //    GUI.Box (Rect (50,75,150,25), "Health:" + playerHealth);
     //EndOfHealth ***************************************************************
     
     //Mana **********************************************************************
 //    GUI.Box (Rect (50,100,150,25), "Mana:" + playerMana);
     //EndOfMana *****************************************************************
     
     //Energy ********************************************************************
 //    GUI.Box (Rect (50,125,150,25), "Energy:" + playerEnergy);
     //EndOfEnergy ***************************************************************
     
     //Currency ******************************************************************
 //    GUI.Box (Rect (50,150,150,25), "Gold:" + playerCurrency);
     //EndOfCurrency *************************************************************
     
     //Experience ****************************************************************
     GUI.Box (Rect (10,10,60,20),"Level:" + curLevel);
     //EndOfExperience ***********************************************************
     
 
     //LoadingPlayers ************************************************************      
     if (GUI.Button(_Load,"Load")) {
       
       GUI.Label(_LoadMSG,"Loading from: "+_FileLocation);
       // Load our UserData into myData
       LoadXML();
       if(_data.ToString() != "")
       {
          myData = DeserializeObject(_data);
          VPosition=new Vector3(myData._iUser.x,myData._iUser.y,myData._iUser.z);             
          player.transform.position=VPosition;
          curExperience = myData._iUser.experience;
          Debug.Log("Load Successful");
       }
             
    }
    //EndOfLoadingPlayers *******************************************************
    
    //SavingPlayers *************************************************************
    if (GUI.Button(_Save,"Save")) {
             
       GUI.Label(_SaveMSG,"Saving to: "+_FileLocation);
       //Debug.Log("SaveLoadXML: sanity check:"+ player.transform.position.x);
       
       myData._iUser.x = player.transform.position.x;
       myData._iUser.y = player.transform.position.y;
       myData._iUser.z = player.transform.position.z;
       myData._iUser.name = playerName;
       myData._iUser.experience = curExperience;   
         
       // Time to creat our XML!
       _data = SerializeObject(myData);
       // This is the final resulting XML from the serialization process
       CreateXML();
       Debug.Log(_data);
    }
    //EndOfSavingPlayers *********************************************************
 }
 
 function Update ()
 {
 //    if(playerHealth <= 0) {
 //        print(playerName + " has died...");
 //       die();
 //    }
 //    if(playerHealth > MaxPlayerHealth)
 //    {
 //      playerHealth = MaxPlayerHealth;
 //    }
     // Here is the level decider. If the current exp is higher then the number there then you +1 level.
     switch(curLevel){
         case 0:
     if(curExperience <= Lv0){
          curLevel += 0;
          }
         case 1:
         if(curExperience >= Lv1){
         curLevel += 1;
         LvMax = Lv1;
          }
        break;
         case 2:
        if(curExperience >= Lv2){
          curLevel += 1;
          LvMax = Lv2;
 
          }
        break;
        case 3:
        if(curExperience >= Lv3){
          curLevel += 1;
                  LvMax = Lv3;
 
          }    
       break;
        case 4:
         if(curExperience >= Lv4){
          curLevel += 1;
                           LvMax = Lv4;
 
          }
       break;
        case 5:
        if(curExperience >= Lv5){
          curLevel += 1;
                           LvMax = Lv5;
 
          }
        break;
        case 6:
        if(curExperience >= Lv6){
          curLevel += 1;
                           LvMax = Lv6;
 
          }
        break;
        case 7:
         if(curExperience >= Lv7){
         curLevel += 1;
                          LvMax = Lv7;
 
          }
        break;
        case 8:
         if(curExperience >= Lv8){
          curLevel += 1;
                           LvMax = Lv8;
 
         }
       break;
        case 9:
         if(curExperience >= Lv9){
          curLevel += 1;
                           LvMax = Lv9;
 
          }
        break;
        case 10:
         if(curExperience >= Lv10){
          curLevel += 1;
                           LvMax = Lv10;
 
          }
        break;
     }
     
 }
 
 
 
 
 //function die()
 //{
 //    player.transform.position = Vector3(5,0,5); // A vector 3 teleport
 //    playerHealth = 1000;
 //    playerMana = 1000;        // Reseting the vitals or else it will loop the death(HP = 0)
 //    playerEnergy = 100;
 //}
 
 //function OnCollisionEnter( collision : Collision ) {
     
 //   if(target == null && GameObject.FindGameObjectsWithTag("Enemy")){
 //    target = GameObject.FindWithTag("Enemy").transform;
         
 //}
 //}
 
 //function OnTriggerEnter()
 //{
 //     if(target.gameObject.tag == "Enemy")
 //     {
 //         collider.isTrigger = true;
 //         playerHealth -= damageNpc; // Comment this out if you dont wanna take damage.
 //         curExperience += 5; //Exp per interaction with the enemy.
          
 //     }
 //}
 
 //XML Code *****************************************************************
 
 function UTF8ByteArrayToString(characters : byte[] )
 {     
    var encoding : UTF8Encoding  = new UTF8Encoding();
    var constructedString : String  = encoding.GetString(characters);
    return (constructedString);
 }
 
 
 function StringToUTF8ByteArray(pXmlString : String)
 {
    var encoding : UTF8Encoding  = new UTF8Encoding();
    var byteArray : byte[]  = encoding.GetBytes(pXmlString);
    return byteArray;
 }
    
 
 function SerializeObject(pObject : Object)
 {
    var XmlizedString : String  = null;
    var memoryStream : MemoryStream  = new MemoryStream();
    var xs : XmlSerializer = new XmlSerializer(typeof(UserData));
    var xmlTextWriter : XmlTextWriter  = new XmlTextWriter(memoryStream, Encoding.UTF8);
    xs.Serialize(xmlTextWriter, pObject);
    memoryStream = xmlTextWriter.BaseStream; // (MemoryStream)
    XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
    return XmlizedString;
 }
 
 
 function DeserializeObject(pXmlizedString : String)   
 {
    var xs : XmlSerializer  = new XmlSerializer(typeof(UserData));
    var memoryStream : MemoryStream  = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
    var xmlTextWriter : XmlTextWriter  = new XmlTextWriter(memoryStream, Encoding.UTF8);
    return xs.Deserialize(memoryStream);
 }
 
 
 function CreateXML()
 {
    var writer : StreamWriter;
 
    var t : FileInfo = new FileInfo(_FileLocation+"/"+ _FileName);
    if(!t.Exists)
    {
       writer = t.CreateText();
    }
    else
    {
       t.Delete();
       writer = t.CreateText();
    }
    writer.Write(_data);
    writer.Close();
    Debug.Log("File written");
 }
    
 function LoadXML()
 {
    //StreamReader r = File.OpenText(_FileLocation+"\\"+ _FileName);
    var r : StreamReader = File.OpenText(_FileLocation+"/"+ _FileName);
    var _info : String = r.ReadToEnd();
    r.Close();
    _data=_info;
    Debug.Log("File Read");
 }
 
 //EndOfXML Code **************************************************************



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 neogamesproduct · Sep 12, 2013 at 06:14 PM 0
Share

wow you script a lot but .. loading game from xml ... not say it's impossible ... but difficult ... try to see the whydoidoit homepage

avatar image Conect11 · Sep 12, 2013 at 06:19 PM 0
Share

thanks for the reply. The thing is, I have the code to load that file right above. The trick for me, in essence, is to get rid of the gui button and apply the loading feature to my 3d text object on my main menu.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Conect11 · Sep 12, 2013 at 10:36 PM

Ok, while not how I envisioned it at first I did solve this, thank God =)

Essentially what I did was just create a second camera with a new layer, "Pause." I then attached a camera switch script and the above listed code onto this camera. Next step should be to throw a pause menu script onto the new camera so that the player doesn't get killed by any roaming enemies.

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

15 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

Related Questions

disable GUI when loading is done 5 Answers

Application LoadLevel on Input.KeydownEnter TextField 0 Answers

Wierd GUI problem 1 Answer

Load from XML on iPhone 1 Answer

reference variables within my xml file. 0 Answers


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