I am creating a prefab for a stylized input, which proxies properties to the underlying game objects that make up the input parts. (Using NovaUI for some parts)
Here is my script for the UnderlinedTextInput
:
using Nova;
using UnityEngine;
public class UnderlinedTextInput : MonoBehaviour
{
[SerializeField]
public Color32 TextColor
{
get => _InputField.Color;
set => _InputField.Color = value;
}
[SerializeField]
public string Text
{
get => _InputField.Text;
set => _InputField.Text = value;
}
[SerializeField]
public Color32 PlaceholderColor
{
get => _Placeholder.Color;
set => _Placeholder.Color = value;
}
[SerializeField]
public string Placeholder
{
get => _Placeholder.Text;
set => _Placeholder.Text = value;
}
[SerializeField]
public Color32 LabelColor
{
get => _Label.Color;
set => _Label.Color = value;
}
[SerializeField]
public string Label
{
get => _Label.Text;
set => _Label.Text = value;
}
[SerializeField]
public Color32 UnderlineColor
{
get => _Underline.Color;
set => _Underline.Color = value;
}
[Header("Prefab Editing-Only")]
[SerializeField] private TextBlock _InputField;
[SerializeField] private TextBlock _Placeholder;
[SerializeField] private TextBlock _Label;
[SerializeField] private UIBlock2D _Underline;
}
And here is the Editor script:
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(UnderlinedTextInput))]
public class UnderlinedTextInputEditor : Editor
{
public override void OnInspectorGUI()
{
UnderlinedTextInput inputComponent = (UnderlinedTextInput)target;
bool isEditingPrefab = PrefabUtility.GetPrefabInstanceStatus(inputComponent) == PrefabInstanceStatus.NotAPrefab;
CheckForInputChanges(inputComponent);
DrawEditorFields(isEditingPrefab);
}
private void DrawEditorFields(bool isEditingPrefab)
{
EditorGUILayout.BeginVertical();
// only show all the properties if we are editing the prefab
if (isEditingPrefab)
{
// draw everything else
DrawDefaultInspector();
}
EditorGUILayout.EndVertical();
}
private void CheckForInputChanges(UnderlinedTextInput inputComponent)
{
EditorGUI.BeginChangeCheck();
string text = EditorGUILayout.TextField("Text", inputComponent.Text);
string placeholder = EditorGUILayout.TextField("Placeholder", inputComponent.Placeholder);
string label = EditorGUILayout.TextField("Label", inputComponent.Label);
Color32 labelColor = EditorGUILayout.ColorField("Label Color", inputComponent.LabelColor);
Color32 underlineColor = EditorGUILayout.ColorField("Underline Color", inputComponent.UnderlineColor);
Color32 placeholderColor = EditorGUILayout.ColorField("Placeholder Color", inputComponent.PlaceholderColor);
Color32 textColor = EditorGUILayout.ColorField("Text Color", inputComponent.TextColor);
if (EditorGUI.EndChangeCheck())
{
inputComponent.Text = text;
inputComponent.Placeholder = placeholder;
inputComponent.Label = label;
inputComponent.LabelColor = labelColor;
inputComponent.UnderlineColor = underlineColor;
inputComponent.PlaceholderColor = placeholderColor;
inputComponent.TextColor = textColor;
EditorUtility.SetDirty(inputComponent);
Undo.RecordObject(inputComponent, "Modify UnderlinedTextInput Properties");
}
}
private void DrawPrefabInstanceInspector(UnderlinedTextInput inputComponent)
{
EditorGUILayout.ColorField("Label Color", inputComponent.LabelColor);
EditorGUILayout.TextField("Label", inputComponent.Label);
EditorGUILayout.ColorField("Placeholder Color", inputComponent.PlaceholderColor);
EditorGUILayout.TextField("Placeholder", inputComponent.Placeholder);
EditorGUILayout.ColorField("Text Color", inputComponent.TextColor);
EditorGUILayout.TextField("Text", inputComponent.Text);
EditorGUILayout.ColorField("Underline Color", inputComponent.UnderlineColor);
}
}
I’m fairly new to Unity, but my guess is that I’m not properly marking the instantiated prefab as dirty. That, or something about using getter/setters for Inspector fields doesn’t allow Unity to save those values? Anyways, is there something I can change to make this work, or is this not a supported workflow?
Thank you in advance!