I have a system in place for saving the objects in my scene to different save files according to your profile. At the base, I’m using the Into the Dev save system, which utilizes the Odin Serializer. It works perfectly in the editor, and not at all on mobile. It seems like it doesn’t really manage to save files. Below I’ll write down the important parts of the code, and then what I’ve tried so far and how it’s worked out.
Firstly, I’ve modified a bit the “DataSerializer” script from IntoTheDev, so as to write the names of saves always with two digits(Save00, Save01, etc), to make loading easier.
public static void ChangeProfile(int profileIndex)
{
if (_currentProfileIndex == profileIndex)
return;
if (profileIndex < 10) filePathModifier = "_0";
else filePathModifier = "_";
SaveFile();
_currentProfileIndex = profileIndex;
GeneratePath();
LoadFile();
}
“ChangeProfile” is called whenever we change the current save file to be used.
private static void GeneratePath() => _savePath = Path.Combine(Application.persistentDataPath, $"{FILE_NAME}" + filePathModifier + $"{_currentProfileIndex}.data");
Basically I just modify the path name a little bit according to our current save profile.
Below are the saving and loading scripts I use, stripped down a bit to make them more readable:
public void SaveAllPOIs()
{
GameManager.savedPOIs.Clear();
foreach (POI poi in addedPOIs)
{
GameManager.savedPOIs.Insert(poi.index, poi);
}
DataSerializer.Save("POI_List", GameManager.savedPOIs);
GameManager.localizationManager.SendMessageToConsole("console_sauv_OK");
}
void Start()
{
if (DataSerializer.TryLoad("POI_List", out List<POI> poilist))
{
for (int i = 0; i < poilist.Count; i++)
{
AddPOIOnLocation(poilist[i].location, poilist[i]);
}
}
}
public void AddPOIOnLocation(Vector2d location, POI poivar)
{
var instance = Instantiate(spawner._markerPrefab, markerParent.transform);
instance.name = "Marker" + (addedPOIs.Count + 1);
instance.transform.localPosition = _map.GeoToWorldPosition(location, true);
instance.transform.localScale = new Vector3(spawner._spawnScale, spawner._spawnScale, spawner._spawnScale);
poivar.marker = instance;
//Spawner variables
spawner.IncreaseMarkers(instance);
spawner.IncreaseLocations(poivar.location);
addedPOIs.Add(poivar);
DrawConnections();
LabelMagic();
}
This is inside the same file as the above, if that matters:
[System.Serializable]
public class POI
{
public int index;
public GameObject marker;
public Vector2d location;
}
As I said, everything works perfectly fine in the editor(I can’t test in a Windows build because I don’t have a system to check for geolocalization on desktop computers, which breaks the game in that case. I use the Mapbox SDK by the way). I’ve had some pointers, which all lead to a dead end:
-
I thought it was a read/write issue on mobile. For Android, I’ve tried manually adding storage permissions on the manifest, as well as allowing External Sotrage in the player settings. Then I’ve tried accessing different folders other than the persistent data path, which worked. I just couldn’t save.
-
The very same thing happens on iOs, as wel as several annoying errors on compilation.
-
“Cannot initialize a parameter of type ‘id _Nonnull’ with an rvalue of type ‘Class’”. If I remember correctly, this stopped the build from finishing. I’ve followed the instructions here and it worked.
-
"MissingMethodException: Default constructor not found for type ToolBox.Serialization.OdinSerializer.Int32Serializer". This one I believe came up when trying to save. For this I added ```
public Int32Serializer() : base() {}
- __**"AOT formatter support was missing for type ‘System.Collections.Generic.List<Vector3>’"**__. This error showed up when I clicked on the save button. Now, this pointed me in another direction. I thought the problem might be AOT code stripping. So I've tried disabling code stripping altogether. Hasn't made a difference on Android, still haven't had the chance to retest on iOS. I've messed around with other build settings on Unity to no avail.
I would appreciate it if anyone had some insight onto what might be happening here. I've wasted wayy too much time already debugging this, so I can't really just keep testing different builds on different platforms while putting the project on hold. Please let me know if any information is missing.
**Unity Version:** 2020.3.33f1
**Development platform:** Windows 10, Mac for iOS building.