SerializedProperty avoiding getter? - unity3d

I am using getters and setters for variables to avoid checking if they are null. Also I don't need Start() and Awake() functions in most cases.
public Button[] TitlebarButtons
{
get { return titlebar_buttons ?? (titlebar_buttons = GetComponentsInChildren<Button>()); }
}
private Button[] titlebar_buttons;
public Color CloseButtonNormalColor
{
get { return TitlebarButtons[2].colors.normalColor; }
set
{
ColorBlock temp = ColorBlock.defaultColorBlock;
temp.normalColor = value;
temp.highlightedColor = TitlebarButtons[2].colors.highlightedColor;
temp.pressedColor = TitlebarButtons[2].colors.pressedColor;
TitlebarButtons[2].colors = temp;
}
}
And in Editor script I am trying to make CloseButtonNormalColor a Serialized Property:
private Title Title;
private SerializedProperty close_button_normal_color;
void OnEnable()
{
Title = (Title)target;
close_button_normal_color = serializedObject.FindProperty("CloseButtonNormalColor");
}
But when I'm using it, NullReferenceExeption appears.
close_button_normal_color.colorValue = EditorGUILayout.ColorField("Normal", close_button_normal_color.colorValue);
Is there any solution for that?

Usually to just exposes properties on inspector you can use
[ShowProperty]
public MyProperty {get; set;}
and fields
[SerializeField]
private myField;

i found two "workarounds" for this.
One come from this thread in unity forum:
https://forum.unity.com/threads/using-property-fields-in-custom-editor-when-using-auto-properties.828597/
So, you can:
public class ClassToCustomEditor
{
[field:SerializeField] public int CustomEditorProperty { get; set; }
}
and then in your custom editor script:
void OnEnable()
{
IsCraftable = serializedObject.FindProperty(
string.Format("<{0}>k__BackingField", "IsCraftable")
);
}
for me, in Unity 2020.1.14f1 works like a charm. If you need more details, i suggest you to visit the thread above.
The other work around is create private fields (whose FindProperty find) and then create propeties who access and modify these fields.
Something like:
public class MyClassToCustomEditor
{
[SerializeField] private string itemName;
public string ItemName
{
get { return itemName; }
set { itemName = value; }
}
}
and then the FindProperty would be able to find it right the way you write it.

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 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);

how check if ScrollRect.OnDrag is active like boolean?

I'm trying to access my scrollrect, to know whether or not the component's drag is used, but the on-drag method is a function as the documentation says, some idea of ​​how to access ondrag like:
"if ondrag is active ....."
See ScrollRect.OnBeginDrag and ScrollRect.OnEndDrag
You can simply use the interfaces IBeginDragHandler and IEndDragHandler on your own component and e.g. set a bool there
public class YourComponent : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
public bool isDrag { get; private set; }
public void OnBeginDrag()
{
isDrag = true;
}
public void OnEndDrag()
{
isDrag = false;
}
}
and than check this bool instead
if(GetComponent<YourComponent>.isDrag) // ....

Interaction between two classes

I want to know the best way to "share" or have access to attributes between 2 classes like this :
class A {
public A() {
B myClassB = new B();
}
int attributeA;
}
Class B {
int foo() {
// I want to have something like : return attributeA;
}
}
I hope that it's clear. If someone has a better way to ask the question let me know 'cause I really don't know how.
Thanks.
I would say by encapsulation you could share the fields (attributes) between classes. It is a fundamental OOP concept which helps a programmer to modify the already implemented code without breaking the code of others who use the fields.
Consider the below example (Java)
public class Person{
public String name = "John";
public int age = 25;
}
Sharing Person class's attributes with another class
public class EncapTest {
public static void main(String[] args) {
Person p = new Person();
p.name = "Tom";
p.age = 20;
System.out.println(p.name);
System.out.println(p.age);
}
}
The above approach is bad way to share(access) attributes between classes because any one can change the fields because they are public. So when you change them, they get changed for everyone (even for those who didn't want). It is like you're breaking the code of others.
See the below example which uses encapsulation
public class Person{
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getName() {
return age;
}
}
Sharing Person class's attributes with another class
public class EncapTest {
public static void main(String[] args) {
Person p = new Person();
p.setName("Tom");
p.setAge(20)
System.out.println(p.getName());
System.out.println(p.getAge());
}
}
By making the attributes private and accessing them using the getters and setters methods, you are hiding the attributes with in the class. So whenever you set the attributes in another class, you're not breaking other programmers' code.

How to implement LeafValueEditor<Address>

I am trying to understand how to correctly implement a LeafValueEditor for a non immutable object. Which of the two way is correct, or should something else be used?
public class Address {
public String line1;
public String city;
public String zip;
}
Option 1:
public class AddressEditor implements LeafValueEditor<Address>
{
private String line1;
private String city;
private String zip;
private Address address;
public void setValue(Address value)
{
this.line1 = value.line1;
this.city = value.city;
this.zip = value.zip;
this.address = value;
}
public Address getValue()
{
this.address.line1 = this.line1;
this.address.city = this.city;
this.address.zip = this.zip;
return this.address;
}
}
Option 2:
public class AddressEditor implements LeafValueEditor<Address>
{
private String line1;
private String city;
private String zip;
public void setValue(Address value)
{
this.line1 = value.line1;
this.city = value.city;
this.zip = value.zip;
}
public Address getValue()
{
Address a = new Address();
this.a.line1 = this.line1;
this.a.city = this.city;
this.a.zip = this.zip;
return a;
}
}
Probably neither, though both technically could work.
A LeafValueEditor is an Editor for leaf values - that is, values that don't generally contain other values. Usually a text or date or number field that would be visible on the page is the leaf editor, and those leaf nodes are contained in a normal Editor.
In this case, it could look something like this:
public class AddressEditor extends Composite implements Editor<Address> {
// not private, fields must be visible for the driver to manipulate them
// automatically, could be package-protected, protected, or public
protected TextBox line1;//automatically maps to getLine1()/setLine1(String)
protected TextBox city;
protected TextBox zip;
public AddressEditor() {
//TODO build the fields, attach them to some parent, and
// initWidget with them
}
}
See http://www.gwtproject.org/doc/latest/DevGuideUiEditors.html#Editor_contract for more details on how this all comes together automatically with just that little wiring.