Hi Guys,
I’m using the JSONUtility in Unity 5.3. I want to have a class I convert to a JSONObject. I want to assign some of the variables with properties, to call some methods when you change the variable. However to allow JSONUtility to serialise the variables, they must be public. The issue being having a public ‘_variable’, with a public property ‘variable’, totally defeats the object. Does anyone have any workarounds or suggestions?
#region Chapter
[Serializable]
public class Chapter
{
// General
public string _name;
public string _description;
public bool _isCompleted = false;
public float _percentageComplete = 0f;
public List<Section> _sectionsList = new List<Section>();
// Public Properties
public string name { get { return _name; } protected set { _name = value; } }
public string description { get { return _description; } protected set { _description = value; } }
public bool isComplete { get { return CheckComplete(_sectionsList); } }
public float percentageComplete { get { return CheckPercentageComplete(_sectionsList); } }
// Constructors
public Chapter() { }
public Chapter(string name, string description, Section subChapter)
{
_name = name;
_description = description;
_sectionsList.Add(subChapter);
}
public Chapter(string name, string description, Section[] subChapters = null)
{
_name = name;
_description = description;
if (subChapters != null)
{
for (int i = 0; i < subChapters.Length; i++)
{
_sectionsList.Add(subChapters[i]);
}
}
}
protected bool CheckComplete(List<Section> sections)
{
bool passed = true;
if(sections != null && sections.Count > 0)
{
for(int i=0; i<sections.Count;i++)
{
if (!sections[i].isComplete)
{
passed = false;
break;
}
}
}
return (_isCompleted = passed);
}
protected float CheckPercentageComplete(List<Section> sections)
{
float completed = 0;
if (sections != null && sections.Count > 0)
{
for (int i = 0; i < sections.Count; i++)
{
if (sections[i].isComplete)
{
completed++;
}
}
return (_percentageComplete = ((completed / sections.Count) * 100));
}
else
{
return 0f;
}
}
}
#endregion