- Home /
Save a gameobject as prefab on button press.,Is there a way for the player to create a prefab with a button press?
Is there a way to save a prefab of an object at the press of a UI.Button?
I have a gameObject called gameManager which contains every instanced object on my game. I want to be able to save/load it at button press.
Is there a way to do so or are prefabs only create-able on Unity Editor?,I was thinking of a a save/load system that saves a whole gameObject called "GameManager" which contains everything about the game to a prefab, and then loading it if the player wishes to. Is there a way to let the user save a prefab with the push of a UI.Button?
Do you want to save data even when the game has been quit and restarted?
Answer by reviyee · Apr 04, 2018 at 04:43 PM
Thanks for everyone's time. I managed to select which GameObjects were enabled/disabled by getting every child object on the GameManager GO, saving a string array (1 if neabled, 0 if disabled) (using PlayerPrefs) and then loading the string, passing it to a int array and setting the state of the GO using said array.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Game_Manager : MonoBehaviour {
// Stores a list of every gameObject as child on the GameManager gameObject.
public List<GameObject> childList = new List<GameObject>();
// Used to transform the PlayerPrefs Load string to an array.
private char[] charArray;
// Used together with CharArray to set
public List<int> isEnabledLoad = new List<int>();
void Start()
{
// Adds every child of the gameManager to a gameObject list.
foreach (Transform child in this.transform)
{
childList.Add(child.gameObject);
}
}
public void Save()
{
// Creates a string to store gameObject Active state using PlayerPrefs.
string enabledString = "";
// If the child GO is disabled, concatenate a 1 to 'enabledString', else, concatenate a 0.
foreach (GameObject child in childList)
{
if (child.activeInHierarchy)
enabledString += '1';
else
enabledString += '0';
}
PlayerPrefs.SetString("savestate", enabledString);
PlayerPrefs.Save();
}
public void Load()
{
// Transforms the Save string to a char array.
charArray = PlayerPrefs.GetString("savestate").ToCharArray();
isEnabledLoad.Clear();
// For each char in charArray, parse the value to integer and add it to an Integer List.
foreach(char c in charArray)
{
isEnabledLoad.Add(int.Parse(c.ToString()));
}
// Loops throught the Integer list and if the value is 0, disable the child GO.
for (int i = 0; i < childList.Count; i++)
{
if(isEnabledLoad[i] == 0)
{
childList[i].SetActive(false);
}
}
}
}
Answer by Ginxx009 · Apr 04, 2018 at 02:06 AM
This may help you a bit i guess.
// Save the transform's GameObject as a prefab asset.
PrefabUtility.CreatePrefab(prefabPath, trans.gameObject);
the trans.gameObject
is the object you want to be the prefab
The PrefabUtility class is only available in the editor, so not usable at runtime.
Answer by Laurentino · Apr 04, 2018 at 07:35 AM
What you need to do is:
a) Serialize your game object to be able to save/load it at runtime.
b) Write an editor utility to load your saved game object and make a prefab out of it
It's no easy task but you could probably find some plugins to help you with that.