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");
}
}