- Home /
This question was
closed Aug 03, 2017 at 08:47 PM by
FortisVenaliter for the following reason:
Question is off-topic or not relevant. Question is regarding a third-party plugin. Support should be sought from the developers of the plugin.
Save and load prefabs instantiated at runtime
I wanted to make a game for android but in the system of saving and loaded of prefabs in the editor it works but in my mobile it does not work (I also try it in another phone but it does not work) :-( I use Easy Save 2 modified.
using UnityEngine; using System.Collections.Generic; using System.IO; using System.Collections; using System.Text;
/ This class will manage the creation of prefabs, including loading and saving them. It will also store a list of all of the prefabs we've created. / public class CrearMinaAlimento : MonoBehaviour {
// The prefab we want to create.
//public GameObject prefab;
// The name of the file we'll create to save and load our data from.
public string filename = "SavedAlimentoPrefabs.txt";
// The horizontal position of the button for this prefab.
// A List which we'll add any created prefabs to.
private List<GameObject> createdPrefabs = new List<GameObject>();
Ray ray;
RaycastHit hit;
public GameObject mina_alimento;
/*
* This is where we initialize our prefabs.
*/
void Start()
{
Debug.Log (Application.persistentDataPath);
// If there are saved prefabs to load, load them.
if (ES2.Exists(filename))
LoadAllPrefabs();
}
/*
* This method will load all of the saved prefabs when called.
*/
void LoadAllPrefabs()
{
// Load our prefab count so we know how many prefabs to load.
int prefabCount = ES2.Load<int>(filename + "?tag=prefabCount");
// Load each prefab using a for loop.
for (int i = 0; i < prefabCount; i++)
LoadPrefab(i);
}
void Update()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
if (Seleccionador_Edi.seleccionar == 2)
{
if (Oro.oro >= 10)
{
if (hit.collider.tag == "Terrain")
{
GameObject obj2 = Instantiate(mina_alimento, new Vector3(hit.point.x, hit.point.y, hit.point.z), Quaternion.identity) as GameObject;
Oro.oro = Oro.oro - 10;
createdPrefabs.Add(obj2);
PlayerPrefs.SetFloat("alimento", Alimento.alimento);
PlayerPrefs.Save();
}
}
}
}
}
}
/*
* Loads the prefab specified by the tag number
* supplied as a parameter.
*/
void LoadPrefab(int tag)
{
// Create a new instance of the prefab.
GameObject newPrefab = Instantiate(mina_alimento) as GameObject;
// Load the Transform using the auto-assigning method.
// Note: this takes a Transform as the second parameter.
ES2.Load<Transform>(filename + "?tag=" + tag, newPrefab.transform);
// Now add the newly created prefab to our createdPrefabs list.
createdPrefabs.Add(newPrefab);
}
/*
* Instantiates the prefab at a random position and with a random
* rotation.
*/
/*void CreateRandomPrefab()
{
// Create a new prefab at a random position with random rotation.
GameObject newPrefab = Instantiate (prefab, Random.insideUnitSphere*5, Random.rotation) as GameObject;
// Now add the newly created prefab to our createdPrefabs list.
createdPrefabs.Add(newPrefab);
}*/
/*
* This is called whenever the application is quit, and is where we'll
* save our prefabs.
*
* Note:
* We could also use OnDestroy(), which would run when the application
* is quit, *and* when the level is changed.
*/
void OnApplicationQuit()
{
// First, we save the length of the createdPrefabs list so we know how
// many prefabs we need to load when we restart the application.
ES2.Save(createdPrefabs.Count, filename + "?tag=prefabCount");
// Now we iterate through our prefab list and save each one seperately,
// using it's position in the array as the tag.
for (int i = 0; i < createdPrefabs.Count; i++)
SavePrefab(createdPrefabs[i], i);
}
/*
* This is where we save the Variables/Components of our prefab.
*/
void SavePrefab(GameObject prefabToSave, int tag)
{
// Save the Transform of the prefab.
ES2.Save(prefabToSave.transform, filename + "?tag=" + tag);
}
/*
* Creates a button which allows us to delete data or spawn a random prefab.
*/
}
Comment