Search Unity

JSONUtility and properties

Discussion in 'Scripting' started by Nigey, Jan 28, 2016.

  1. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    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?

    Code (CSharp):
    1.         #region Chapter
    2.         [Serializable]
    3.         public class Chapter
    4.         {
    5.             // General
    6.             public string _name;
    7.             public string _description;
    8.             public bool _isCompleted = false;
    9.             public float _percentageComplete = 0f;
    10.             public List<Section> _sectionsList = new List<Section>();
    11.  
    12.             // Public Properties
    13.             public string name { get { return _name; } protected set { _name = value; } }
    14.             public string description { get { return _description; } protected set { _description = value; } }
    15.             public bool isComplete { get { return CheckComplete(_sectionsList); } }
    16.             public float percentageComplete { get { return CheckPercentageComplete(_sectionsList); } }
    17.  
    18.             // Constructors
    19.             public Chapter() { }
    20.             public Chapter(string name, string description, Section subChapter)
    21.             {
    22.                 _name = name;
    23.                 _description = description;
    24.                 _sectionsList.Add(subChapter);
    25.             }
    26.  
    27.             public Chapter(string name, string description, Section[] subChapters = null)
    28.             {
    29.                 _name = name;
    30.                 _description = description;
    31.                
    32.                 if (subChapters != null)
    33.                 {
    34.                     for (int i = 0; i < subChapters.Length; i++)
    35.                     {
    36.                         _sectionsList.Add(subChapters[i]);
    37.                     }
    38.                 }
    39.             }
    40.            
    41.             protected bool CheckComplete(List<Section> sections)
    42.             {
    43.                 bool passed = true;
    44.  
    45.                 if(sections != null && sections.Count > 0)
    46.                 {
    47.                     for(int i=0; i<sections.Count;i++)
    48.                     {
    49.                         if (!sections[i].isComplete)
    50.                         {
    51.                             passed = false;
    52.                             break;
    53.                         }
    54.                     }
    55.                 }
    56.                
    57.                 return (_isCompleted = passed);
    58.             }
    59.  
    60.             protected float CheckPercentageComplete(List<Section> sections)
    61.             {
    62.                 float completed = 0;
    63.  
    64.                 if (sections != null && sections.Count > 0)
    65.                 {
    66.                     for (int i = 0; i < sections.Count; i++)
    67.                     {
    68.                         if (sections[i].isComplete)
    69.                         {
    70.                             completed++;
    71.                         }
    72.                     }
    73.  
    74.                     return (_percentageComplete = ((completed / sections.Count) * 100));
    75.                 }
    76.                 else
    77.                 {
    78.                     return 0f;
    79.                 }
    80.             }
    81.         }
    82.         #endregion
     
  2. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Have you tried the SerializeField attribute?
     
    vctr_ro and Nigey like this.
  3. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Would need to be that simple wouldn't it lol, thanks