- Home /
Custom inspector for specific variable type to be reused?
Ok, i know that i can create a custom inspector for a specific component, but whan i mean here i slightly different.
In a component i used a public variable of type System.TimeSpan but it isn't displayed in the inspector, my first taught is that unity have inspector defined for the commonly used variable type, so i made a custom inspector for this particular component, but: is it possibile to tell unity once for all that all the variable of a certain type has to be displayed in a custom defined way in the inspector without re-implementing it in every inspector?
Thanks in advance to everyone guys.
Answer by NoseKills · Jul 31, 2017 at 03:56 PM
As @HarshadK said you can make your own propertydrawer. And yes the serialization is something you have to keep in mind.
I mean if you have a component with a DateTime field, it usually wouldn't be useful to be able to edit its value in inspector if you don't serialize the the results to be used by your game. Unity can only serialize a limited amount of types automatically for you and those types already have a propertydrawer.
So what you can do is make your own class that stores a DateTime as long using its Ticks
[Serializable]
public class SerializedDateTime {
[SerializeField]
private long rawTicks;
public static explicit operator DateTime(SerializedDateTime sdt) {
return new DateTime(sdt.rawTicks);
}
}
Then make a PropertyDrawer that has for example int fields for year, month and day and have the drawer create a DateTime based on them whenever they change. Then have the drawer set the rawValue
of the SerializedDateTime
object it edits to the value of DateTime.Ticks
Then you can use SerializedDateTime as the type whenever you need to store a DateTime in a prefab for example. You can also make the cast operator implicit so you dont have to put (DateTime) in front of every usage, or keep it to avoid mixups
Ok, but i would like to avoid making a new class just for a variable type, moreover System.TimeSpan is already serializable, i just need to make a TimeSpanDrawer to display it correctly inthe inspector.
No, System.TimeSpan can not be serialized by Unity. $$anonymous$$eep in $$anonymous$$d that Unity does not use the .NET serialization system but it's own. Even Unity finally supports serializable structs, the TimeSpan struct has a private long field and therefore the field inside TimeSpan is not serialized. Since that's the only field, nothing is serialized.
A propertydrawer can only change how a serialized field looks like but can't change if it's serialized or not.
In theory you can create propertydrawers for built-in classes, even primitive types like "int", float, ... You can create a property drawer for fields marked with your own PropertyAttribute. An example is the built-in "Range" attribute. Just attach it to a float variable and the inspector will use a slider with the specifed $$anonymous$$ / max values. Another one is the "$$anonymous$$ultiline" attribute for string variables.
However as i said TimeSpan can not be serialized by Unity. That's why you have to create a wrapper for it. It doesn't need to be a class since Unity now supports serializable structs as well.
Answer by HarshadK · Jul 31, 2017 at 07:39 AM
If I understand correctly then what you need is Property Drawer. Take a look at links below:
Yes, it look exactly what i need, i'l check this as soon as possible, thank you @Harshad$$anonymous$$
I looked at the link you gave me (i still haven't the chance to test it), it looks like the Property Drawer is made just for custom serializable classes, but in my case i need to specify the inspector look for a .net built in variable type. It doesn't explicitly speak about variables, does it work the same?
Your answer
![](https://koobas.hobune.stream/wayback/20220612131556im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can i get SerializedProperty from UnityEvent which in List. Sorry for my Eng. 2 Answers
Unity Editor - Class variable value contrain 4 Answers
Custom Inspector & Arrays 0 Answers
Custom editor super slow even with no draw operations 1 Answer
Drawing a custom variable in the inspector y position bugs selection 0 Answers