ソースコード
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
internal static class Example
{
[MenuItem( "Example/Hoge" )]
private static void Hoge()
{
var colorPresetLibrary = AssetDatabase.LoadAssetAtPath<Object>( "XXXX.colors" );
var type = colorPresetLibrary.GetType();
var countMethod = type.GetMethod( "Count", BindingFlags.Public | BindingFlags.Instance );
var addMethod = type.GetMethod( "Add", BindingFlags.Public | BindingFlags.Instance );
var removeMethod = type.GetMethod( "Remove", BindingFlags.Public | BindingFlags.Instance );
while ( 0 < ( int )countMethod!.Invoke( colorPresetLibrary, Array.Empty<object>() ) )
{
removeMethod!.Invoke( colorPresetLibrary, new object[] { 0 } );
}
addMethod!.Invoke( colorPresetLibrary, new object[] { Color.red, "あか" } );
EditorUtility.SetDirty( colorPresetLibrary );
AssetDatabase.SaveAssetIfDirty( colorPresetLibrary );
}
}