Unity Doesnt Serialize int? field - unity3d

I have a class i want to change the properties of in the editor. So i made my class System.Serializable and made the variables public that i want to be able to change.
Like so:
[System.Serializable]
public class UIOptionsRing
{
public float Radius, DistanceBetweenPoints, StartOffset, GapInDegrees;
public int? GapAfterElementNumer = 3; //this var doesnt show up
public Vector3 CircleCenter;
public GameObject CircleElementsContainer;
}
But the problem i am having is that the GapAfterElementNumer is not show up in the editor at all the other fields are. How i can i make it so that int? also shows up?

Nullable types are not serialized in Unity Editor because it's serializer doesn't support null.
There's a small workaround if you're not going to serialize this class to json using JsonUtility.
The key idea is that you have to create your own nullable int. Something like
public class IntNullable
{
public int Value;
public bool HasValue;
}
Just like it's done inside .NET. Then you can create a Custom Editor for IntNullable or your UIOptionsRing. In this editor you can make a filed for int value and a button "Set Null", which will change the value of HasValue variable. And further you need to work with this custom IntNullable in your code.

Unity not only can't show nullable fields in the inspector, it cannot serialize them. In order to support this we need to make a custom version of System.Nullable (as #vmchar explains) that is serializable and then give it a property drawer. Making a seamless replacement for System.Nullable is not necessarily obvious, so I've included this example. It should be a drop in replacement for nullable ( int? can be replaced with SN<int> and all else should work due to the implicit casts) along with a basic custom property drawer.
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
/// <summary>
/// Serializable Nullable (SN) Does the same as C# System.Nullable, except it's an ordinary
/// serializable struct, allowing unity to serialize it and show it in the inspector.
/// </summary>
[System.Serializable]
public struct SN<T> where T : struct {
public T Value { get {
if (!HasValue)
throw new System.InvalidOperationException("Serializable nullable object must have a value.");
return v;
} }
public bool HasValue { get { return hasValue; } }
[SerializeField]
private T v;
[SerializeField]
private bool hasValue;
public SN(bool hasValue, T v) {
this.v = v;
this.hasValue = hasValue;
}
private SN(T v) {
this.v = v;
this.hasValue = true;
}
public static implicit operator SN<T>(T value) {
return new SN<T>(value);
}
public static implicit operator SN<T>(System.Nullable<T> value) {
return value.HasValue ? new SN<T>(value.Value) : new SN<T>();
}
public static implicit operator System.Nullable<T>(SN<T> value) {
return value.HasValue ? (T?)value.Value : null;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(SN<>))]
internal class SNDrawer : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
EditorGUI.BeginProperty(position, label, property);
// Draw label
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
// Don't make child fields be indented
var indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
// Calculate rects
var setRect = new Rect(position.x, position.y, 15, position.height);
var consumed = setRect.width + 5;
var valueRect = new Rect(position.x + consumed, position.y, position.width - consumed, position.height);
// Draw fields - pass GUIContent.none to each so they are drawn without labels
var hasValueProp = property.FindPropertyRelative("hasValue");
EditorGUI.PropertyField(setRect, hasValueProp, GUIContent.none);
bool guiEnabled = GUI.enabled;
GUI.enabled = guiEnabled && hasValueProp.boolValue;
EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("v"), GUIContent.none);
GUI.enabled = guiEnabled;
// Set indent back to what it was
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}
}
#endif
It's performance may not be on par with System.Nullable, but it should be fine for most purposes. It's been serving me well so far in Unity 2021.1 with C# 4 enabled.

An improvement on vmchar's answer, which allows null assignment:
[Serializable]
public struct NullableInt
{
public int Value;
public bool HasValue;
public NullableInt(int value)
{
Value = value;
HasValue = true;
}
public static implicit operator NullableInt(int value) => new NullableInt(value);
public static implicit operator NullableInt(NullableNull value) => new NullableInt();
public static implicit operator int(NullableInt value) => value.Value;
public static implicit operator int? (NullableInt value) => value.HasValue ? value.Value : new int?();
}
public sealed class NullableNull
{
private NullableNull()
{ }
}

Related

Serialisation not working when inheriting from monobehaviour

