Hi,
Im currently making a 2D weapon customizer where
you can click on a button and then a weapon with random parts will be spawned.
Each Weapon has a stock,barrel,body and grip (I have made multiple of every category).
Now I want to save all Weaponparts in a Database so i can access them from everywhere but I dont know how.
using System.Collections.Generic;
using UnityEngine;
public class CustomizedWeapon
{
public int stock;
public int barrel;
public int body;
public int grip;
}
public class MyScript : MonoBehaviour
{
public GameObject m_stock;
public GameObject m_barrel;
public GameObject m_body;
public GameObject m_grip;
public List<GameObject> stocks = new List<GameObject>();
public List<GameObject> barrels = new List<GameObject>();
public List<GameObject> bodys = new List<GameObject>();
public List<GameObject> grips = new List<GameObject>();
CustomizedWeapon m_customizedWeapon;
// When the player has chosen a randomly generated weapon, you call this method to store the weapon data.
public void SaveCustomizedWeapon(int i_stock, int i_barrel, int i_body, int i_grip)
{
CustomizedWeapon customizedWeapon = new CustomizedWeapon { stock = i_stock, barrel = i_barrel, body = i_body, grip = i_grip };
PlayerPrefs.SetString("customizedWeapon", JsonUtility.ToJson(customizedWeapon));
PlayerPrefs.Save();
}
// When you want to load the customized weapon, you call this method to retrieve the data
public void LoadCustomizedWeapon()
{
m_customizedWeapon = JsonUtility.FromJson<CustomizedWeapon>(PlayerPrefs.GetString("customizedWeapon"));
// Now you can rebuild the weapon from the data
m_stock = stocks[m_customizedWeapon.stock];
m_barrel = barrels[m_customizedWeapon.barrel];
m_body = bodys[m_customizedWeapon.body];
m_grip = grips[m_customizedWeapon.grip];
}
}
You should post your code (of how you generate the weapon) because now it’s kinda hard to help you.
As you can see I populate public GameObject fields according to the data from the CustomizedWeapon class because I do not know what your setup is, but hopefully this can help you, if anything is not clear, I’ll be happy to explain more in-depth after you post your code.
Edit: fixed copy/paste error where m_barrel, m_body and m_grip would all be set from the stocks List.