这个星期真是太倒霉了,得了结石了,在医院打吊针吊了3天,重要的是那个痛啊,简直是太痛了。从这点我认识到了,身体是革命的本钱,以后不管干什么,保护好自己的身体才是最重要的。好了,不聊这些了,希望这黑色的星期快点过去,。关于U3D怎么读取JSON文件,怎么解析JSON。这里我用到的是

SimpleJSON,用的是官网的这种办法。其实还一种是ListJSON,用法都差不多。这里我就先说下怎么用SimpleJSON吧,首先我们要使用SimpleJSON这个库,就必须把库导到U3D里面,

unity3D读取SimpleJSON的办法_读取

记住这里必须plugins文件夹。因为他需要预编译。

好了然后我们写一个JSON文件吧,

unity3D读取SimpleJSON的办法_unity3D_02


然后我们在U3D就需要用SimpleJSON读取和解析JSON文件了

代码1:读取JSON

using UnityEngine;
using System.Collections;


public class LoadText : MonoBehaviour {
   
   public TextAsset txt;

   void Start()
   {

   GameTex.moveSpriet=txt.text;

   }
}


代码2:解析JSON 然后拿到里面的数据,实例化一个按钮


using UnityEngine;
using System.Collections;
using SimpleJSON;

public class GameTex : MonoBehaviour
{

    public  static string moveSpriet;
    public GameObject sprite;
    public GameObject go;
    public ArrayList gameText = new ArrayList ();
    int x = 0;
    int y = 0;

    void Update ()
    {

        InitTextList ();
    }

    
    public void InitTextList ()
    {
        var n = JSONNode.Parse (moveSpriet);
        for (int i = 0; i < n.Count; i++) {
            //  Debug.Log(n[i]);
            gameText.Add (loadJS ([0]));
            Debug.Log ([0]);

        }
  
    }

    public LoadText loadJS (JSONNode o)
    {
         
        // Debug.Log(o["name"]);
        //name=o["name"].AsInt;
        x = o ["x"].AsInt;
        y = o ["y"].AsInt;
        return null;
    }

  
    public void MoveSprite ()
    {
        GameObject target1 = Instantiate (sprite)as GameObject;
        target1.transform.parent = this.transform;
        target1.transform.localPosition = new Vector3 (x, y, 0);
        target1.transform.localScale = Vector3.one;
         
    }
}

这里就是拿到x和y,然后实例化


效果:



unity3D读取SimpleJSON的办法_unity3D_03