I'm having trouble with sprite references saving correctly. I know this was an added feature, so trying to get to the bottom of why this isn't working for me.
Version of ES3: 3.3.2f7
I'm utilizing a Type to save sprites, and I'm guessing the issue I'm having is becuase the sprite is fairly buried. It's saving every other aspect of the Technology, except for the sprite.
Here are the various bits of code:
TechDatabase.cs
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PixelCrushers;
public class TechDatabase : MonoBehaviour
{
public List<TechCategory> techCategories = new List<TechCategory>();
void Awake()
{
SaveSystem.saveDataApplied += OnSaveDataApplied;
EventHandler.ScenePortalTriggered += SaveBeforeChangingScenes;
}
public void OnSaveDataApplied()
{
if (!ManualSave.manuallyLoadingGame)
{
if (ES3.KeyExists("techdatabase"))
{
techCategories = ES3.Load<List<TechCategory>>("techdatabase");
}
}
}
void SaveBeforeChangingScenes()
{
ES3.Save("techdatabase", techCategories);
}
public void OnApplicationQuit()
{
ES3.Save("techdatabase", techCategories);
}
private void OnDestroy()
{
SaveSystem.saveDataApplied -= OnSaveDataApplied;
EventHandler.ScenePortalTriggered -= SaveBeforeChangingScenes;
}
}
[System.Serializable]
public class TechGroup : IEnumerable<Technology>
{
public List<Technology> technologies = new List<Technology>();
public IEnumerator<Technology> GetEnumerator()
{
return technologies.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
TechCategory.cs
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class TechCategory : IEnumerable<TechGroup>
{
public string categoryName;
public List<TechGroup> techGroups = new List<TechGroup>();
public IEnumerator<TechGroup> GetEnumerator()
{
return techGroups.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
Code: Select all
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
[System.Serializable]
public class Technology
{
public enum AvailabilityState
{
Locked,
Unlocked,
Researching,
Learned
}
public AvailabilityState availabilityState;
public string techName;
public string techDescription;
public Sprite techImage;
public List<ResourceAmount> resourceCosts;
public List<RequiredTech> techRequirements;
public List<TechModifiers> techModifiers;
[SerializeField]
private int researchedTurns;
public int requiredResearchTurns;
public Color techUIColor;
public Technology()
{
if(availabilityState == AvailabilityState.Locked)
{
TechEvents.OnTechResearchCompleted += CheckRequirements;
}
}
public void Unlock()
{
availabilityState = AvailabilityState.Unlocked;
TechEvents.TechUnlocked(this);
TechEvents.OnTechResearchCompleted -= CheckRequirements;
Debug.Log("Tech was unlocked: " + this.techName);
}
public void Learn()
{
TechEvents.TechResearchCompleted(this);
TechEvents.OnTurnPassed -= NewTurn;
availabilityState = AvailabilityState.Learned;
Debug.Log("Tech was learned: " + this.techName);
EventHandler.CallLearnedTech(this.techName);
}
public void NewTurn()
{
if(availabilityState == AvailabilityState.Researching)
{
researchedTurns++;
if(researchedTurns >= requiredResearchTurns)
{
Learn();
}
}
}
private void CheckRequirements(Technology tech)
{
if(availabilityState == AvailabilityState.Locked)
{
RequiredTech requireTech = techRequirements.FirstOrDefault(rt => rt.techName == tech.techName);
if(requireTech != null && !requireTech.completed)
{
requireTech.completed = true;
if(!techRequirements.Any(t => t.completed == false))
{
Unlock();
}
}
}
}
public bool StartResearching()
{
bool hasEnoughResources = false;
List<ResourceAmount> resourceAmounts = new List<ResourceAmount>();
for(int i = 0; i < resourceCosts.Count; i++)
{
ResourceAmount currentAmount = TechResources.Instance.GetResourceAmount(resourceCosts[i].resourceType);
ResourceAmount cost = resourceCosts[i];
//TODO add in quest item check
if(resourceCosts[i].resourceType == ResourceTypes.QuestItem)
{
}
if(currentAmount.resourceAmount >= cost.resourceAmount)
{
resourceAmounts.Add(currentAmount);
}
}
if(resourceAmounts.Count >= resourceCosts.Count)
{
hasEnoughResources = true;
for (int i = 0; i < resourceCosts.Count; i++)
{
resourceAmounts[i].resourceAmount -= resourceCosts[i].resourceAmount;
}
availabilityState = AvailabilityState.Researching;
TechEvents.TechResearchStarted(this);
TechEvents.OnTurnPassed += NewTurn;
GameObject.FindObjectOfType<TechTreeUI>().UpdateAvailableXP();
GameObject.FindObjectOfType<TechTreeUI>().RemoveResourceCost(techName);
}
return hasEnoughResources;
}
}
[System.Serializable]
public class RequiredTech
{
public string techName;
public bool completed;
}
[System.Serializable]
public class TechModifiers
{
public enum ModifierType { Recipe, Tool, Gameplay }
public ModifierType modifier;
public string item;
}
Hopefully this is enough information, but if not, let me know what else I can provide.
Thanks!