Unity 序列化泛型多维数组

作者: 2b75747cf703 | 来源:发表于2017-01-20 15:04 被阅读230次

为了能够在Unity的监视面板拖动或者调整数据,Unity本身不支持多维数组的序列化,所以要做个包装。

2B青年:

    [Serializable]
    public class MultiDimensionalInt
    {
        public int[] intArray;
    }

    public MultiDimensionalInt[] myIntArray;

所有代码都要改动,到处要加.intArray,略忧伤。

普通青年:

using System;
using UnityEngine;

[Serializable]
public class IntArray
{
    public int this[int index]
    {
        get
        {
            return array[index];
        }
        set
        {
            array[index] = value;
        }
    }

    [SerializeField]
    private int[] array;
}

不用加.intArray,但是Length之类的方法没了,也要到处改代码。

文艺青年:

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

[Serializable]
public class Array<T> : IList<T>
{
    [SerializeField]
    private List<T> list;

    public Array()
    {
        list = new List<T>();
    }

    public Array(IEnumerable<T> collection)
    {
        list = new List<T>(collection);
    }

    public Array(int capacity)
    {
        list = new List<T>(capacity);
    }

    public T this[int index]
    {
        get
        {
            return list[index];
        }

        set
        {
            list[index] = value;
        }
    }

    public int Count
    {
        get
        {
            return list.Count;
        }
    }

    public bool IsReadOnly
    {
        get
        {
            return false;
        }
    }

    public void Add(T item)
    {
        list.Add(item);
    }

    public void Clear()
    {
        list.Clear();
    }

    public bool Contains(T item)
    {
        return list.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        list.CopyTo(array, arrayIndex);
    }

    public IEnumerator<T> GetEnumerator()
    {
        return list.GetEnumerator();
    }

    public int IndexOf(T item)
    {
        return list.IndexOf(item);
    }

    public void Insert(int index, T item)
    {
        list.Insert(index, item);
    }

    public bool Remove(T item)
    {
        return list.Remove(item);
    }

    public void RemoveAt(int index)
    {
        list.RemoveAt(index);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return list.GetEnumerator();
    }
}

#region 基础数据类型

[Serializable]
public class IntArray : Array<int>
{

}

[Serializable]
public class IntArray2D : Array<IntArray>
{

}

[Serializable]
public class IntArray3D : Array<IntArray2D>
{

}

[Serializable]
public class FloatArray : Array<float>
{

}

[Serializable]
public class FloatArray2D : Array<FloatArray>
{

}

[Serializable]
public class FloatArray3D : Array<FloatArray2D>
{

}

[Serializable]
public class StringArray : Array<string>
{

}

[Serializable]
public class StringArray2D : Array<StringArray>
{

}

[Serializable]
public class StringArray3D : Array<StringArray2D>
{

}

#endregion

#region Unity数据类型

[Serializable]
public class GameObjectArray : Array<GameObject>
{

}

[Serializable]
public class GameObjectArray2D : Array<GameObjectArray>
{

}

[Serializable]
public class GameObjectArray3D : Array<GameObjectArray2D>
{

}

[Serializable]
public class TransformArray : Array<Transform>
{

}

[Serializable]
public class TransformArray2D : Array<TransformArray>
{

}

[Serializable]
public class TransformArray3D : Array<TransformArray2D>
{

}

[Serializable]
public class Vector2Array : Array<Vector2>
{

}

[Serializable]
public class Vector2Array2D : Array<Vector2Array>
{

}

[Serializable]
public class Vector2Array3D : Array<Vector2Array2D>
{

}

[Serializable]
public class Vector3Array : Array<Vector3>
{

}

[Serializable]
public class Vector3Array2D : Array<Vector3Array>
{

}

[Serializable]
public class Vector3Array3D : Array<Vector3Array2D>
{

}

[Serializable]
public class Vector4Array : Array<Vector4>
{

}

[Serializable]
public class Vector4Array2D : Array<Vector4Array>
{

}

[Serializable]
public class Vector4Array3D : Array<Vector4Array2D>
{

}

[Serializable]
public class ColorArray : Array<Color>
{

}

[Serializable]
public class ColorArray2D : Array<ColorArray>
{

}

[Serializable]
public class ColorArray3D : Array<ColorArray2D>
{

}

#endregion

现在只要换个类名就行了。

相关文章

  • Unity 序列化泛型多维数组

    为了能够在Unity的监视面板拖动或者调整数据,Unity本身不支持多维数组的序列化,所以要做个包装。 2B青年:...

  • TypeScript04(数组类型)

    数组的类型 类型[ ] 数组泛型 规则 Array<类型> 用接口表示数组 一般用来描述类数组 多维数组 argu...

  • 实战技术

    第一课 泛型 泛型注意点:在数组中,一般用可变数组添加方法,泛型才会生效,如果使用不可变数组,添加元素,泛型没有效...

  • C语言 泛型动态数组

    泛型实现思路:万能指针void *动态数组实现思路:动态进行数组内存的扩容 realloc 泛型动态数组 数组可以...

  • TypeScript 泛型

    泛型函数 使用 数组 类 泛型约束

  • Android Studio 集成Unity 工程并相互调用

    新建Unity工程 unity泛型单例脚本 SingletonUnity.cs 调用Java脚本 MobPlugi...

  • V语言学习笔记-14泛型

    目前的泛型主要有这三种:泛型结构体,泛型函数,泛型方法 泛型结构体 泛型函数 判断2个数组是否相等的泛型函数 泛型方法

  • 4.泛型数组——GenericArrayType

    参考: 1. 泛型数组

  • 当反射遇上泛型该如何处理

    由于后面讲到的反序列化器在反序列化List的时候需要确定泛型的type,所以这边先讲一下针对类型擦除的泛型,我们要...

  • 泛型

    分别用几个demo说明一下泛型的用法。 1、 一个泛型变量 2、 两个泛型变量 3、 数组泛型 4、 集合泛型 5...

网友评论

本文标题:Unity 序列化泛型多维数组

本文链接:https://www.haomeiwen.com/subject/asgwbttx.html