- Home /
Accessing color presets in c# script.
I have many hex color presets that I'm tweaking in the game to get some nice effects. Thing is, lots of my particles relay on script that sets their colors. At the moment I have to store each of the presets as hex constants which is really inefficient.
My question, Can I access the presets in C# script? Something like Presets.RedPreset etc etc ?
Answer by Ronnie_0 · Jan 03, 2018 at 12:24 AM
@Olly, looks like there is a class called ColorPresetLibrary
, but it's not accessible via code. However, you can work around that by using a SerializedObject
.
For this example, I have a color preset saved in my project at Assets/Editor/UI Colors.color
.
string colorPresetPath = "Assets/Editor/UI Colors.colors";
UnityEngine.Object presetObject = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(colorPresetPath);
SerializedObject so = new SerializedObject(presetObject);
SerializedProperty presets = so.FindProperty("m_Presets");
SerializedProperty firstElement= presets.GetArrayElementAtIndex(0);
Debug.Log(firstElement.FindPropertyRelative("m_Color").colorValue);
Just found this while trying to access my custom Curve Preset Library and was able to use your method with a slight tweak to get it working, cheers!
SerializedObject so = new SerializedObject(presetObject);
SerializedProperty presets = so.FindProperty("m_Presets");
SerializedProperty firstElement = presets.GetArrayElementAtIndex(0);
SerializedProperty newCurve = firstElement.FindPropertyRelative("m_Curve");
AnimationCurve curveSkirt = newCurve.animationCurveValue;
and then the usage:
newDynamBone.m_RadiusDistrib = curveSkirt;
The whole thing is pretty interesting, was previously trying to access values directly through the curve library but it stated the values were inaccessible, so serialization was the best method. With the animationCurveValue in SerializedProperty scripting docs makes that even more apparent.
But yea cheers again and will be playing with this more often :P