public string LevelName;
boolBoy.slime2 = true;
i want to put the LevelName where the slime2 is.
public string LevelName;
boolBoy.slime2 = true;
i want to put the LevelName where the slime2 is.
First let me say that I think you are over complicating this.
From what I am gathering here it looks like when you are ready to go to the next level you want to set a bool value that the current level (denoted by slime - slime13) to true and then load the next level.
But you will also want to be able save and load this information to PlayerPrefs so that the information can be retained from one level to another.
First it is more efficient to use an array of bool values instead of 13 bool values (this also makes it easier to expand the number of values needed) like this
public bool[] slimeList;
Take a look at the following class
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.SceneManagement;
public class SlimeMananger : MonoBehaviour
{
//public string weep;
// public int LEVEL;
public bool[] slimeList;
public int minimumArrayLength = 13;
static private int endianDiff1;
static private int endianDiff2;
static private int idx;
static private byte [] byteBlock;
enum ArrayType {atFloat, atInt32, atBool}
// Use this for initialization
void Start()
{
// make sure that the list is at least as large as the number of available scenes
int listLength = ((slimeList.Length > 0) ? slimeList.Length : minimumArrayLength);
if (SceneManager.sceneCountInBuildSettings > listLength)
{
listLength = SceneManager.sceneCountInBuildSettings;
}
slimeList = GetBoolArray("SlimeList", false, listLength);
}
// go to the next available level
// note: all levels must be added to "Scenes in Build" by selecting File\Build Settings in the main menu
public void GoToNextLevel()
{
int sceneIndex = SceneManager.GetActiveScene().buildIndex;
slimeList[sceneIndex] = true; // set the current index to true
SetBoolArray("SlimeList", slimeList); // save the slimeList to PlayerPrefs
if (sceneIndex < (SceneManager.sceneCountInBuildSettings - 1))
{
SceneManager.LoadScene(sceneIndex + 1);
}
}
// save the slimeList when the application closes
private void OnApplicationQuit()
{
SetBoolArray ("SlimeList", slimeList);
}
// saves a bool array to PlayerPrefs
public static bool SetBoolArray (String key, bool[] boolArray)
{
// Make a byte array that's a multiple of 8 in length, plus 5 bytes to store the number of entries as an int32 (+ identifier)
// We have to store the number of entries, since the boolArray length might not be a multiple of 8, so there could be some padded zeroes
var bytes = new byte[(boolArray.Length + 7)/8 + 5];
bytes[0] = System.Convert.ToByte (ArrayType.atBool); // Identifier
var bits = new BitArray(boolArray);
bits.CopyTo (bytes, 5);
Initialize();
ConvertInt32ToBytes (boolArray.Length, bytes); // The number of entries in the boolArray goes in the first 4 bytes
return SaveBytes (key, bytes);
}
// gets a bool array from PlayerPrefs
public static bool[] GetBoolArray (String key, int defaultSize, bool defaultValue)
{
if (PlayerPrefs.HasKey(key))
{
var bytes = System.Convert.FromBase64String (PlayerPrefs.GetString(key));
if (bytes.Length < 5)
{
Debug.LogError ("Corrupt preference file for " + key);
return new bool[0];
}
if ((ArrayType)bytes[0] != ArrayType.atBool)
{
Debug.LogError (key + " is not a boolean array");
return new bool[0];
}
Initialize();
// Make a new bytes array that doesn't include the number of entries + identifier (first 5 bytes) and turn that into a BitArray
var bytes2 = new byte[bytes.Length-5];
System.Array.Copy(bytes, 5, bytes2, 0, bytes2.Length);
var bits = new BitArray(bytes2);
// Get the number of entries from the first 4 bytes after the identifier and resize the BitArray to that length, then convert it to a boolean array
bits.Length = ConvertBytesToInt32 (bytes);
var boolArray = new bool[bits.Count];
bits.CopyTo (boolArray, 0);
// make sure that the array is at least as large as the defaultSize
if (boolArray.Length < defaultSize)
{
var tempBoolArray = new bool[defaultSize];
for(int i = 0; i < defaultSize; i++)
{
if (i < boolArray.Length)
{
tempBoolArray _= boolArray*;*_
}
else
{
tempBoolArray = defaultValue;
}
}
return tempBoolArray;
}
else
{
return boolArray;
}
}
return new bool[0];
}
// gets a bool array from PlayerPrefs
public static bool[] GetBoolArray (String key, bool defaultValue, int defaultSize)
{
if (PlayerPrefs.HasKey(key))
{
return GetBoolArray(key, defaultSize, defaultValue);
}
var boolArray = new bool[defaultSize];
for(int i=0;i<defaultSize;i++)
{
boolArray = defaultValue;
}
return boolArray;
}
private static bool SaveBytes (String key, byte[] bytes)
{
* try*
* {*
* PlayerPrefs.SetString (key, System.Convert.ToBase64String (bytes));*
* }*
* catch*
* {*
* return false;*
* }*
* return true;*
}
private static int ConvertBytesToInt32 (byte[] bytes)
{
ConvertFrom4Bytes (bytes);
return System.BitConverter.ToInt32 (byteBlock, 0);
}
private static void ConvertInt32ToBytes (int i, byte[] bytes)
{
byteBlock = System.BitConverter.GetBytes (i);
ConvertTo4Bytes (bytes);
}
private static void ConvertTo4Bytes (byte[] bytes)
{
bytes[idx ] = byteBlock[ endianDiff1];
bytes[idx+1] = byteBlock[1 + endianDiff2];
bytes[idx+2] = byteBlock[2 - endianDiff2];
bytes[idx+3] = byteBlock[3 - endianDiff1];
idx += 4;
}
private static void ConvertFrom4Bytes (byte[] bytes)
{
byteBlock[ endianDiff1] = bytes[idx ];
byteBlock[1 + endianDiff2] = bytes[idx+1];
byteBlock[2 - endianDiff2] = bytes[idx+2];
byteBlock[3 - endianDiff1] = bytes[idx+3];
idx += 4;
}
private static void Initialize ()
{
if (System.BitConverter.IsLittleEndian)
{
endianDiff1 = 0;
endianDiff2 = 0;
}
else
{
endianDiff1 = 3;
endianDiff2 = 1;
}
if (byteBlock == null)
{
byteBlock = new byte[4];
}
idx = 1;
}
}
You have two public variables
public bool[] slimeList;
public int minimumArrayLength = 13;
And the way that this class is set up slimeList could just as well be a private be cause at runtime it either loads the current list of values from PlayerPrefs or it creates the list of values (all values not loaded from PlayerPrefs are initialized to false). All of this ensures that the list is of the proper size
1. The length must be equal to or greater than the number of scenes in the “Scenes in Build” list - you can find this by selecting File\Build Settings in the main menu.
2. The length must be equal to or greater than the minimumArrayLength public variable set in the inspector.
To use this first create an empty GameObject, call it something meaningful like SlimeManager and attach this class. Now make a prefab out of this newly created object as you will need to add it to every scene.
Loading and saving of values will now automatically happen on game start, changing levels or application quit.
Next in FinishLevel.cs do this (you also need to add this to every scene, except the last one)
using UnityEngine;
using System.Collections;
namespace MoreMountains.CorgiEngine
{
///
/// Add this class to a trigger and it will send your player to the next level ///
public class FinishLevel : ButtonActivated
{
public SlimeMananger slimeMananger;
public void Start()
{
// precaution
if (slimeMananger == null)
{
Debug.LogError(“slimeMananger not set in the inspector. Please set and try again.”);
// attempting to find SlimeMananger
if ((GameObject.Find(“SlimeMananger”) != null) && (GameObject.Find(“SlimeMananger”).GetComponent() != null))
{
slimeMananger = GameObject.Find(“SlimeMananger”).GetComponent();
}
}
}
/// /// When the button is pressed we start the dialogue ///
public override void TriggerButtonAction()
{
if (slimeMananger != null)
{
slimeMananger.GoToNextLevel();
}
}
}
}
Now TriggerButtonAction() will function as you like.
1. It will set the proper index in the slimeList to true.
2. It will save the slimeList to PlayerPrefs
3. The next level will be loaded
4. Upon loading the next level the slimeList will be loaded from PlayerPrefs