First off, this asset is amazing. Thank you for your great work.
Serialization sucks, especially in Unity so this is great.
What I'm trying to do is load tiles from a save file, however the reference to the sprite gets lost on load.
Here is my SaveSystem script:
Code: Select all
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.Tilemaps;
public class SaveSystem : MonoBehaviour
{
public Player player;
public Transform playerTransform;
public Crop[] crops;
public Item[] inventoryItems;
public Inventory inventory;
public TilemapRenderer floorTilemapRenderer;
public Tilemap floorTilemap;
public PlayerBalance pBalance;
public Sprite femaleSprite;
void OnApplicationPause(bool pauseStatus)
{
if (pauseStatus)
Save();
}
private void Awake()
{
//Load player name and gender even if we have never saved.
player.name = ES3.Load<string>("PlayerName");
bool gender = ES3.Load<bool>("Gender");
if (gender)
{
//if true female character sprites should be loaded
player._animator.runtimeAnimatorController = player.femaleAnimator;
player.gameObject.GetComponent<SpriteRenderer>().sprite = femaleSprite;
}
if (ES3.KeyExists("PlayerPosistion"))
{
floorTilemap = ES3.Load<Tilemap>("FloorTilemap");
BoundsInt bounds = ES3.Load<BoundsInt>("Bounds");
TileBase[] allTiles = ES3.Load<TileBase[]>("AllTiles");
floorTilemap.SetTilesBlock(bounds, allTiles);
inventory.hoeEquipped = ES3.Load<Item>("HoeEquipped");
inventory.shovelEquipped = ES3.Load<Item>("ShovelEquipped");
List<Item> tempArray = ES3.Load<List<Item>>("Inventory");
inventory.items.Clear();
foreach(Item item in tempArray)
{
inventory.items.Add(item);
}
ES3.LoadInto("PlayerPosistion", playerTransform);
//Load all crop data
for(int i = 0; i < crops.Length; i++)
{
string cropName = crops[i].cropName;
crops[i].isEmpty = ES3.Load<bool>("crop" + i + "isEmpty");
crops[i].readyToHarvest = ES3.Load<bool>("crop" + i + "readyToHarvest");
crops[i].growthTime = ES3.Load<float>("crop" + i + "growthTime");
crops[i].interval = ES3.Load<float>("crop" + i + "interval");
crops[i].spriteIndex = ES3.Load<int>("crop" + i + "spriteIndex");
crops[i].cropName = ES3.Load<string>("crop" + i + "cropName");
crops[i].emptySlot = ES3.Load<UnityEngine.Sprite>("crop" + i + "emptySlot");
crops[i].spriteRenderer = ES3.Load<UnityEngine.SpriteRenderer>("crop" + i + "spriteRenderer");
crops[i].animator = ES3.Load<Animator>("crop" + i + "animator");
crops[i].playerAnimator = ES3.Load<Animator>("crop" + i + "playerAnimator");
crops[i].inventory = ES3.Load<Inventory>("crop" + i + "inventory");
crops[i].isInsideCrop = ES3.Load<bool>("crop" + i + "isInsideCrop");
crops[i].counter = ES3.Load<float>("crop" + i + "counter");
crops[i].spriteAtlas = ES3.Load<Sprite[]>("crop" + i + "spriteAtlas");
switch (cropName)
{
case "Wheat":
crops[i].wheatAtlas.CopyTo(crops[i].spriteAtlas, 0);
break;
case "Red Apple":
crops[i].redAppleTreeAtlas.CopyTo(crops[i].spriteAtlas, 0);
break;
case "Blue Apple":
crops[i].blueAppleTreeAtlas.CopyTo(crops[i].spriteAtlas, 0);
break;
case "Turnip":
crops[i].turnipAtlas.CopyTo(crops[i].spriteAtlas, 0);
break;
case "Orange":
crops[i].orangeTreeAtlas.CopyTo(crops[i].spriteAtlas, 0);
break;
case "Green Apple":
crops[i].greenAppleTreeAtlas.CopyTo(crops[i].spriteAtlas, 0);
break;
}
}
pBalance.gold = Int32.Parse(ES3.Load("Gold").ToString());
}
}
private void Start()
{
InvokeRepeating("Save", 180f, 180f);
}
public void Save()
{
try
{
BoundsInt bounds = floorTilemap.cellBounds;
TileBase[] allTiles = floorTilemap.GetTilesBlock(bounds);
//Get tiles in tilemap and then save them
ES3.Save("AllTiles", allTiles);
ES3.Save("Bounds", bounds);
ES3.Save("FloorTilemap", floorTilemap);
ES3.Save("FloorTilemapRenderer", floorTilemapRenderer);
ES3.Save("HoeEquipped", inventory.hoeEquipped);
ES3.Save("ShovelEquipped", inventory.shovelEquipped);
ES3.Save("PlayerPosistion", playerTransform);
ES3.Save("Gold", pBalance.gold);
ES3.Save("Inventory", inventory.items);
//Save all crop data
if (crops.Length != 0) {
for (int i = 0; i < crops.Length; i++)
{
ES3.Save("crop" + i + "isEmpty", crops[i].isEmpty);
ES3.Save("crop" + i + "readyToHarvest", crops[i].readyToHarvest);
ES3.Save("crop" + i + "growthTime", crops[i].growthTime);
ES3.Save("crop" + i + "interval", crops[i].interval);
ES3.Save("crop" + i + "spriteIndex", crops[i].spriteIndex);
ES3.Save("crop" + i + "cropName", crops[i].cropName);
ES3.Save("crop" + i + "emptySlot", crops[i].emptySlot);
ES3.Save("crop" + i + "spriteAtlas", crops[i].spriteAtlas);
ES3.Save("crop" + i + "spriteRenderer", crops[i].spriteRenderer);
ES3.Save("crop" + i + "animator", crops[i].animator);
ES3.Save("crop" + i + "playerAnimator", crops[i].playerAnimator);
ES3.Save("crop" + i + "inventory", crops[i].inventory);
ES3.Save("crop" + i + "isInsideCrop", crops[i].isInsideCrop);
ES3.Save("crop" + i + "counter", crops[i].counter);
}
}
Debug.Log("Successfully saved game data!");
}
catch(Exception ex)
{
Debug.Log("Failed to save game data!\n"+ex);
}
}
}
andReference for UnityEngine.Sprite with ID 4735998202259677281 could not be found in Easy Save's reference manager
Reference for UnityEngine.Texture2D with ID 1871183610814004230 could not be found in Easy Save's reference manager.
Thank you for your time!