Unity¤Ë´ØÏ¢¤¹¤ëµ­»ö¤Ç¤¹

¼ñ»Ý


¡¡PlayFab ¤Ê¤É¤Î¥µ¡¼¥Ð¡¼¤ËÊݸ¤·¤¿¥Ç¡¼¥¿¤Î¿¤¯¤Ï Json ·Á¼°¤Î¥Õ¥¡¥¤¥ë¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¡£
¤½¤Î¤¿¤á¡¢¥×¥ì¥¤¥ä¡¼¥Ç¡¼¥¿¤ä¥²¡¼¥àÆâ¤Î¾õÂÖ¤ò¸úΨŪ¤«¤Ä¡¢Àµ¤·¤¤ÊýË¡¤ò»È¤Ã¤ÆÊݸ¤ª¤è¤ÓÆɤ߹þ¤àɬÍפ¬¤¢¤ê¤Þ¤¹¡£

¡¡Ëܵ­»ö¤Ç¤Ï¡¢Json.NET¤ÈUniRx¤ÎReactiveProperty¤òÁȤ߹ç¤ï¤»¤Æ¡¢¥·¥ê¥¢¥é¥¤¥º¤È¥Ç¥·¥ê¥¢¥é¥¤¥º¤ò¹Ô¤¦¼êË¡¤ò²òÀ⤷¤Þ¤¹¡£


¤ä¤ê¤¿¤¤¤³¤È


¡¡¡¦Unity¥²¡¼¥àÆâ¤Î¥×¥ì¥¤¥ä¡¼¥Ç¡¼¥¿¤òJson·Á¼°¤ÇÊݸ
¡¡¡¦Êݸ¤µ¤ì¤¿Json¥Ç¡¼¥¿¤òÆɤ߹þ¤ó¤ÇUnity¥ª¥Ö¥¸¥§¥¯¥È¤Ë¥Ç¥·¥ê¥¢¥é¥¤¥º
¡¡¡¦¥ê¥¢¥¯¥Æ¥£¥Ö¥×¥í¥°¥é¥ß¥ó¥°¥é¥¤¥Ö¥é¥êUniRx¤ÎReactiveProperty¤ò»È¤Ã¤Æ¸úΨŪ¤Ê¥Ç¡¼¥¿´ÉÍý


¤Ç¤­¤Ê¤¤¤³¤È


¡¡Ä̾ï¤ÎJson.NET¤Ç¤ÏReactiveProperty¤Î¥Ç¥·¥ê¥¢¥é¥¤¥º¤¬½ÐÍè¤Þ¤»¤ó¡£

¡¡Î㤨¤Ð¡¢°Ê²¼¤Î¤è¤¦¤ÊReactiveProperty¤ò»ý¤Ä¥¯¥é¥¹¤¬¤¢¤ë¤È¤·¤Þ¤¹¡£

[System.Serializable]
public class PlayerData {

    public ReactiveProperty<int> Score { get; set; }

    // ¥³¥ó¥¹¥È¥é¥¯¥¿
    public PlayerData() {
        // ½é´ü²½¤Ê¤ÉɬÍפ˱þ¤¸¤Æ½èÍý¤òÄɲÃ
        Score = new ReactiveProperty<int>();
    }
}

¡¡¤³¤Î¥¯¥é¥¹¤òÄ̾ï¤ÎJson.NET¤Ç¥Ç¥·¥ê¥¢¥é¥¤¥º¤·¤è¤¦¤È¤¹¤ë¤È¡¢¤¦¤Þ¤¯¤¤¤«¤º¥¨¥é¡¼¤¬È¯À¸¤·¤Þ¤¹¡£