Im trying to do a property drawer for a class that i need to be updated as well as editable from the editor.
If i make the class a monobehaviour the serialisation stops working, but if i remove the monobehaviour inheritance it wont update with the game loop.
Is there any way to have both? I would need the object to be able to instantiate with default (empty) values if a monobehaviour script has non instantiated reference.
[Serializable]
public class MySmallTestProp : MonoBehaviour, ISerializationCallbackReceiver
{
[SerializeField]
private string name;
[SerializeField]
private string _name;
[SerializeField]
private float _someFloat;
public float someFloat;
public MySmallTestProp()
{ }
public void OnBeforeSerialize()
{
_name = name;
}
public void OnAfterDeserialize()
{
name = _name;
}
}
[CustomPropertyDrawer(typeof(MySmallTestProp))]
public class MySmallTestPropPropertyDrawer : PropertyDrawer
{
float rowHeight;
int rowSpacing = 5;
int index;
Rect currentPosition;
public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
{
rowHeight = base.GetPropertyHeight(prop, label);
var rows = 2;
if (Application.isPlaying)
{
rows++;
}
else
{
rows++;
}
return rowHeight * rows;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
index = 1;
currentPosition = position;
EditorGUI.BeginProperty(position, label, property);
var nameProp = property.FindPropertyRelative("_name");
EditorGUI.PropertyField(NextPosition(), nameProp, new GUIContent("Name"));
EditorGUI.EndProperty();
}
Rect NextPosition()
{
currentPosition.height = rowHeight;
currentPosition.y = rowSpacing + (rowHeight + rowSpacing) * index++;
return currentPosition;
}
}
So if I understand you correctly what you want to achieve is having a class which is
Serializable
has some default field values
Receives an Update call every frame
Actually I don't think you need any custom property drawer for this.
First two points are as simple as having e.g.
[Serializable]
public class Example
{
// By default this has the value "Default String"
public string someString = "Default String";
// This can be edited only via the Inspector
// by default it is 42
[SerializeField] private float someFloat = 42.0f;
// This is a read-only public access
public float SomeFloat => someFloat;
}
Now to the last and tricky part - the update calls.
The easiest way is to have a dedicated MonoBehaviour like e.g.
public class UpdateDispatcher : MonoBehaviour
{
// Storing the instance for Singleton Pattern
private static UpdateDispatcher _instance;
// Register to this event to receive one call each Update
public static event Action OnUpdate;
// This method is automatically called by Unity when the application is started
// or you enter play mode in the editor
[RuntimeInitializeOnLoadMethod]
private static void Init()
{
// _instsnce is already assigned and alive?
if(_instance) return;
// Otherwise search for one in the scene
_instance = FindObjectOfType<UpdateDispatcher>();
// Found one?
if(_instance) return;
// Otherwise create it now
_instance = new GameObject(nameof(UpdateDispatcher)).AddComponent<UpdateDispatcher>();
}
private void Awake ()
{
// Does another instance already exist?
if(_instance && _instance != this)
{
// Destroy this one
Destroy (gameObject);
return;
}
// Otherwise assign this as the instance and make sure it isn't destroyed when the scene chsnges
_instance = this;
DontDestroyOnLoad (gameObject);
}
private void Update ()
{
// Call the event every frame if something is registered
OnUpdate?.Invoke ();
}
}
And then you can use ISerislizationCallbackReceiver but not for actually doing the serialization (it is already done automatically for the fields) but rather for registration to the update callback like e.g.
[Serializable]
public class Example, ISerializationCallbackReceiver
{
// By default this has the value "Default String"
public string someString = "Default String";
// This can be edited only vis the Inspector
// by default it is 42
[SerializeField] private float someFloat = 42.0f;
// This is a read-only public access
public float SomeFloat => someFloat;
// Nothing to do here, only needed for the interface
public void OnBeforeSerialize() { }
public void OnAfterDeserialize()
{
// Register to the Update event
// It is save to unregister before registering even if we haven't been registered before
// this makes sure we are registered only exactly once
UpdateDispatcher.OnUpdate -= Update;
UpdateDispatcher.OnUpdate += Update;
}
private void Update ()
{
someFloat += Time.deltaTime;
}
}
This answer should be more like a comment, but due to the extension I've decided to post it here.
The objective of a PropertyDrawer is to display properties differently on the editor.
To achieve that you need 2 things:
1.One class that inherits from PropertyAttribute, this will be the reference used in your future scripts.
2.Another class that inherits from PropertyDrawer, here you can type HOW to display the attribute.
One implementation example of a property drawer that shows an attribute without leting the user to edit it from editor:
using UnityEditor;
using UnityEngine;
public class DisplayWithoutEdit : PropertyAttribute
{
}
[CustomPropertyDrawer(typeof(DisplayWithoutEdit))]
public class DisplayWithoutEditDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label, true);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
GUI.enabled = false;
EditorGUI.PropertyField(position, property, label, true);
GUI.enabled = true;
}
}
Then you can use it on another script doing something like:
[DisplayWithoutEdit] public float randomNumber = 0f;

