Problem is not reproducible or outdated
EditorGUILayout.FloatField return strange value when it is repaired elsewhere
public class StatBase
{
[SerializeField] private float _baseValue;
public virtual float BaseValue
{
get { return _baseValue; }
set
{
Debug.Log(value - _baseValue);
_baseValue = value;
}
}
}
public class StatAttribute : StatBase
{
[SerializeField] private int _statLinkerValue;
public override float BaseValue
{
get
{
if (base.BaseValue + StatLinkerValue < MinValue)
return MinValue;
else if (base.BaseValue + StatLinkerValue > MaxValue)
return MaxValue;
else
return base.BaseValue + StatLinkerValue;
}
}
}
public class EntityEditor : Editor
{
public override void OnInspectorGUI()
{
showBase = EditorGUILayout.Toggle("Show Base Value", showBase);
if (showBase)
{
stat.BaseValue = EditorGUILayout.FloatField("Base Value",
stat.BaseValue);
GUILayout.Space(10);
}
}
}
stat variable is a StatAttribute, They have a method that updates the value of StatLinkerValue that I will call from the editor (Update button). Everything works fine when I click the Update button, StatLinkerValue varies from 0 to 100, but somehow the editor has repeatedly added the _baseValue 100 until it equals MaxValue. This error will not occur if I do not present the BaseValue in the Editor, so I added a toggle to turn it off before performing the update.Everything went well, but when I turned on Toggle again, _baseValue immediately increased. I have spent quite a long time without understanding what is happening, I have not assigned _baseValue to any form other than stat.BaseValue = EditorGUILayout.FloatField ("Base Value", stat.BaseValue); Inside Editor. Finally sorry for my english.
Answer by haruna9x · Jul 12, 2017 at 11:51 AM
public class BaseTest
{
float _baseValue;
public virtual float BaseValue
{
get { return _baseValue; }
set { _baseValue = value; }
}
}
public class AttributeTest : BaseTest
{
float _linkerValue;
public float LinkerValue
{
get { return _linkerValue; }
set { _linkerValue = value; }
}
public override float BaseValue
{
get { return base.BaseValue + LinkerValue; }
}
public void UpdateLinker()
{
_linkerValue += 10;
}
}
public class StatTest : MonoBehaviour
{
AttributeTest _attack;
public AttributeTest Attack
{
get { return _attack; }
set { _attack = value; }
}
public StatTest()
{
Attack = new AttributeTest();
}
}
[CustomEditor(typeof(StatTest))]
public class EditTest : Editor
{
StatTest data;
private void OnEnable()
{
data = target as StatTest;
}
public override void OnInspectorGUI()
{
data.Attack.BaseValue = EditorGUILayout.FloatField("Base Value", data.Attack.BaseValue);
data.Attack.LinkerValue = EditorGUILayout.FloatField("Linker Value", data.Attack.LinkerValue);
if (GUILayout.Button("Update"))
{
data.Attack.UpdateLinker();
}
}
}
Seems in the end I found out the reason. An error will occur whenever I let the BaseValue and Linker Value at the same time as in the above example, Editor has mistakenly something with BaseValue override.
Follow this Question
Related Questions
How do I use EditorGUILayout.EnumPopup with an enum with 'holes' in a custom inspector. 3 Answers
Unity automatically resets my gameview scale back to 1. How do I stop this? 0 Answers
How can I display a scriptable object in a custom editor ? 1 Answer
EditorGUILayout.EnumPopup shows always the same selected option but works as programmed 1 Answer
Adding space() to BeginHorizontal() causes extra vertical spacing after certain amount. 1 Answer