关于Unity中常用的数据结构和JSON处理(专题三)

数据结构通俗来讲就是用某个对象去存储数据集合,比如要存储100个整数,要用什么样的数据类型能把它们存储好。

Jason处理,服务器对接,配置文件的使用,Unity和Jason之间相互的转换。

Array

数组

1: 类型[] 名字 = new 类型[数量]{“初始化的值”, “”, ''”}; 如果有初始值也可以省略大小;
2: 优点:

  (1)内存连续,速度快;

3: 缺点:

  (1)大小固定,容易访问越界;

Array实例

1.创建Unity项目和文件目录,保存场景

2.创建一个空节点GameObject,创建一个脚本jason_test挂载在GameObject下面

 打开jason_test

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

public class jason_test : MonoBehaviour {

    // Use this for initialization
    void Start () {
        // 数组
        // string, 数组
        string[] str_array = new string[] { "yes", "blake", "hello" };//可以写string[3],也可以不写
        int[] int_array = new int[100];

        // 索引来访问0
        int_array[0] = 1;
        int_array[1] = 2;
        // end 

        Debug.Log("Length = " + int_array.Length);
        // 优点:连续的内存, 访问速度快,通过数组名称加索引就能访问,存储,使用。
        // 缺点:大小是固定,已经创建就不能加大/缩小;
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

3.运行结果


ArrayList

1: 属于 System.Collections 命令空间
2: ArrayList l = new ArrayList();
3: 操作:添加 Add(数据), 修改[index] = 内容, 删除 RemoveAt(index);
4: 优点:
  (1)不用固定大小;
  (2)可以存放任意类型;
5: 缺点
  (1)由于存放不同类型的数据,导致很多看不见的性能消耗, 多次转换等;

ArrayList实例

1.创建Unity项目和文件目录,保存场景

2.创建一个空节点GameObject,创建一个脚本jason_test挂载在GameObject下面

 打开jason_test

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

public class jason_test : MonoBehaviour {

    // Use this for initialization
    void Start () {
        // ArrayList
        // 大小是灵活的,不是一开始写死的
        // 所有的操作都是Object,Object是C#的基类,所有的数据类型都是基于Object类
        ArrayList array_list = new ArrayList();

        // 同一个list里面我可以是不同类型的数据;
        array_list.Add("string");
        array_list.Add(true);
        array_list.Add(false);
        array_list.Add(100);
        array_list.Add(10.5f);

        // array_list是把所有的对象都当作Object来处理,所以拿到的对象要强制转换为具体的类型
        // [索引]访问, 0开始
        string a = (string)array_list[0];
        Debug.Log(a);
        array_list.RemoveAt(0); // 删除第0个元素,删除之后原来的第1个元素就变成第0个
        array_list[0] = false;
        bool e = (bool)array_list[0];
        Debug.Log(e);
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

3.运行结果

List<T>

是介于Array和ArrayList之间的一种类型,比Array灵活,但是又没有ArrayList那么灵活

1: 属于 using System.Collections.Generic; 命令空间
2: List<T> l = new List<T>();
3: 操作:
  添加 Add(数据), 修改[index] = 内容, 删除 RemoveAt(index);
4: 优点:
  (1)不用固定大小;
  (2)存储的类型是泛型模板,比较灵活, 每个对象实例只能存储一个类型,没有那么多的强制转换操作,性能提高一点。

List<T>实例

1.创建Unity项目和文件目录,保存场景

2.创建一个空节点GameObject,创建一个脚本jason_test挂载在GameObject下面

 打开jason_test

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

public class jason_test : MonoBehaviour {

    // Use this for initialization
    void Start () { 
        // List<T>,一般都用这个
        // List<T> 指定模板,那么List就是存放这种数据类型
        List<float> float_list = new List<float>();
        float_list.Add(5.3f);
        float_list.Add(5.4f);

        // 访问,读写
        Debug.Log(float_list[0]);
        float_list[1] = 8;
        Debug.Log(float_list[1]);

        //打印List大小
        Debug.Log(float_list.Count);
        float_list.RemoveAt(0);
        Debug.Log(float_list.Count);
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

3.运行结果

Dictionary<K, T>

1: 属于 using System.Collections.Generic; 命令空间
2: Dictionary<KT, VT> l = new Dictionary<KT, VT>(); key --> value,key --> value。key可以是字符串,整数,也可以是其他的泛型的类型。
3: 操作:
  添加 Add(数据), 修改[key] = 内容, 删除 RemoveAt(index);
4: 优点:
  (1)不用固定大小;
  (2)存储的类型是泛型模板,比较灵活, 每个dict只能存储一个类型;
  (3) key也是泛型 string, int ....

Dictionary<K, T>实例

1.创建Unity项目和文件目录,保存场景

2.创建一个空节点GameObject,创建一个脚本jason_test挂载在GameObject下面

 打开jason_test

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

public class jason_test : MonoBehaviour {

    // Use this for initialization
    void Start () {  
       // 字典  key, value key(模板string, int..), value(模板...)
       // 比如用string类型的key来存放float类型的value;
       // key和value的类型确定了就不能更改
       Dictionary<string, float> dict = new Dictionary<string,float>();
       dict.Add("speed", 100.0f);
       dict.Add("Attack", 20.0f);

       Debug.Log(dict.Count); // 2

       Debug.Log(dict["speed"]); // 100.0f
       dict["speed"] = 3.0f;
       Debug.Log(dict["speed"]); // 3.0f

       dict.Remove("Attack");
       Debug.Log(dict.Count); // 1

    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

3.运行结果

Json数据格式

数据对象都是在内存里面的,怎样把内存中的数据对象存到磁盘中去呢?这就需要把内存中的数据转换成一种数据格式,再存到磁盘中去。

Jason就是把JavaScript的Object对象转换成一个文本存到磁盘中去。如把字符串存到磁盘里面,然后从磁盘中再读字符串出来,再把它反解为JavaScript的Object的内存对象。

1: JSON (JavaScript Object Notation)
 随着JavaScript的流行与互联网应用,JavaScript里面最强大的数据类型Object,使用起来极其的方便,能存key--value存的非常好,整数,小数,还能在Object里面嵌套Object。

 为了能更好的做数据交换,设计了JSON协议,能够将JavaScript里面的Object,变成可以阅读的文本数据及JSON数据格式。实现JavaScript里面的Object与JSON的转换,Object对象转换成JSON数据以后,方便传输与存储,JSON变为Object方便对象重建;

2: python语言, Lua语言等其它的脚本语言都有类是于JavaScript的Object数据结构,所以JSON数据能在其它的语言里面也非常方便的使用;

3: JSON采用完全独立于语言的文本格式的字符串(string),易于阅读与编写以及解析与生成,在很多时候数据交换都采用JSON, 数据--->JSON-->传输,存储--->解码JSON-->数据

4: 上面的过程又叫序列化与反序列化;

 序列化:内存数据---->打包---->介质

 反序列化:介质---->解析---->内存数据


Json数据格式

1: JSON Object {}; 里面为key: value;
2: value为数字, 11.0, 12, 0,
 value为bool true, false
 value为数组 [ 值, bool, 数组, Object]
 value 为Object { key: value}
3: Unity 5.3.x以后自带的Json数据解析器
5: Unity 5.3以前可以使用第三方的C#库LitJSon,老技术,没什么好讲的,在(第66)有压缩包LitJson.zip。


Unity5.3 Jason序列化

1: 序列化与反序列化: 内存数据-->文件存储介质; 文件存储介质--->内存数据
2: Unity序列化:
(1) 把要序列化的对象声明称:
  [System.Serializable], using System; [Serializable]
(2) JsonUtility.toJson(object); // 将对象转成json字符串;
(3) 将Json字符串的数据解析到对象 JsonUtility.FromJsonOverwrite(json_str, obj);
(4) 当Unity序列化你的脚本的时候,它将仅仅序列化公有域。如果作为附加你也想要Unity去序列化你的一个私有域,你可以添加SerializeField(序列化域)属性给这个域。
(5) JSON数组:
(6) JSON对象:

3.很多游戏的数据,可以配到Jason文件里面,然后从文件里面读出来,很多时候互联网向服务器发送数据,我们也可以来发送Jason文本,然后再解析出数据类型

Jason序列化和反序列化实例

1.创建Unity项目和文件目录,保存场景

2.创建一个空节点GameObject,创建一个脚本jason_test挂载在GameObject下面

 打开jason_test

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

// 声明一个可序列化的对象
// 对这个声明可序列化的对象里面的public成员,做序列化
// [System.Serializable]
[Serializable]
class sub
{
    public string sub_name = "sub_name";
}

[Serializable]
class my_object
{
    public int a = 3;
    public int b = 4;
    public string age = "blake";

    // 不是public,又想序列化?
    [SerializeField] // 可序列化的字段
    bool is_male = true;

    public int[] int_array;//object里面定义数组

    public sub s;//object里面定义object
}

public class jason_test : MonoBehaviour {

    // Use this for initialization
    void Start () {
        // JSON
        //json序列化
        // (1) 使用[Serializable]标记要序列化的对象,是可序列化的
        my_object obj = new my_object();
        obj.int_array = new int[] { 1, 2, 3 };//初始化
        obj.s = new sub();//初始化

        // (2) 内存数据对象,序列化成json字符串
        string json_string = JsonUtility.ToJson(obj);//序列化成字符串
        Debug.Log(json_string); // 有了这个json_string字符串,就可以存储,传输


        // json反序列化
        my_object obj2 = new my_object();//obj2里面是混乱的初始值
        JsonUtility.FromJsonOverwrite(json_string, obj2);//把json_string字符串解析到obj2中
        Debug.Log(obj2.int_array[1]); // 输出2,说明反序列化成功,已经可以使用解析出来的数据了
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

3.运行结果

原文地址:https://www.cnblogs.com/HangZhe/p/7289831.html