#Emiel scene problem
1 messages · Page 1 of 1 (latest)
hi
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelLoad : MonoBehaviour
{
public int iLevelToLoad;
public string sLevelToLoad;
public bool useIntegerToLoadLevel = false;
// Start is called before the first frame update
void Start()
{
SceneManager.LoadScene(PlayerPrefs.GetString("Level"));
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter(Collider other)
{
GameObject collisionGameObject = other.gameObject;
if (collisionGameObject.name == "Player")
{
LoadScene();
}
}
void LoadScene()
{
Debug.Log("skjldf");
if (useIntegerToLoadLevel)
{
SceneManager.LoadScene(iLevelToLoad);
Debug.Log("skjldf");
}
else
{
//if (SceneManager.GetActiveScene().name == "Level2")
//{
PlayerPrefs.SetString("Level", sLevelToLoad);
Debug.Log("skjldf");
SceneManager.LoadScene(PlayerPrefs.GetString("Level"));
PlayerPrefs.Save();
//SceneManager.LoadScene(sLevelToLoad);
}
}
}
this is my code for now
but it doesnt work
you are trying to load a level
and play it for a while
and then load another level
if the level is finished
and you want that what you did in the previous level is saved when i reopen it
yes
that impossible
tho you can do it in the complex
ever heard of a word
"Game data"
np
a game data is a file/folder where the game stores its data as it progresses
i just want when i reopen the game it loads the level after all the levels i have finished
so for example you finish level1
and quit the game
then you reopen it
and you go to level2
no exactly
then?
not start from level1 but from level2
then you will either need to store your player prefs
thats what i did in the code
ever heard of binary data storage
no
player prefs wont work exactly as you want them to
oh
then lets take it to json cuz binary is too advanced
ever heard of json
its a serializable format that can store data in a specific layout and you can parse it to get the required values of a class back
ok
well then lets write a json instead of binary
ok
ok but what is it
a binary formatter will serialize your data which you provide it as input into microsoft specified serialized output
its also binary with a header
ok and what do i have to write in it
currently 3
ohk then
first of all lets write our binary class that can contain that value
so that its your file you always need a verifier
ok
ok
wait a while till i write the code
sure
here
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public class BinaryData : MonoBehaviour
{
public Int64 header = 817272;
public UInt16 level = 0;
public void SaveBinary(BinaryData data)
{
if (data == null)
return;
string saveBinaryPath = Application.persistentDataPath;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, data);
byte[] playerdata = ms.ToArray();
File.WriteAllBytes(saveBinaryPath + "PlayerData.Binary", playerdata);
}
public void LoadBinary(BinaryData retrievedData)
{
string BinaryPath = Application.persistentDataPath;
string fullPath = BinaryPath + "PlayerData.Binary";
byte[] PlayerData = File.ReadAllBytes(fullPath);
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
ms.Write(PlayerData, 0, PlayerData.Length);
retrievedData = (BinaryData)bf.Deserialize(ms);
}
}```
your code
wow
that,s a class from which you can store your binary values and save it to the directory
let me guide on how to use it
or can you figure it out yourself
thanks for doing this
if you want to store more values then you can just add more fields
like add a field named
public string message;
and then save it as well to binary
can you tell me where i have to store the levels
oh ok
ok
your choice
and how can i dynamically render the levels
you mean when you close a level and then reopen you are from where you were standing
?
yes
then it goes to more data
you can add more fields like vector3 for position when the game is closed
ok
and most game has an auto saving feature which means that as you progress the game saves the data
you can keep that as well
that,s the most basic
and where do i have to assign the script
its not a script
its a code
you will have to write a script using this class
that,s a binary class from which you can store your data and retrieve it
oooohhhh
if you dont know how to use it then you can ask in channel
so what does the script have to contain