Save iDictionary

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
morrigan
Posts: 25
Joined: Sun Feb 14, 2016 7:16 pm

Save iDictionary

Post by morrigan »

Hello everybody, I am trying to complete the saving system of my project. This is the last part I guess!
I have a Text.Ui with a script that implements iDictionary to replace letters. Can I use the LoadDictionary method to save it? I've tried to save it, but the only error I get is about the variable I'm trying to access, which is non-static, so I don't know if it does work. This is the code with the Easy Save methods.

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;


public class ColorText: MonoBehaviour
{
	static ColorText instance;
	static	Text myText;
	string baseString;
	IDictionary<string,string> characterMap = new Dictionary<string,string> ();


	void Awake()
	{
		instance = this;
		myText = GetComponent<Text>();
		ChangeText(myText.text);
	}


	public static void AddColor(string letterAlphabet, Color textColor)
	{
		if(instance==null)
		{
			return;
		}

		letterAlphabet = Regex.Escape(letterAlphabet);
		instance.characterMap[letterAlphabet] = "<color=#"+ColorToHex(textColor)+">"+letterAlphabet+"</color>";
		instance.Format();
	}

	public static void RemoveColor(string letterAlphabet)
	{
		if(instance==null)
		{
			return;
		}

		letterAlphabet = Regex.Escape(letterAlphabet);
		if(instance.characterMap.ContainsKey(letterAlphabet))
		{
			instance.characterMap.Remove(letterAlphabet);
		}

		instance.Format();
	}

	public static void ChangeText(string newString)
	{
		if(instance==null)
		{
			return;
		}
		instance.baseString = newString;
		instance.Format();
	}

	static	string ColorToHex (Color textColor)
	{
		string hex  = ((int)(textColor.r * 255)).ToString("X2")
			+ ((int)(textColor.g * 255)).ToString("X2")
			+ ((int)(textColor.b * 255)).ToString("X2")
			+ ((int)(textColor.a * 255)).ToString("X2");

		return hex;
	}

	void Format()
	{
		if(characterMap.Count==0)
		{
			myText.text = baseString;
			return;
		}

		Regex regex = new Regex (String.Join ("|", characterMap.Keys.ToArray()));
		myText.text = regex.Replace (baseString, m => characterMap [m.Value]);
		if (ES2.Exists ("sleepsavefile.txt?tag=Text Saved")) 
		{
			instance.characterMap = ES2.LoadDictionary<string, string> ("sleepsavefile.txt?tag=Text Saved");
		}
	}

	public static void  saveText()
	{
		ES2.Save(characterMap, "sleepsavefile.txt?tag=Text Saved");
	}

}
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: Save iDictionary

Post by Joel »

Hi there,

Because IDictionary is an interface, it's not possible to serialise it because you cannot instantiate interfaces.

The easiest way to fix this is to change your characterMap variable from an IDictionary to a Dictionary. i.e.

Change this:
IDictionary<string,string> characterMap = new Dictionary<string,string> ();
To this:
Dictionary<string,string> characterMap = new Dictionary<string,string> ();
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
morrigan
Posts: 25
Joined: Sun Feb 14, 2016 7:16 pm

Re: Save iDictionary

Post by morrigan »

Okay, I had to fix a couple of errors in my code, but now everything works perfectly. Thank you very much Joel!
PabloAM
Posts: 26
Joined: Tue Nov 18, 2014 8:43 pm

Re: Save iDictionary

Post by PabloAM »

Hello, I´m going to release it to iOS and Android.
That works on both systems??

Thanks!
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: Save iDictionary

Post by Joel »

I see no reason why it shouldn't. Dictionary is supported on both platforms.
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
PabloAM
Posts: 26
Joined: Tue Nov 18, 2014 8:43 pm

Re: Save iDictionary

Post by PabloAM »

In your docs says it´s not compatible:

http://docs.moodkie.com/easy-save-2/api ... ictionary/

Thanks!
User avatar
Joel
Moodkie Staff
Posts: 4852
Joined: Wed Nov 07, 2012 10:32 pm

Re: Save iDictionary

Post by Joel »

I believe this is a limitation in earlier versions of Unity, but not not since Unity 4.

I'll remove the comment as this is no longer an issue.
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Locked