- Home /
Extending unity UI Button problem
I have custom button that extends from UnityEngine.UI.Button, something like this:
using UnityEngine;
using UnityEngine.UI;
using System;
[Serializable]
public class MyButton : Button {
public float testFloat;
public Image testImage;
public Sprite testSprite;
}
Then my editor class:
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using UnityEditor.UI;
[CustomEditor(typeof(MyButton))]
[CanEditMultipleObjects]
public class MyButtonEditor : ButtonEditor {
SerializedProperty floatProp;
SerializedProperty imageProp;
SerializedProperty spriteProp;
public override void OnInspectorGUI()
{
serializedObject.Update();
floatProp = serializedObject.FindProperty("testFloat");
imageProp = serializedObject.FindProperty("testImage");
spriteProp = serializedObject.FindProperty("testSprite");
//Normal button options
base.OnInspectorGUI();
EditorGUILayout.PropertyField(floatProp, new GUIContent("Test Float"));
EditorGUILayout.PropertyField(imageProp, new GUIContent("Test Image"));
EditorGUILayout.PropertyField(spriteProp, new GUIContent("Test Sprite"));
serializedObject.ApplyModifiedProperties();
}
}
My problem is that this code works on a new/clean project but when I add it to my project I can't drag or set values to the class variables. Even the float variable, that I can change on the editor, when I reload the scene the value isn't saved. So basically they appear on the editor but for Image and Sprite I can't set values and float I can change the value but does nothing...
At first I was setting the values directly like:
component.testFloat = EditorGUILayout.FloatField("Test Float", component.testFloat);
component.testImage = (Image)EditorGUILayout.ObjectField("Test Image", component.testImage, typeof(Image), true);
component.testSprite = (Sprite)EditorGUILayout.ObjectField("Test Sprite", component.testSprite, typeof(Sprite), true);
Then I read that this will not work with prefabs and that is what I want.
The new/clean project is set to the same platform as my project. I spent a lot of time trying to figure why this doesn't work on the project that I want. I don't have any errors on console...
Anyone had a similar problem that can point me to a direction? My project has a considerable size so maybe is something that is creating a conflict but without any console error will be hard to know what.
Thanks!
Well after some sleep and a clear head I found the problem. Our project still has EZGui, we are updating to the new UI, and EZGui have a Sprite class. So I needed to reference the right Sprite (UnityEngine.Sprite) to work.
I discover this after finding that float and image did work using the SerializedProperty. It wasn't working when I was messing the values directly and since my objects are prefabs it didn't store any value.
Your answer
Follow this Question
Related Questions
Editor Targets are updated but SerializedPropertys arent 1 Answer
Invalid iteration - (You need to stop calling Next when it returns false) 2 Answers
How might I go about creating a custom window similar to that of the Log window? 0 Answers
Horizontal Scrollbar shows up when Vertical Scrollbar appears 2 Answers
Getting CustomPropertyDrawer from SerializedProperty 1 Answer