How can I create foldout EditorGUI element with toggle in Unity Inspector

Need your help.
Easily create a foldout element with toggle list. Like that
But I need to create foldout element with toggle in a header. Like that
I think it's possible because scripts header already have this
I tried to find the answer here but didn't find anything like it.
Thank you for help
You can override how your Serializable class is rendered using EditorGUI by creating a custom PropertyAttribute and PropertyDrawer.
Example
I cooked up an attribute that takes a boolean field (specified by you) and instead of rendering it as usual, it renders it as a checkbox at the top. You can have any number of boolean fields inside your class, they should render just fine.
This implementation renders only boolean fields. If you wish to render other kind of stuff besides that, feel free to extend this solution.
Implementation
using UnityEngine;
public class ToggleListAttribute : PropertyAttribute
{
public string StatusPropertyName { get; private set; }
public ToggleListAttribute(string statusPropertyName)
{
StatusPropertyName = statusPropertyName;
}
}
using System;
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(ToggleListAttribute))]
public class ToggleListDrawer : PropertyDrawer
{
private bool show;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var statusProperty = GetStatusPropertyFrom(property);
var foldoutRect = GetLinePositionFrom(position, 1);
show = EditorGUI.Foldout(
foldoutRect,
show,
string.Empty,
false);
statusProperty.boolValue = EditorGUI.ToggleLeft(
foldoutRect,
property.displayName,
statusProperty.boolValue);
if (show)
RenderSubproperties(property, position);
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (show)
return EditorGUIUtility.singleLineHeight * (GetBooleanPropertyCount(property) + 1);
else
return EditorGUIUtility.singleLineHeight;
}
private SerializedProperty GetStatusPropertyFrom(SerializedProperty property)
{
var listAttribute = attribute as ToggleListAttribute;
var statusProperty = property.FindPropertyRelative(
listAttribute.StatusPropertyName);
if (statusProperty == null)
throw new Exception($"No property named \"{listAttribute.StatusPropertyName}\" found!");
return statusProperty;
}
private void RenderSubproperties(SerializedProperty property, Rect position)
{
var innerPosition = new Rect(
position.x + EditorGUIUtility.standardVerticalSpacing * 4,
position.y,
position.width,
position.height);
var statusProperty = GetStatusPropertyFrom(property);
int line = 2;
foreach (var instance in property)
{
var subproperty = instance as SerializedProperty;
if (subproperty.propertyType != SerializedPropertyType.Boolean ||
subproperty.name == statusProperty.name)
{
continue;
}
subproperty.boolValue = EditorGUI.ToggleLeft(
GetLinePositionFrom(innerPosition, line),
subproperty.displayName,
subproperty.boolValue);
line++;
}
}
private int GetBooleanPropertyCount(SerializedProperty property)
{
int count = 0;
foreach (var instance in property)
{
var subproperty = instance as SerializedProperty;
if (subproperty.propertyType != SerializedPropertyType.Boolean)
continue;
count++;
}
return count - 1;
}
private Rect GetLinePositionFrom(Rect rect, int line)
{
float heightModifier = EditorGUIUtility.singleLineHeight * (line - 1);
return new Rect(
rect.x,
rect.y + heightModifier,
rect.width,
EditorGUIUtility.singleLineHeight);
}
}
Usage
using System;
using UnityEngine;
public class Example : MonoBehaviour
{
[ToggleList("enabled")]
public RenderList list1;
[ToggleList("enabled")]
public RenderList2 list2;
}
[Serializable]
public class RenderList
{
public bool enabled;
public bool floor;
public bool car;
public bool train;
}
[Serializable]
public class RenderList2
{
public bool enabled;
public bool one;
public bool two;
public bool three;
public bool four;
public bool five;
public bool six;
public bool seven;
}
Use EditorGUILayout.InspectorTitlebar(foldout: foldout, editor: targetEditor);

