- Home /
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;
}
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?
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'
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
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.
Can you show little example? It's something new for me.
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.
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
}