Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Loading array from .json

Discussion in 'Scripting' started by Aldebaraan, Jun 26, 2020.

  1. Aldebaraan

    Aldebaraan

    Joined:
    Jun 21, 2020
    Posts:
    6
    .json values:
    Code (CSharp):
    1.  
    2.     "Presets":
    3.    {
    4.        "facepresets":["FacePreset1", "FacePreset2", "FacePreset3"]
    5.     },
    6.  
    7.  
    8.     "FacePreset1" : {
    9.        "Overall_Face_Breadth": {"x":0.1, "y":0.2},
    10.        "Upper_face_depth": {"x":0.1, "y":0.2},
    11.        "Face_height": {"x":0.1, "y":0.2},
    12.        "Lower_face_depth": {"x":0.1, "y":0.2},
    13.        "Lower_face_width": {"x":0.1, "y":0.2},
    14.     },
    15.  
    16.     "FacePreset2" : {
    17.        "Overall_Face_Breadth": {"x":0.2, "y":0.3},
    18.        "Upper_face_depth": {"x":0.2, "y":0.3},
    19.        "Face_height": {"x":0.2, "y":0.3},
    20.        "Lower_face_depth": {"x":0.2, "y":0.3},
    21.        "Lower_face_width": {"x":0.2, "y":0.3},
    22.     },
    23.  
    24.     "FacePreset3" : {
    25.        "Overall_Face_Breadth": {"x":0.3, "y":0.4},
    26.        "Upper_face_depth": {"x":0.3, "y":0.4},
    27.        "Face_height": {"x":0.3, "y":0.4},
    28.        "Lower_face_depth": {"x":0.3, "y":0.4},
    29.        "Lower_face_width": {"x":0.3, "y":0.4},
    30.     },
    31.  

    some C# code:
    Code (CSharp):
    1.  
    2.     [System.Serializable]
    3.     public class Token
    4.     {
    5.         public FacePreset facepresets;
    6.     }
    7.  
    8.     [System.Serializable]
    9.     public class FacePreset
    10.     {
    11.         public Vector2 Overall_Face_Breadth;
    12.         public Vector2 Upper_face_depth;
    13.         public Vector2 Face_height;
    14.         public Vector2 Lower_face_depth;
    15.         public Vector2 Lower_face_width;
    16.     }  
    17.    
    18.     public Vector2 currentkey;
    19.  
    20.     void RandomiseCharFace()
    21.     {
    22.         if (female == null) return;
    23.        var PATH_TO_JSON = Path.Combine(Directory.GetCurrentDirectory(), "\\Randomizer.json");
    24.         var token = JsonUtility.FromJson<Token>(System.IO.File.ReadAllText(PATH_TO_JSON));
    25.        
    26.        num1 = facepresets[UnityEngine.Random.Range(0, facepresets.Length)];
    27.        
    28.         int slider = 0;
    29.         //RandomFacePreset = UnityEngine.Random.Range(currentkey.x, currentkey.y);
    30.         string RandomFacePreset = "FacePreset1";
    31.  
    32.         foreach (PropertyInfo Property in token.facepresets.GetType().GetProperties())
    33.         {
    34.             Vector2 currentkey = (Vector2)Property.GetValue(token.facepresets);
    35.             female.customInfo.shapeValueBody[slider] = UnityEngine.Random.Range(currentkey.x, currentkey.y);
    36.             slider++;
    37.         }
    38.    }
    39.  
    The code above seems to be enough to get a predefined preset like FacePreset1.

    What i'm trying to do: Pick a item randomly from facepresets in the .json and then apply it to
    Vector2 currentkey = (Vector2)Property.GetValue(token.random_facepresets_here);

    e.g. try #1 gets FacePreset3. It gets the vector values from "FacePreset3" in the .json and apply them one by one inside the foreach loop

    This is to make possible for users to create their own presets without needing to compile the source code.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    Instead of the array of the names and then the names tied to each preset. Instead just have an array of presets:

    JSON:
    Code (csharp):
    1.  
    2. {
    3.     "Presets":[{
    4.        "Name":"FacePreset1",
    5.        "Overall_Face_Breadth": {"x":0.1, "y":0.2},
    6.        "Upper_face_depth": {"x":0.1, "y":0.2},
    7.        "Face_height": {"x":0.1, "y":0.2},
    8.        "Lower_face_depth": {"x":0.1, "y":0.2},
    9.        "Lower_face_width": {"x":0.1, "y":0.2},
    10.     },
    11.     {
    12.        "Name":"FacePreset2",
    13.        "Overall_Face_Breadth": {"x":0.2, "y":0.3},
    14.        "Upper_face_depth": {"x":0.2, "y":0.3},
    15.        "Face_height": {"x":0.2, "y":0.3},
    16.        "Lower_face_depth": {"x":0.2, "y":0.3},
    17.        "Lower_face_width": {"x":0.2, "y":0.3},
    18.     },
    19.     {
    20.        "Name":"FacePreset3",
    21.        "Overall_Face_Breadth": {"x":0.3, "y":0.4},
    22.        "Upper_face_depth": {"x":0.3, "y":0.4},
    23.        "Face_height": {"x":0.3, "y":0.4},
    24.        "Lower_face_depth": {"x":0.3, "y":0.4},
    25.        "Lower_face_width": {"x":0.3, "y":0.4},
    26.     }]
    27. }
    28.  
    Contracts:
    Code (csharp):
    1.  
    2. [System.Serializable]
    3. public class Token
    4. {
    5.     public FacePreset[] Presets;
    6. }
    7.  
    8. [System.Serializable]
    9. public class FacePreset
    10. {
    11.     public string Name;
    12.     public Vector2 Overall_Face_Breadth;
    13.     public Vector2 Upper_face_depth;
    14.     public Vector2 Face_height;
    15.     public Vector2 Lower_face_depth;
    16.     public Vector2 Lower_face_width;
    17. }
    18.  
    Then pick a random preset:
    Code (csharp):
    1.  
    2. void RandomiseCharFace()
    3. {
    4.     if (female == null) return;
    5.     var PATH_TO_JSON = Path.Combine(Directory.GetCurrentDirectory(), @"\Randomizer.json");
    6.     var token = JsonUtility.FromJson<Token>(System.IO.File.ReadAllText(PATH_TO_JSON));
    7.  
    8.     var preset = = token.Presets[UnityEngine.Random.Range(0, token.Presets.Length)];
    9.    
    10.     //not sure what the shape of this 'female' is... so I'm just going to read out values
    11.     var v1 = preset.Overall_Face_Breadth;
    12.     var v2 = preset.Upper_face_depth;
    13.     var v3 = preset.Face_height;
    14.     //so on, so forth
    15. }
    16.  
     
    Aldebaraan likes this.
  3. Aldebaraan

    Aldebaraan

    Joined:
    Jun 21, 2020
    Posts:
    6
    Thank you! But isn't there a way to automate this inside a foreach loop? Otherwise i would need to do this:
    Code (csharp):
    1.  
    2. void RandomiseCharFace()
    3. {
    4.     if (female == null) return;
    5.     var PATH_TO_JSON = Path.Combine(Directory.GetCurrentDirectory(), @"\Randomizer.json");
    6.     var token = JsonUtility.FromJson<Token>(System.IO.File.ReadAllText(PATH_TO_JSON));
    7.  
    8.     var preset = token.Presets[UnityEngine.Random.Range(0, token.Presets.Length)];
    9.     var v1 = preset.Overall_Face_Breadth;
    10.     var v2 = preset.Upper_face_depth;
    11.     var v3 = preset.Face_height;
    12.     var v4 = preset.Lower_face_depth;
    13.     var v5 = preset.Lower_face_width;
    14.     female.customInfo.shapeValueBody[0] = UnityEngine.Random.Range(v1.x, v1.y);
    15.     female.customInfo.shapeValueBody[1] = UnityEngine.Random.Range(v2.x, v2.y);
    16.     female.customInfo.shapeValueBody[2] = UnityEngine.Random.Range(v3.x, v3.y);
    17.     female.customInfo.shapeValueBody[3] = UnityEngine.Random.Range(v4.x, v4.y);
    18.     female.customInfo.shapeValueBody[4] = UnityEngine.Random.Range(v5.x, v5.y);
    19. }
    20.  
    female.customInfo.shapeValueBody[x] is the slider value. 0 if for Overall_Face_Breadth, 1 is for Upper_face_depth, etc. This is why a automization makes more sense to me.

    And wouldn't using var preset pick a random preset for each value?
    e.g.
    var v1 = preset.Overall_Face_Breadth;
    gets facepreset2
    and then
    var v2 = preset.Upper_face_depth;
    gets facepreset1
    ???