当前位置:   article > 正文

Untiy里JsonUtility.ToJson使用方法

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/794240
推荐阅读
相关标签
  

闽ICP备14008679号