ArgumentException: Could not cast or convert from System.Int64 to UniRx.ReactiveProperty`1[System.Int32].
Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable (System.Object value, System.Type initialType, System.Type targetType)

JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Int32' 
because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly.
To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change 
the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object.
JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.



²ò·è¤Î¥¢¥×¥í¡¼¥ÁÊýË¡­¡¡¡¡¼Json ¤ÎÆâÍƤò ReactiveProperty ¤ËÂбþ²Äǽ¤Ê·Á¼°¤Ë¤·¤ÆÂбþ¤¹¤ë¡¼


¡¡Json.NET¤Ë¤ª¤¤¤Æ¡¢ReactiveProperty ¤òÀµ¤·¤¯¥Ç¥·¥ê¥¢¥é¥¤¥º¤¹¤ë¤¿¤á¤Ë¤Ï¡¢JsonConveter ¥¯¥é¥¹¤ò·Ñ¾µ¤·¤¿
ReactivePropertyConverter ¤òºîÀ®¤·¡¢ReadJson ¥á¥½¥Ã¥É¤È WriteJson ¥á¥½¥Ã¥É¤ò¥ª¡¼¥Ð¡¼¥é¥¤¥É¤·¤Æ¼ÂÁõ¤·¤Þ¤¹¡£

¡¡°Ê²¼¤Ï¤½¤Î¥µ¥ó¥×¥ë¥³¡¼¥É¤Ç¤¹¡£

using Newtonsoft.Json;
using UniRx;
using System;
using Newtonsoft.Json.Linq;

public class ReactivePropertyConverter<T> : JsonConverter<ReactiveProperty<T>> {

    public override void WriteJson(JsonWriter writer, ReactiveProperty<T> value, JsonSerializer serializer) {
        if (value != null && value.HasValue) {
            // HasValue¤¬true¤Î¾ì¹ç¡¢Value¤ò½ÐÎÏ
            serializer.Serialize(writer, new { Value = value.Value, HasValue = true });
        } else {
            // HasValue¤¬false¤Î¾ì¹ç¡¢null¤ò½ÐÎÏ
            serializer.Serialize(writer, null);
        }
    }

    public override ReactiveProperty<T> ReadJson(JsonReader reader, Type objectType, ReactiveProperty<T> existingValue, bool hasExistingValue, JsonSerializer serializer) {
        var token = JToken.Load(reader);

        if (token.Type == JTokenType.Null) {
            // JSON¤¬null¤Î¾ì¹ç¡¢¿·¤·¤¤ReactiveProperty¤òºîÀ®¤·¤ÆÊÖ¤¹
            return new ReactiveProperty<T>();
        } else {
            // JSON¤¬Ãͤò»ý¤Ã¤Æ¤¤¤ë¾ì¹ç¡¢¤½¤ÎÃͤò¸µ¤Ë¿·¤·¤¤ReactiveProperty¤òºîÀ®¤·¤ÆÊÖ¤¹
            var valueToken = token["Value"];

            if (valueToken != null) {
                var value = valueToken.ToObject<T>(serializer);
                return new ReactiveProperty<T>(value);
            } else {
                // "Value"¥×¥í¥Ñ¥Æ¥£¤¬Â¸ºß¤·¤Ê¤¤¾ì¹ç¡¢Îã³°½èÍý¤Þ¤¿¤Ï¥Ç¥Õ¥©¥ë¥È¤Î½èÍý¤ò¹Ô¤¦
                // Îã: throw new JsonSerializationException("Expected 'Value' property not found.");
                return new ReactiveProperty<T>();
            }
        }
    }
}


¥µ¥ó¥×¥ë¥³¡¼¥É


¡¡ReactivePropertyConverter ¥¯¥é¥¹¤Ï¡¢Å¬ÍѤ·¤¿¤¤ ReactiveProperty ¤Ë°À­¾ðÊó¤òÉÕÍ¿¤¹¤ë¤³¤È¤ÇÍøÍѤǤ­¤Þ¤¹¡£

¡¡Î㤨¤Ð¡¢Àè¤Û¤É¤Î PlayerData ¥¯¥é¥¹Æâ¤Î ReactiveProperty ¤Ë°À­¾ðÊó¤òÉÕÍ¿¤·¤Þ¤¹¡£

[System.Serializable]
public class PlayerData {
 
    [JsonConverter(typeof(ReactivePropertyConverter<int>))]  //  ¢«¡¡Â°À­¾ðÊó¤òÉÕÍ¿
    public ReactiveProperty<int> Score { get; set; }
}

¡¡¤³¤ì¤Ç¥Ç¥·¥ê¥¢¥é¥¤¥º¤È¥·¥ê¥¢¥é¥¤¥º¤¬²Äǽ¤Ê ReactiveProperty ¤Ë¤Ê¤ê¤Þ¤¹¡£

¡¡¤³¤Îµ¡Ç½¤ò³èÍѤ¹¤ë¤È Json ¤¬ ReactiveProperty ¤ËŬ¤·¤¿·Á¼°¤È¤·¤ÆÊݸ¡¦Æɤ߹þ¤ß¤µ¤ì¤Þ¤¹¡£

{
  "Score": {
    "Value": 0,
    "HasValue": true
  }
}



¡¡ÍøÍѤ¹¤ë¾ì¹ç¤Ë¤Ï¡¢²¼µ­¤Î¤è¤¦¤ËÍøÍѤ·¤Þ¤¹¡£

public class GameManager : MonoBehaviour {

    void Start() {
        // ¥×¥ì¥¤¥ä¡¼¥Ç¡¼¥¿¤ÎºîÀ®¤È½é´ü²½
        PlayerData player = new PlayerData {
            Score = new (1000)
        };

        // ¥×¥ì¥¤¥ä¡¼¥Ç¡¼¥¿¤òJson¤Ë¥·¥ê¥¢¥é¥¤¥º
        string json = JsonConvert.SerializeObject(player);

        // Json¥Ç¡¼¥¿¤ò¥Ç¥·¥ê¥¢¥é¥¤¥º¤·¤Æ¿·¤·¤¤¥×¥ì¥¤¥ä¡¼¥Ç¡¼¥¿¤òºîÀ®
        PlayerData loadedPlayer = JsonConvert.DeserializeObject<PlayerData>(json);

        // ¥Ç¥·¥ê¥¢¥é¥¤¥º¸å¤Î¥×¥ì¥¤¥ä¡¼¥Ç¡¼¥¿¤Î¥¹¥³¥¢¤òɽ¼¨
        Debug.Log("Loaded Player Score: " + loadedPlayer.Score.Value);
    }
}

¡¡¤³¤Î¥µ¥ó¥×¥ë¥³¡¼¥É¤Ç¤Ï¡¢ReactivePropertyConverter¤ò»ÈÍѤ·¤ÆJson.NET¤¬ReactiveProperty¤òÀµ¤·¤¯½èÍý¤Ç¤­¤ë¤è¤¦¤Ë¤·¤Æ¤¤¤Þ¤¹¡£

¡¡¤³¤ì¤Ë¤è¤ê¡¢UniRx¤ÎReactiveProperty¤ò´Þ¤à¥ª¥Ö¥¸¥§¥¯¥È¤ò´Êñ¤ËJson·Á¼°¤ÇÊݸ¡¦Æɤ߹þ¤ß¤Ç¤­¤ë¤è¤¦¤Ë¤Ê¤ê¤Þ¤¹¡£


²ò·è¤Î¥¢¥×¥í¡¼¥ÁÊýË¡­¢¡¡¡¼Json ¤ÎÆâÍƤϤ½¤Î¤Þ¤Þ¤ÇÂбþ¤¹¤ë¡¼


¡¡¥¢¥×¥í¡¼¥ÁÊýË¡­¡¤Î¾ì¹ç¡¢¤¹¤Ç¤ËÊݸ¤µ¤ì¤Æ¤¤¤ë Json ¥Ç¡¼¥¿¤¬Â¸ºß¤·¤Æ¤¤¤ë¤È¡¢
¥Õ¥¡¥¤¥ë¤Î·Á¼°¤¬¹ç¤ï¤Ê¤¤¤¿¤á¥¨¥é¡¼¤È¤Ê¤ê¤Þ¤¹¡£¤è¤Ã¤Æ¡¢ÅÓÃ椫¤é¤Î±¿ÍÑÊѹ¹¤Ï½ÐÍè¤Þ¤»¤ó¡£

¡¡ºÇ½é¤«¤é¥¢¥×¥í¡¼¥ÁÊýË¡­¡¤ÇÀ©ºî¤·¤Æ¤¤¤ë¾ì¹ç¤Ë¤ÏÌäÂꤢ¤ê¤Þ¤»¤ó¤¬¡¢
¤¹¤Ç¤Ë Json ¥Õ¥¡¥¤¥ë¤¬¤¢¤ë¾ì¹ç¤Ë¤Ï¡¢¤½¤Î¥Õ¥¡¥¤¥ë¤Î·Á¼°¤Ï¤½¤Î¤Þ¤Þ¤Ë¤·¤Æ¤ª¤­¡¢
Êݸ¤¹¤ë¥¿¥¤¥ß¥ó¥°¤ä¡¢Æɤ߹þ¤à¥¿¥¤¥ß¥ó¥°¤Ç ReactiveProperty ¤ËÂбþ¤µ¤»¤ëÊýË¡¤¬¤è¤¤¤Ç¤·¤ç¤¦¡£

¡¡°Ê²¼¤Ï¤½¤Î¥µ¥ó¥×¥ë¥³¡¼¥É¤Ç¤¹¡£


¥µ¥ó¥×¥ë¥³¡¼¥É


¡¡PlayerData ¥¯¥é¥¹¤Ë¡¢Json¤Î¥Ç¥·¥ê¥¢¥é¥¤¥º¤ª¤è¤Ó¥·¥ê¥¢¥é¥¤¥º¤Î¤¿¤á¤Î¥á¥½¥Ã¥É¤òÄɲ䷤Ƥ¤¤Þ¤¹¡£
ReactiveProperty¤ÏÈóSerializable¤È¤·¤ÆÊÝ»ý¤·¡¢Json¤Î½èÍýÁ°¸å¤ÇscoreÊÑ¿ô¤ÈscoreReactive¤ÎƱ´ü¤ò¤È¤ë¤è¤¦¤Ë¤·¤Æ¤¤¤Þ¤¹¡£
¤³¤ì¤Ë¤è¤ê¡¢Json¤Î¥·¥ê¥¢¥é¥¤¥º¡¦¥Ç¥·¥ê¥¢¥é¥¤¥º»þ¤ËReactiveProperty¤ÎÃͤâŬÀڤ˽èÍý¤Ç¤­¤Þ¤¹¡£


using UnityEngine;

[System.Serializable]
public class PlayerData
{
    [SerializeField]
    private int score;

    // ReactiveProperty¤ÏJsonUtility¤Ç¥·¥ê¥¢¥é¥¤¥º¤Ç¤­¤Ê¤¤¤Î¤Ç¡¢ÈóSerializable¤È¤·¤ÆÊÝ»ý
    private ReactiveProperty<int> scoreReactive;

    // ReactiveProperty¤ò³°Éô¤Ë¸ø³«¤¹¤ë¤¿¤á¤Î¥×¥í¥Ñ¥Æ¥£
    public IReadOnlyReactiveProperty<int> ScoreReactive => scoreReactive;


    // ¥³¥ó¥¹¥È¥é¥¯¥¿
    public PlayerData(int initialScore)
    {
        // ½é´ü²½¤Ê¤ÉɬÍפ˱þ¤¸¤Æ½èÍý¤òÄɲÃ
        score = initialScore;
        scoreReactive = new ReactiveProperty<int>(score);
    }

    // ¥µ¡¼¥Ð¡¼¤«¤é¼èÆÀ¤·¤¿Json¥Ç¡¼¥¿¤òPlayerData¤ËÊÑ´¹¤¹¤ë¥á¥½¥Ã¥É
    public static PlayerData FromJson(string json)
    {
        PlayerData playerData = JsonUtility.FromJson<PlayerData>(json);

        // JsonUtility¤Ç¥Ç¥·¥ê¥¢¥é¥¤¥º¤·¤¿¸å¡¢score¤òReactiveProperty¤ËÀßÄê
        playerData.scoreReactive.Value = playerData.score;

        return playerData;
    }

    // ¥µ¡¼¥Ð¡¼¤ËÁ÷¤ë¤¿¤á¤ÎJson¥Ç¡¼¥¿¤òÀ¸À®¤¹¤ë¥á¥½¥Ã¥É
    public string ToJson()
    {
        // ¥µ¡¼¥Ð¡¼¤ËÁ÷¤ëºÝ¤Ë¤Ï¡¢ReactiveProperty¤ÎÃͤòscoreÊÑ¿ô¤ËÀßÄê
        score = scoreReactive.Value;

        return JsonUtility.ToJson(this);
    }
}



¡¡¤³¤Î¾ì¹ç¤Î Json ·Á¼°¤Ï°ìÈÌŪ¤Ê·Á¼°¤Ë¤Ê¤ê¤Þ¤¹¡£

{
  "score": 0
}



¡¡ÍøÍѤ¹¤ë¾ì¹ç¤Ë¤Ï¡¢²¼µ­¤Î¤è¤¦¤ËÍøÍѤ·¤Þ¤¹¡£

using UnityEngine;
using Newtonsoft.Json;

public class GameManager : MonoBehaviour
{
    void Start()
    {
        // TODO ¥µ¡¼¥Ð¡¼¤«¤é¼õ¤±¼è¤Ã¤¿Json¥Ç¡¼¥¿¤ò²¾Äꤹ¤ë
        string receivedJsonFromServer = "{ \"score\": 1500 }";

        // PlayerData¤ÎFromJson¥á¥½¥Ã¥É¤ò»ÈÍѤ·¤ÆJson¥Ç¡¼¥¿¤ò¥Ç¥·¥ê¥¢¥é¥¤¥º
        PlayerData loadedPlayer = PlayerData.FromJson(receivedJsonFromServer);

        // ¥Ç¥·¥ê¥¢¥é¥¤¥º¸å¤Î¥×¥ì¥¤¥ä¡¼¥Ç¡¼¥¿¤Î¥¹¥³¥¢¤òɽ¼¨
        Debug.Log("Loaded Player Score: " + loadedPlayer.ScoreReactive.Value);

        // ¿·¤·¤¤¥×¥ì¥¤¥ä¡¼¥Ç¡¼¥¿¤òºîÀ®
        PlayerData newPlayer = new PlayerData(2000);

        // PlayerData¤ÎToJson¥á¥½¥Ã¥É¤ò»ÈÍѤ·¤Æ¥×¥ì¥¤¥ä¡¼¥Ç¡¼¥¿¤òJson¤Ë¥·¥ê¥¢¥é¥¤¥º
        string json = newPlayer.ToJson();

        // ¥·¥ê¥¢¥é¥¤¥º¤·¤¿Json¥Ç¡¼¥¿¤òɽ¼¨
        Debug.Log("Serialized Player Data: " + json);

        // TODO ¥µ¡¼¥Ð¡¼¤ØÁ÷¿®

    }
}

¡¡¥µ¡¼¥Ð¡¼¤«¤é¤ÎJson¥Ç¡¼¥¿¤ò²¾Äꤷ¤Æ¥Ç¥·¥ê¥¢¥é¥¤¥º¤·¡¢¿·¤·¤¤¥×¥ì¥¤¥ä¡¼¥Ç¡¼¥¿¤òºîÀ®¤·¤Æ¥·¥ê¥¢¥é¥¤¥º¤·¤Æ¤¤¤Þ¤¹¡£

¡¡¥²¡¼¥àÆâ¤Ç¤Ï PlayerData Æâ¤Î ReactiveProperty ¤ò³èÍѤ·¤Æ¹ØÆɽèÍý¤ò¹Ô¤¤¡¢
¥µ¡¼¥Ð¡¼¤ËÁ÷¿®¤¹¤ëÁ°¤Ë Json ¤ËÃÖ¤­´¹¤¨¤Æ¤«¤éÁ÷¿®¤¹¤ë¤è¤¦¤Ë¤·¤Þ¤¹¡£

¥³¥á¥ó¥È¤ò¤«¤¯


¡Öhttp://¡×¤ò´Þ¤àÅê¹Æ¤Ï¶Ø»ß¤µ¤ì¤Æ¤¤¤Þ¤¹¡£

ÍøÍѵ¬Ìó¤ò¤´³Îǧ¤Î¤¦¤¨¤´µ­Æþ²¼¤µ¤¤

Menu


´ðÁÃ

µ»½Ñ/Ãμ±(¼ÂÁõÎã)

3D¥¢¥¯¥·¥ç¥ó¥²¡¼¥à

2D¤ª¤Ï¤¸¤­¥²¡¼¥à(ȯŸÊÔ)

2D¶¯À©²£¥¹¥¯¥í¡¼¥ë¥¢¥¯¥·¥ç¥ó(ȯŸÊÔ)

2D¥¿¥Ã¥×¥·¥å¡¼¥Æ¥£¥ó¥°(³ÈÄ¥ÊÔ)

¥ì¡¼¥¹¥²¡¼¥à(È´¿è)

2DÊüÃÖ¥²¡¼¥à(ȯŸÊÔ)

3D¥ì¡¼¥ë¥¬¥ó¥·¥å¡¼¥Æ¥£¥ó¥°(±þÍÑÊÔ)

3Dæ½Ð¥²¡¼¥à(È´¿è)

2D¥ê¥¢¥ë¥¿¥¤¥à¥¹¥È¥é¥Æ¥¸¡¼

3D¥¿¥Ã¥×¥¢¥¯¥·¥ç¥ó(NavMeshAgent »ÈÍÑ)

2D¥È¥Ã¥×¥Ó¥å¡¼¥¢¥¯¥·¥ç¥ó(¥«¥¨¥ë¤Î°Ù¤Ë¡Á¡¢¥Ü¥³¥¹¥«¥¦¥©¡¼¥ºÉ÷)

VideoPlayer ¥¤¥Ù¥ó¥ÈϢư¤Î¼ÂÁõÎã

VideoPlayer ¥ê¥¹¥ÈÆ⤫¤é¥à¡¼¥Ó¡¼ºÆÀ¸¤Î¼ÂÁõÎã(ȯŸ)

AR ²èÁüÉÕ¤­¥ª¥Ö¥¸¥§¥¯¥ÈÀ¸À®¤Î¼ÂÁõÎã

AR ¥ê¥¹¥ÈÆ⤫¤éÀ¸À®¤Î¼ÂÁõÎã(ȯŸ)

2D¥È¥Ã¥×¥Ó¥å¡¼¥¢¥¯¥·¥ç¥ó(¥µ¥Ð¥¤¥Ð¡¼É÷)

private



¤³¤Î¥µ¥¤¥ÈÆâ¤ÎºîÉʤϥæ¥Ë¥Æ¥£¤Á¤ã¤ó¥é¥¤¥»¥ó¥¹¾ò¹à¤Î¸µ¤ËÄ󶡤µ¤ì¤Æ¤¤¤Þ¤¹¡£

´ÉÍý¿Í/Éû´ÉÍý¿Í¤Î¤ßÊÔ½¸¤Ç¤­¤Þ¤¹