- Home /
Serialization of Singleton vs Static class in Editor
When do I use Singleton and when should I use a static class when working with unityEditor? How easy is it to serialize data with each one?
Thank you
Answer by SamuZen · Oct 29, 2014 at 05:28 AM
At My Editor Scripts i use Static Class ... all public fields get serialized automatically and for private fields just use [SerializeField]
the question about Singleton Vs Static ... at my point of view, static i just use to hold string data ... and Singleton when my class need the functions ...
if is just for a bunch of String fields ... i say go for static, but if you need to pass data structure for List or Dictionary , you do at the initialization of the Singleton ...
using UnityEngine;
public class SomePerson : MonoBehaviour {
//This field gets serialized because it is public.
public string name = "John";
//This field does not get serialized because it is private.
private int age = 40;
//This field gets serialized even though it is private
//because it has the SerializeField attribute applied.
[SerializeField]
private bool hasHealthPotion = true;
void Update () {
}
}
try put [SerializeField] before the field ...
can you post some of your code ??
Static classes can contain only static members, public fields are not serialized.
Right, this answer is more than confusing. Static classes are never serialized by almost any serialization system. This includes Unity's serialization system.
The question was specifically about singletons and editor scripts but the example is neither a singleton nor an editor script.
Your answer
Follow this Question
Related Questions
do Static classes get serialized? 0 Answers
Unserialized private variable values from static instance persist when exiting play mode 1 Answer
Can a static class have an Update() function which Unity calls automatically? 1 Answer
Writing an extension method for a class. Is it possible? How? 3 Answers
Inpsector missing variables from a Serializable class? 1 Answer