UnityEditor SerializedProperty 数组储存

在给 SerializedProperty 赋值的时候发现没有数组类型,就查了一些资料发现了储存的方法,这里只写了一点,可自行扩展

 

public static class SerializedPropertyExtensions
{

    private static void SetArraySize<T>(SerializedProperty arrayProperty, T[] values)
    {
        if (arrayProperty == null)
            throw new ArgumentNullException(nameof(arrayProperty));
        if (values == null)
            throw new ArgumentNullException(nameof(values));

        arrayProperty.arraySize = values.Length;
    }
    public static void SetArray(this SerializedProperty arrayProperty, GameObject[] values)
    {
        SetArraySize(arrayProperty, values);

        for (int i = 0; i < values.Length; i++)
        {
            arrayProperty.GetArrayElementAtIndex(i).objectReferenceValue = values[i];
        }
    }
    public static void SetArray(this SerializedProperty arrayProperty, int[] values)
    {
        SetArraySize(arrayProperty, values);

        for (int i = 0; i < values.Length; i++)
        {
            arrayProperty.GetArrayElementAtIndex(i).intValue = values[i];
        }
    }
    public static void SetArray(this SerializedProperty arrayProperty, float[] values)
    {
        SetArraySize(arrayProperty, values);

        for (int i = 0; i < values.Length; i++)
        {
            arrayProperty.GetArrayElementAtIndex(i).floatValue = values[i];
        }
    }
    public static void SetArray(this SerializedProperty arrayProperty, double[] values)
    {
        SetArraySize(arrayProperty, values);

        for (int i = 0; i < values.Length; i++)
        {
            arrayProperty.GetArrayElementAtIndex(i).doubleValue = values[i];
        }
    }
    public static void SetArray(this SerializedProperty arrayProperty, string[] values)
    {
        SetArraySize(arrayProperty, values);

        for (int i = 0; i < values.Length; i++)
        {
            arrayProperty.GetArrayElementAtIndex(i).stringValue = values[i];
        }
    }
    public static void SetArray(this SerializedProperty arrayProperty, Color[] values)
    {
        SetArraySize(arrayProperty, values);

        for (int i = 0; i < values.Length; i++)
        {
            arrayProperty.GetArrayElementAtIndex(i).colorValue = values[i];
        }
    }
}

 

posted @ 2024-07-10 10:20  OldWu  阅读(1)  评论(0编辑  收藏  举报