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 /
  • Help Room /
avatar image
0
Question by djgaven588 · Dec 16, 2016 at 11:50 PM · vector3quaternionsaveloadsave-to-file

Saving and loading cube data

I am trying to save all of the data of cubes as floats for x,y,z positioning and x,y,z rotation, and the name of the block as a string to save it for later. I am then loading all this data into a vector3 quaternion and a huge if statement line checking what type of block it is and loading it. It doesn't save the x,y,z positioning, and x,y,z rotation right and keeps making a gigantic ball of crap. Any ideas of how I can save each of the variables individually? Btw I am trying to save those variables separately for serialization, not trying to be a noob and fail when I serialize my data into some type of file. Wouldn't want that data exploited!

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

2 Replies

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

Answer by TBruce · Dec 17, 2016 at 12:40 AM

there are several ways to accomplish what you want, but it is best to use a library that will do it all for you. You can use the PlayerPrefsX.cs class written by community user Eric Haines, or you can use the script shown here that has the necessary code taken from PlayerPrefsX.cs (660 lines vs 292 lines).

Here is an example class that easily saves positions and quaternions from a list of transforms that works with either versions of the extended PlayerPrefs code

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class SaveLoad : MonoBehaviour
 {
     public List<Transform> transforms = new List<Transform>();
 
     // this is just an example of saving a position and a quaternion using the PlayerPrefsExt class
     public void SaveTransformData(string positionKey, string quaternionKey)
     {
         if (transforms.count > 0)
         {
             for int i = 0; i < transforms.count; i++)
             {
                 PlayerPrefsExt.SetVector3(positionKey + "_" + i.ToString(), transforms[i].position);
 
                 PlayerPrefsExt.SetQuaternion(quaternionKey + "_" + i.ToString(), transforms[i].rotation);
             }
         }
     }
 
     // this is just an example of loading a position and a quaternion using the PlayerPrefsExt class
     public void LoadTransformData(string positionKey, string quaternionKey)
     {
         if (transforms.count > 0)
         {
             for int i = 0; i < transforms.count; i++)
             {
                 transforms[i].position = PlayerPrefsExt.GetVector3(positionKey + "_" + i.ToString(), new Vector3(0, 0, 0));
 
                 transforms[i].rotation = PlayerPrefsExt.GetQuaternion(quaternionKey + "_" + i.ToString(), Quaternion.Euler(0, 0, 0));
             }
         }
     }
 }

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 djgaven588 · Dec 17, 2016 at 01:58 AM 0
Share

I was using PosX, PosY, and PosZ to save data. I then instantiated the objects based on those saved values. But the real problem seems to be it is saving incorrect values. I.E: Cube is at 1,1,1 with a rotation of 0,0,0 and saves as 0,0.987,1.25 and 90,42,78, any explination of why checking a gameobjects position with (Name of object variable).transform.localPosition.x; and doing it for all axis and rotations wont work?

avatar image djgaven588 · Dec 17, 2016 at 02:18 AM 0
Share

Well, found the issue! When saving rotations don't forget the w argument! Very important you don't forget it or everything gets strange rotation! Thank you for making me double check my code for errors!

avatar image
0

Answer by djgaven588 · Dec 17, 2016 at 12:57 AM

I want to avoid playerprefs at all costs, my experience with them was short and I would not like to use them again. They really lagged my first concept of my game making it stop for 3 second to save a three digit integer.

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

91 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

Related Questions

Is it normal that saving data to file makes game freeze for a long time? 1 Answer

reading and writing files in built project,Files 0 Answers

Help offline progression, works on windows but not on Android. 0 Answers

Avoiding Gimbal Lock with Vector3 EulerAngles and Applying Torque 0 Answers

Rotating 3d person character 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