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 Kamil1064 · Jun 15, 2016 at 05:57 PM · savingloading fileresource.load

Load data made in editor?

Hi, I'm saving some values inside unity editor and only here. But I would like to load them in builded game. Here is full code, it's working only in editor. I think to use "Resource" folder but have problem with that.

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using UnityEngine.UI;
 
 public class testLoadSave : MonoBehaviour {
 
     public GameObject[] obiekty = new GameObject[5];
     public int slot = 1;
     public Text slotTekst;
 
     
     // Update is called once per frame
     void Update () {
 
         for (int i = 0; i < obiekty.Length; i++) {
             if(i > 0)
                 obiekty[i].transform.RotateAround(obiekty[0].transform.position, Vector3.up, 20 * Time.deltaTime * i);
         }
     }
 
     public void load()
     {
         // how to use resource.Load here ???
         BinaryFormatter bf = new BinaryFormatter();
         FileStream filo = File.Open("Assets/Resources/" + "test_" + slot, FileMode.Open);
         playerDatoso datki = (playerDatoso)bf.Deserialize(filo);
         filo.Close ();
 
         for (int i = 0; i < 5; i++) {
             Vector3 posi = new Vector3(0,0,0);
             posi.x = datki.poziX[i];
             posi.y = datki.poziY[i];
             posi.z = datki.poziZ[i];
             obiekty[i].transform.position = posi;
             Quaternion roti = new Quaternion(0,0,0,0);
             roti.w = datki.rotW[i];
             roti.x = datki.rotX[i];
             roti.y = datki.rotY[i];
             roti.z = datki.rotZ[i];
             obiekty[i].transform.rotation = roti;
         }
     }
 
     public void Save()
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream filo = File.Create("Assets/Resources/" + "test_" + slot);
 
         playerDatoso datki = new playerDatoso();
 
         datki.poziX = new float[5];
         datki.poziY = new float[5];
         datki.poziZ = new float[5];
 
         datki.rotW = new float[5];
         datki.rotX = new float[5];
         datki.rotY = new float[5];
         datki.rotZ = new float[5];
 
         for (int i = 0; i < 5; i++) {
             datki.poziX[i] = obiekty[i].transform.position.x;
             datki.poziY[i] = obiekty[i].transform.position.y;
             datki.poziZ[i] = obiekty[i].transform.position.z;
             datki.rotW[i] = obiekty[i].transform.rotation.w;
             datki.rotX[i] = obiekty[i].transform.rotation.x;
             datki.rotY[i] = obiekty[i].transform.rotation.y;
             datki.rotZ[i] = obiekty[i].transform.rotation.z;
         }
 
         bf.Serialize (filo, datki);
         filo.Close ();
 
     }
 
     public void Prawo()
     {
         slot ++;
         Klampuj();
     }
 
     public void Lewo()
     {
         slot--;
         Klampuj();
     }
 
     void Klampuj()
     {
         slot =     Mathf.Clamp(slot, 1, 5);
         slotTekst.text = slot.ToString();
     }
 }
 [Serializable]
 class playerDatoso
 {
     public float[] poziX, poziY, poziZ; 
     public float[] rotW, rotX, rotY, rotZ;
 }
 
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 Glurth · Jun 15, 2016 at 05:58 PM 0
Share

What is the problem or question?

avatar image Kamil1064 Glurth · Jun 15, 2016 at 06:23 PM 0
Share

How in Load function use Resource.load to load in game files I made in editor? it's commented in line 26. Or maybe I'm doing it;s wrong?

avatar image Kamil1064 Glurth · Jun 16, 2016 at 01:41 PM 0
Share

When i have in Load() function line:

   FileStream filo = File.Open(Resources.Load("test_" + slot), File$$anonymous$$ode.Open);

i get errors:

  1. error CS1502: The best overloaded method match for `System.IO.File.Open(string, System.IO.File$$anonymous$$ode)' has some invalid arguments
  2.  Argument `#1' cannot convert `UnityEngine.Object' expression to type `string'






avatar image phxvyper Kamil1064 · Jun 16, 2016 at 08:54 PM 0
Share

The error youre getting is because youre trying to pass a Resource you've loaded into File.Open, which takes a string. You don't need to use File.Open because you're using Resources.Load

Here is the documentation on Resources and Resource.Load:

https://docs.unity3d.com/ScriptReference/Resources.html https://docs.unity3d.com/ScriptReference/Resources.Load.html

Show more comments
avatar image Glurth · Jun 15, 2016 at 07:49 PM 0
Share

Note sure if this helps but...Usually in unity, classes (like playerDatoso), that you want to use as an asset, are derived from the ScriptableObject Class. ScriptableObjects may be created as assets, usually using the AssetDatabase class, which contains functions like CreateAsset. Once the asset is created you can use the same way you would use a $$anonymous$$esh or a prefab, and drag it around into components that use it.

avatar image Kamil1064 Glurth · Jun 15, 2016 at 07:55 PM 0
Share

Can you show little example? It's something new for me.

1 Reply

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

Answer by Kamil1064 · Jun 20, 2016 at 06:22 PM

Ok finally found the solution, offcourse that was to get just the right one line :P

 // so used another variable
 private string streamPath;
 // then set up this in Start() function
 streamPath = Application.streamingAssetsPath;
 // an at last in Load() function
 FileStream filo = File.Open(streamPath + "/test_" + slot + ".txt", FileMode.Open);
 // rest like in my first post

With this I need to create folder StreamingAssets and hold there files I need. Tested in editor, pc and android build.

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 Kamil1064 · Jun 24, 2016 at 04:51 PM 0
Share

It looks like it's not working :( Added coorutine to check and that's what I get:

 IEnumerator Hahaha()
     {
         yield return new WaitForSeconds(3);
         string filaPath = System.IO.Path.Combine(Application.strea$$anonymous$$gAssetsPath, "test_1.txt");
         WWW www = new WWW(filaPath);
         yield return www;
         string result = www.text;
         if(!string.IsNullOrEmpty(result))
         {
             string myPath = "";
             if(File.Exists(Application.strea$$anonymous$$gAssetsPath + "test_1.txt"))
             {
                 myPath = "exist";
                 print("exist");
             }
             else
             {
                 myPath = "not exist";
                 print("not exist"); // get this on android build
             }
 
             jaka.text = myPath;
         }else
             jaka.text = "nothing"; // get this in editor
     }

avatar image Kamil1064 Kamil1064 · Jun 29, 2016 at 07:08 PM 0
Share

Scriptable Object works perfectly

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

46 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

Related Questions

SerializationException: serializationStream supports seeking, but its length is 0 when trying to load a save game 1 Answer

Resources.load loads twice ( onTriggerenter->load-another->destroy-self ) 1 Answer

Saving screenshot to iPhone Camera Roll 3 Answers

PlayerPrefs Saving Player's Position Java Script 2 Answers

Saving and loading seperate data 2 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