How can I build a unity UI component that is reusable across scenes

I want to build a UI modal in Unity that can be used across multiple scenes, how do I achieve this and later on probably build it as a stand-alone library people can use in their unity projects. Are there tutorials on this?
Explore this repo. It's usable, but experimental for now. UIElements are becoming a new standard for Unity, so there will be a preview package in a few weeks/month.
If you want to use current UI, you can create separate scenes for each reusable window and open them additive, passing parameters and callbacks to static methods, see my example
Line
public class ResultLineEntity {
public int Place;
public readonly string Title;
public readonly TimeSpan Time;
public ResultLineEntity(string title, TimeSpan time, int? place = null) {
if (place != null) Place = place.Value;
Title = title;
Time = time;
}
}
Window
public class ResultsUi : MonoBehaviour {
public static ResultLineEntity[] Lines
{
get => _lines;
set
{
_lines = value;
OnLinesChange?.Invoke();
}
}
private static ResultLineEntity[] _lines;
public static UnityEvent OnLinesChange { get; private set; } = new UnityEvent();
[SerializeField] private Transform root;
[SerializeField] private ResultLineUi prefab;
private void Awake() {
SetLines();
OnLinesChange?.AddListener(SetLines);
}
public void SetLines() {
if (Lines==null || Lines.Length < 1) return;
foreach (var child in root.GetComponentsInChildren<ResultLineUi>()) {
Destroy(child.gameObject);
}
var resultsOrdered = Lines.OrderBy(x => x.Time).ToArray();
for (var index = 0; index < resultsOrdered.Length; index++) {
var line = resultsOrdered[index];
line.Place = index + 1;
var currentLine = Instantiate(prefab, root);
currentLine.SetLine(line);
}
}
}
Example
ResultsUi.Lines = lines;
SceneManager.LoadScene(ResultsSceneName, LoadSceneMode.Additive);

unity conditional fields custom editor

my goal:
i have a script
public class MyScript: MonoBehaviour
{
public bool A;
public bool B;
}
I need B to be visible only if A is TRUE
i'de done an extention to the script and added UnityEditor in the title
[CustomEditor(typeof(MyScript))]
public class MyEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
MyScript tool = (MyScript) target;
tool.A = GUILayout.Toggle(tool.A, "Flag");
if(tool.A)
{
tool.B= EditorGUILayout.Toggle(tool.B, "Flag");
}
}
}
but nothing really changed.
what did i do wrong?
First of all your class definition is wrong. You either need [Serializable] or the class should inherit from MonoBehaviour if it shall be attached to a GameObject. Either way remove the ()
[Serializable]
public class MyScript
{
public bool A;
public bool B;
}
or
public class MyScript : MonoBehaviour
{
public bool A;
public bool B;
}
Then note that a Custom Editor is only for classes inherting from either a MonoBehaviour or a ScriptableObject. In other cases you will rather have to implement a Custom PropertyDrawer.
You should always try to not directly make changes in the target. You would have to handle a lot of things like marking as dirty, undo/redo etc by yourself...
Rather always go through SerializedPropertys.
Also note that base.OnInspectorGUI(); will draw the default inspector
So assuming MyScript is a MonoBehaviour class
[CustomEditor(typeof(MyScript))]
public class MyEditor : Editor
{
SerializedProperty a;
SerializedProperty b;
// is called once when according object gains focus in the hierachy
private void OnEnable()
{
// link serialized properties to the target's fields
// more efficient doing this only once
a = serializedObject.FindProperty("A");
b = serializedObject.FindProperty("B");
}
public override void OnInspectorGUI()
{
// fetch current values from the real instance into the serialized "clone"
serializedObject.Update();
// Draw field for A
EditorGUILayout.PropertyField(a);
if(a.boolValue)
{
// Draw field for B
EditorGUILayout.PropertyField(b);
}
// write back serialized values to the real instance
// automatically handles all marking dirty and undo/redo
serializedObject.ApplyModifiedProperties();
}
}
Or if MyScript is actually not a MonoBehaviour then as PropertyDrawer which works basically very similar except you have to use the EditorGUI versions of the fields always requiring a position Rect as parameter:
[CustomPropertyDrawer(typeof(MyScript), true)]
public class MyEditor : PropertyDrawer
{
private bool isUnFolded;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// draw folder for the entire class
isUnFolded = EditorGUI.Foldout(new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight), isUnFolded, label);
// go to the next line
position.y += EditorGUIUtility.singleLineHeight;
// only draw the rest if unfolded
if (isUnFolded)
{
// draw fields indented
EditorGUI.indentLevel++;
// similar to before get the according serialized properties for the fields
var a = property.FindPropertyRelative("A");
var b = property.FindPropertyRelative("B");
// Draw A field
EditorGUI.PropertyField(new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight), a);
position.y += EditorGUIUtility.singleLineHeight;
if (a.boolValue)
{
// Draw B field
EditorGUI.PropertyField(new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight), b);
}
// reset indentation
EditorGUI.indentLevel--;
}
}
// IMPORTANT you have to implement this since your new property is
// higher then 1 single line
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
// default is 1 single line
var height = 1;
// if unfolded at least 1 line more, if a is true 2 lines more
if(isUnFolded) height += (property.FindPropertyRelative("A").boolValue ? 2 : 1);
return height * EditorGUIUtility.singleLineHeight;
}
}

