Make it easier to inherit from Button with custom properties for the editor
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 35/100
Línea de trabajo
Comienza con Editor/UGUI/UI/SelectableEditor.cs, especialmente ChildClassPropertiesGUI e IsDerivedSelectableEditor, y reproduce el caso de herencia con el ejemplo MyButton. Rastrea cómo se descubren OnClick de Button y las propiedades serializadas de la clase derivada; el trabajo estará terminado cuando aparezcan tanto las propiedades heredadas de Button como las propiedades de MyButton y sigan siendo editables sin un editor separado para cada subclase.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
I wanted to have a button that would behave like a sprite button for the background: one sprite by default, a different sprite when it's highlighted, and another for when it's pressed, but also have the ability to set the text dynamically.
To get this behavior I need to set text position dynamically for when button is in different states. Since there are no public events for when button is highlighted, selected, returns to default state, I need to either inherit from the Button class or copy paste the code and make my own class. I decided to inherit from the Button class:
[AddComponentMenu("UI/My Button", 30)]
public class MyButton : Button {
HorizontalLayoutGroup HorizontalLayoutGroup;
int NormalTextTopPadding;
int NormalTextBottomPadding;
[SerializeField] int HighlightedTextBottomPadding;
[SerializeField] int PressedTextBottomPadding;
void Awake() {
HorizontalLayoutGroup = GetComponent<HorizontalLayoutGroup>();
NormalTextTopPadding = HorizontalLayoutGroup.padding.top;
NormalTextBottomPadding = HorizontalLayoutGroup.padding.bottom;
}
protected override void DoStateTransition(SelectionState state, bool instant) {
base.DoStateTransition(state, instant);
if(state == SelectionState.Normal || state == SelectionState.Selected) {
HorizontalLayoutGroup.padding.top = NormalTextTopPadding;
HorizontalLayoutGroup.padding.bottom = NormalTextBottomPadding;
}
if(state == SelectionState.Highlighted) {
HorizontalLayoutGroup.padding.top = NormalTextTopPadding + HighlightedTextBottomPadding;
HorizontalLayoutGroup.padding.bottom = NormalTextBottomPadding - HighlightedTextBottomPadding;
}
if(state == SelectionState.Pressed) {
HorizontalLayoutGroup.padding.top = NormalTextTopPadding + HighlightedTextBottomPadding + PressedTextBottomPadding;
HorizontalLayoutGroup.padding.bottom = NormalTextBottomPadding - HighlightedTextBottomPadding - PressedTextBottomPadding;
}
}
}
This is the class. The first problem that I get is that the editor doesn't draw the properties HighlightedTextBottomPadding and PressedTextBottomPadding . I don't understand why, since in the SelectableEditor class it should draw all the child properties:
private void ChildClassPropertiesGUI()
{
if (IsDerivedSelectableEditor())
return;
DrawPropertiesExcluding(serializedObject, m_PropertyPathToExcludeForChildClasses);
}
So what do I do? I write my own editor to draw my properties:
[CustomEditor(typeof(MyButton), true)]
public class MyButtonEditor : SelectableEditor {
SerializedProperty HighlightedTextBottomPadding;
SerializedProperty PressedTextBottomPadding;
protected virtual void OnEnable() {
base.OnEnable();
HighlightedTextBottomPadding = serializedObject.FindProperty("HighlightedTextBottomPadding");
PressedTextBottomPadding = serializedObject.FindProperty("PressedTextBottomPadding");
}
public override void OnInspectorGUI() {
base.OnInspectorGUI();
EditorGUILayout.PropertyField(HighlightedTextBottomPadding);
EditorGUILayout.PropertyField(PressedTextBottomPadding);
serializedObject.ApplyModifiedProperties();
}
}
This starts to draw the MyButton properties but it stops drawing the original button OnClick property. That's because I now have a custom editor and IsDerivedSelectableEditor() returns false and ChildClassPropertiesGUI() doesn't draw child class properties. A naive thing to try is to add SerializedProperty OnClick; field to the editor and then find it on Enable: OnClick = serializedObject.FindProperty("m_onClick"); and then draw it: EditorGUILayout.PropertyField(OnClick);. Again, for some reason it doesn't work(FindProperty returns null), even considering the MyButton object does have m_onClick property: it inherits it from Button class.
So, how do I fix that problem? I have to write my own ButtonEditor class for Button and then inherit from that instead in my own MyButtonEditor:
[CustomEditor(typeof(Button), true)]
public class ButtonEditor : SelectableEditor {
SerializedProperty OnClick;
protected virtual void OnEnable() {
base.OnEnable();
OnClick = serializedObject.FindProperty("m_OnClick");
}
public override void OnInspectorGUI() {
base.OnInspectorGUI();
EditorGUILayout.PropertyField(OnClick);
serializedObject.ApplyModifiedProperties();
}
}
Now with all those changes I(finally) have my own button with the behavior I want. But it's too much code for something simple like that. It would be nice to just write MyButton class and be done with that. Not sure why SelectableEditor only iterates over Button properties but not properties of my child class.
- Lenguaje dominante
- C#
- Estrellas
- 1.6k
- Forks
- 324
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de Unity-Technologies/uGUI
-
> - [ ]Abierto
Dificultad 5/5 Más de una semana Aptitud para principiantes 1/100
Unity-Technologies/uGUI#22 ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 35/100
Unity-Technologies/uGUI#20 ·
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 35/100
Unity-Technologies/uGUI#17 ·
-
When clicking on a mobile platform, some gameobject will trigger OnEnter event without OnExit eventAbierto
Dificultad 3/5 1-2 días Aptitud para principiantes 35/100
Unity-Technologies/uGUI#16 ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 45/100
Unity-Technologies/uGUI#13 ·
Todos los issues de Unity-Technologies/uGUI
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
microsoft/fluentui-blazor#5364 ·
Los mantenedores suelen responder en 1 día
-
.NET triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
microsoft/agent-framework#8811 ·
Los mantenedores suelen responder en 1 día
-
.NET Docs
Dificultad 1/5 Menos de una hora Aptitud para principiantes 82/100
getsentry/sentry-dotnet#5637 · 1 comentario ·
Los mantenedores suelen responder en 2 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
QuantConnect/Lean#9842 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
NethermindEth/nethermind#14012 ·
Los mantenedores suelen responder en 1 día