Version: 2017.1
public static void FromJsonOverwrite (string json, object objectToOverwrite);

Parameters

json The JSON representation of the object.
objectToOverwrite The object to overwrite.

Description

Overwrite data in an object by reading from its JSON representation.

This is similar to JsonUtility.FromJsonOverwrite, but it supports any engine object. The fields available are the same as are accessible via the SerializedObject API, or as found in the YAML-serialized form of the object.

Note that using this method with a struct may not do what you expect because structs are passed to the method by value and not by reference. This means that instead of the method overwriting your original struct, a boxed copy of the struct is passed into the method and overwritten. You can avoid this by making your own boxed copy of the struct to pass into the method and then copying the values back again after the method returns. See example below.

Even when you do this, Unity’s built-in structs (such as Vector3 or Bounds) cannot be directly passed to the method, so you must enclose Unity’s built-in structs inside a wrapper class or struct.

using UnityEngine;
using UnityEditor;

[System.Serializable] struct MyStruct { public int value; }

public class StructExample : MonoBehaviour { void Start() { MyStruct myStruct = new MyStruct(); object boxedStruct = myStruct; var json = @"{ ""value"" : 42 }"; EditorJsonUtility.FromJsonOverwrite(json, boxedStruct); myStruct = (MyStruct)boxedStruct; Debug.Log("myStruct.value = " + myStruct.value); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961