How to display & modify array in the Editor Window?

I have GameObject array field in my CustomEditor class derived from the UnityEngine.Editor. I need to be able to display (draw) and give user ability to modify that array.
Just like how Unity's Inspector doing this for Serializable fields of objects derived from the ScriptableObject. E.g. displaying materials array in the inspector:
Refer to yours editor object as to the SerializedObject and then find any required property, draw it, and apply modification:
public class MyEditorWindow : EditorWindow
{
[MenuItem("Window/My Editor Window")]
public static void ShowWindow()
{
GetWindow<MyEditorWindow>();
}
public string[] Strings = { "Larry", "Curly", "Moe" };
void OnGUI()
{
// "target" can be any class derived from ScriptableObject
// (could be EditorWindow, MonoBehaviour, etc)
ScriptableObject target = this;
SerializedObject so = new SerializedObject(target);
SerializedProperty stringsProperty = so.FindProperty("Strings");
EditorGUILayout.PropertyField(stringsProperty, true); // True means show children
so.ApplyModifiedProperties(); // Remember to apply modified properties
}
}
Original answer here.
I managed to do it with a for loop and an extra int:
using UnityEditor;
using UnityEngine;
public class RockSpawner : EditorWindow
{
int rockCollectionSize = 0;
GameObject[] rockCollection;
[MenuItem("Tools/Rock Spawner")]
public static void ShowWindow()
{
GetWindow(typeof(RockSpawner));
}
void OnGUI()
{
rockCollectionSize = EditorGUILayout.IntField("Rock collection size", rockCollectionSize);
if (rockCollection != null && rockCollectionSize != rockCollection.Length)
rockCollection = new GameObject[rockCollectionSize];
for (int i = 0; i < rockCollectionSize; i++)
{
rockCollection[i] = EditorGUILayout.ObjectField("Rock " + i.ToString(), rockCollection[i], typeof(GameObject), false) as GameObject;
}
}
}
The top answer didn't work well for me because the properties did not update, there for I moved the declaration lines to the OnEnable function you do not want to declare them over and over again) and used to so.Update() to update the changed variables.
using UnityEngine;
using UnityEditor;
public class MyEditorWindow : EditorWindow
{
[MenuItem("Window/My Editor Window")]
public static void ShowWindow()
{
GetWindow<MyEditorWindow>();
}
public string[] Strings = { "Larry", "Curly", "Moe" };
SerializedObject so;
private void OnEnable()
{
ScriptableObject target = this;
so = new SerializedObject(target);
}
void OnGUI()
{
// "target" can be any class derived from ScriptableObject
// (could be EditorWindow, MonoBehaviour, etc)
so.Update();
SerializedProperty stringsProperty = so.FindProperty("Strings");
EditorGUILayout.PropertyField(stringsProperty, true); // True means show children
so.ApplyModifiedProperties(); // Remember to apply modified properties
}
}
Links:
Base for my answer
SerializedObject.Update
public GameObject[] yourGameObjects;
Then in the inspector set your size, and fields should open up.
add a public array to your script
public GameObject[] myObjects