LoginSignup
0
1

More than 3 years have passed since last update.

Unityメモ

Last updated at Posted at 2020-05-25

JavaScript

https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse


    ws.on('message',function(message){
        const obj = JSON.parse(message);

        console.log("command: ", obj.command);
        console.log("level: ", obj.level);
        if( obj.command == 'move'){
          console.log('timeElapsed : '+ obj.timeElapsed)
        }
    });

Unity

    https://docs.unity3d.com/ja/2018.4/Manual/JSONSerialization.html
    [Serializable]
    public class MyClass
    {
        public string command;
        public int level;
        public float timeElapsed;
    }
    void Update() {

        MyClass myObject = new MyClass();
            myObject.command = "move";
            myObject.level = 1;
            myObject.timeElapsed = 47.5f;
            string json = JsonUtility.ToJson(myObject);

            byte[] data = System.Text.Encoding.ASCII.GetBytes(json);

            ws.Send(data);
        }

unity > stringからbyte[]への変換

byte[] data = System.Text.Encoding.ASCII.GetBytes(text);
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1