Untiy里JsonUtility.ToJson使用方法

Untiy里JsonUtility.ToJson使用方法

unityapi里只是做了一些简单的介绍,下面简单介绍下结构体里有数组的情况下ToJson()的使用方法;

1.要把数组结构里面的元素单独赋值,开辟内存再进行ToJson()
2.结构体必须要可序列化,不然识别不出来

下面展示一些 内联代码片

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System;

[Serializable]
public class CurlingthrowData
{
		public Vector3 Dir;//Current Rotation
	 public Vector3 posZ;//Current Position
   	 public float Time;//CurrentTime
	 public CurlingthrowData(Vector3 Dir1, Vector3 posZ1, float Time1)
	 {
    Dir = Dir1;
    posZ = posZ1;
    Time = Time1;
  }
}
[Serializable]
public class L4RsultData
{
		 public CurlingthrowData []ThrowDatas1;
		public Vector3 CurlingStopPosition;
		 public Vector3 CurlingStopEulerAngles;
		 public float StopTime;
		 public float Score;
		public L4RsultData(CurlingthrowData [] ThrowDatas11, Vector3 CurlingStopPosition1, Vector3 					CurlingStopEulerAngles1, float StopTime1, float Score1)
		 {
   			 CurlingStopPosition = CurlingStopPosition1;
   			 ThrowDatas1 = ThrowDatas11;
   			 CurlingStopEulerAngles = CurlingStopEulerAngles1;
   			 StopTime = StopTime1;
   			 Score = Score1;

			}
			}
	[Serializable]
	public class Strenth
	{	
 	public L4RsultData[] L4RsultData;
	}

		public class Test_Graph : MonoBehaviour 
		{
		 	 public Strenth strenth;
			
		 	
			 public float health;
void Start()   
{
    strenth = new Strenth();
    

    Vector3 temp = new Vector3(0, 2, 2);
    Vector3 temp1 = new Vector3(2, 2, 2);
    float sa = 2;

    CurlingthrowData temp2 = new CurlingthrowData(temp, temp1, sa);
    CurlingthrowData[] temp3 = new CurlingthrowData[1];
    temp3[0] = temp2;
    Vector3 curliingstopPosition = new Vector3(0, 0, 0);
    Vector3 CurlingStopEulerAngles = new Vector3(0, 0, 0);
    float stoptime = 2;
    float score = 30;

    strenth.L4RsultData = new L4RsultData [1];
    strenth.L4RsultData[0] = new L4RsultData ( temp3, curliingstopPosition, CurlingStopEulerAngles, stoptime, score );


    string a = JsonUtility.ToJson(strenth);
    print(a);//最后运行一下

}



public void Update()
{

}

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宝马变青桔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值