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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

EditorJsonUtility in Unity 5.5

Discussion in 'Scripting' started by SanityIsOverrated, Dec 30, 2016.

  1. SanityIsOverrated

    SanityIsOverrated

    Joined:
    Dec 22, 2013
    Posts:
    31
    Hello,

    according to the release notes of Unity 5.5
    I wanted to try this as it sounds very useful for saving data from Editor scripts. However, for me, EditorJsonUtility doesn't generate a fileID for object references, not even an instanceID as before.

    Here are my test scripts:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Test : MonoBehaviour
    7. {
    8.     public GameObject reference;
    9.     public Transform component;
    10.     public string data;
    11. }
    12.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [CustomEditor(typeof(Test))]
    7. public class TestEditor : Editor
    8. {
    9.     public Test Target
    10.     {
    11.         get
    12.         {
    13.             return (Test)target;
    14.         }
    15.     }
    16.  
    17.     public override void OnInspectorGUI()
    18.     {
    19.         Debug.Log(EditorJsonUtility.ToJson(Target));
    20.         Debug.Log(JsonUtility.ToJson(Target));
    21.         base.OnInspectorGUI();
    22.     }
    23. }
    24.  
    I get this from EditorJsonUtility.ToJson:
    Code (JavaScript):
    1. {"MonoBehaviour":{"m_Enabled":true,"m_EditorHideFlags":0,"m_Name":"","m_EditorClassIdentifier":"","reference":{"instanceID":0},"component":{"instanceID":0},"data":"Test"}}
    -> instanceID is 0 ??? no fileID!

    JsonUtility.ToJson is atleast generating instanceIDs...
    Code (JavaScript):
    1. {"reference":{"instanceID":9326},"component":{"instanceID":9336},"data":"Test"}
    Is this broken or has anyone worked out how to use this "feature"?

    Best,
    SanityIsOverrated
     
  2. jterry

    jterry

    Joined:
    Oct 30, 2014
    Posts:
    9
    Did you ever figure this out? I have the same problem.
     
  3. hedgehog90

    hedgehog90

    Joined:
    May 18, 2016
    Posts:
    27
    Also wondering what's up with this.
    It doesn't appear to be a bug, but can someone explain this behaviour?
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    What file ID could it possibly give for a scene object? I've never used it, but I would assume it would work more effectively on an asset instead. Try it on a ScriptableObject saved to the disk, or a prefab, and see if you have better luck there.
     
  5. mm1000

    mm1000

    Joined:
    Dec 3, 2015
    Posts:
    3
    Unity 2017.3.0p1
    I've got an object with script that links two Transforms. When I serialize it by script below I see this: "instanceID": 0

    private static string GetParameters(GameObject go)
    {
    string ans = "";

    ans += EditorJsonUtility.ToJson(go,true); // GameObject
    var components = go.GetComponents<Component>();

    for (int i = 0, len = components.Length; i < len; i++)
    {
    var componentString = EditorJsonUtility.ToJson(components[i], true);
    ans = ans.Insert(ans.Length - 1, "," + componentString.Substring(1, componentString.Length - 2));
    }
    return ans;
    }
     
  6. philmzo

    philmzo

    Joined:
    Dec 2, 2015
    Posts:
    7
    I also have this problem with EditorJsonUtility.ToJson.
    tried on an asset file and only the first level (0) is given, while content of .asset file shows the sub-level content.
     
  7. usernameHed

    usernameHed

    Joined:
    Apr 5, 2016
    Posts:
    92
    Same probleme here, what is going on ?