update CCK to 3.10, fixing unity 2021 crash :)

This commit is contained in:
Crispy 2024-08-03 22:24:42 +02:00
parent 48a978fa2a
commit d11e0fb3a9
492 changed files with 2165204 additions and 437687 deletions

View file

@ -0,0 +1,78 @@
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace ABI.CCK.Scripts.Editor.Tools
{
public static class SerializedPropertyExtensions
{
public static SerializedProperty AddWithDefaults<T>(this SerializedProperty prop) where T : new()
{
prop.arraySize++;
SerializedProperty newArrayElement = prop.GetArrayElementAtIndex(prop.arraySize - 1);
T defaultInstance = new();
Type type = typeof(T);
foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.Instance))
{
SerializedProperty property = newArrayElement.FindPropertyRelative(field.Name);
if (property == null) continue;
object defaultValue = field.GetValue(defaultInstance);
if (defaultValue == null) continue;
if (field.FieldType == typeof(float))
{
property.floatValue = (float)defaultValue;
}
else if (field.FieldType == typeof(int)
|| field.FieldType.IsEnum) // dont use enumValueIndex here
{
property.intValue = (int)defaultValue;
}
// todo: add more types here if needed, mainly enum seemed to not respect default
}
return newArrayElement;
}
public static SerializedProperty CreateFromInstance<T>(this SerializedProperty prop, T instance)
{
prop.arraySize++;
SerializedProperty newArrayElement = prop.GetArrayElementAtIndex(prop.arraySize - 1);
Type type = typeof(T);
foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.Instance))
{
SerializedProperty property = newArrayElement.FindPropertyRelative(field.Name);
if (property == null) continue;
object value = field.GetValue(instance);
if (value == null) continue;
if (field.FieldType == typeof(float))
{
property.floatValue = (float)value;
}
else if (field.FieldType == typeof(int)
|| field.FieldType.IsEnum)
{
property.intValue = (int)value;
}
else if (field.FieldType == typeof(bool))
{
property.boolValue = (bool)value;
}
else if (field.FieldType == typeof(string))
{
property.stringValue = (string)value;
}
}
return newArrayElement;
}
}
}