本文主要是介绍Unity JsonUtility创建Json,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
所有备注了eden的部分
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using UnityEngine.UI;
//eden:引入system,用于下方建类[serializable]
using System;//eden:建类,一个类代表Json中的一个数组
[Serializable]
public class arrJ
{public double[] arrPos = new double[3];public double[] arrRot = new double[9];
}public class GameController : MonoBehaviour
{public InputField[] inputFields;public Text submitOk;[DllImport("__Internal")]private static extern void OpenWindow();//eden:给js函数传递一个Json字符串[DllImport("__Internal")]private static extern void HandleSql(string arrJ);// Start is called before the first frame updatevoid Start(){ExitSubmitOk();}// Update is called once per framevoid Update(){}public void OnClickUrl(){OpenWindow();}//eden:把arrPos、arrRot两个数组加入到Json字符串中并传给js函数public void OnClickSubmit(){if (inputFields[0].text != null && inputFields[1].text != null){//HandleSql(double.Parse(inputFields[0].text), double.Parse(inputFields[1].text));arrJ aj = new arrJ();double tmp = 0.1;for (int i = 0; i < 3; i++){aj.arrPos[i] = tmp;tmp++;}for (int i = 0; i < 9; i++){aj.arrRot[i] = tmp;tmp++;}string arrj = JsonUtility.ToJson(aj);HandleSql(arrj);}}public void SubmitOk(string alert){submitOk.text = alert;submitOk.enabled = true;Invoke("ExitSubmitOk", 1);}public void ExitSubmitOk(){submitOk.enabled = false;}
}
这篇关于Unity JsonUtility创建Json的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!