Different type for public and private representation

31 views
Skip to first unread message

jwvand...@gmail.com

unread,
Jun 4, 2017, 4:27:10 PM6/4/17
to Developer Support
Hi there,

I am trying to set up something where the private backing field is a string, but the public property seen by Unity is a Color. I am doing this so that I can store more compact hexadecimal strings on disk, but still work with pickable colors in the editor.

However the properties in the Unity Inspector still act like strings.


[SerializeField]
[Candlelight.PropertyBackingField("Background")]
private string background;


public Color GetBackground()
{
Color c;
if (ColorUtility.TryParseHtmlString(this.background, out c))
{
return c;
}
else
{
this.background = ColorUtility.ToHtmlStringRGBA(new Color(0, 0, 0, 0));
return new Color(0, 0, 0, 0);
}
}

public void SetBackground(Color value)
{
this.background = ColorUtility.ToHtmlStringRGBA(value);
}

Developer Support

unread,
Jun 5, 2017, 3:24:36 AM6/5/17
to Developer Support
Hi! Sorry the documentation wasn't clear enough here, but the tool will not change how your backing field is displayed in the Inspector, it will only route the input values through your property (whose type must match that of the backing field or be a more specific type than that specified by the backing field). If you want to serialize a string but display a color picker in the Inspector, you should make you own custom PropertyDrawer/PropertyAttribute
Reply all
Reply to author
Forward
0 new messages