- Home /
How to make costume classes Visible in the Inspector
The Class:
[Serializable]
public class LowPolyMapSettings : MonoBehaviour {
//private
private float _SegmentWidth = 1f;
private float _SegmentHeight = 1f;
private int _NumberOfSegmentsAtXAxis = 1;
private int _NumberOfSegmentsAtZAxis = 1;
//Fields
public float SegmentWidth {
set {
if (value > 0){
_SegmentWidth = value;
} else {
throw new ArgumentException(" < 0 ");
}
}
get {
return _SegmentWidth;
}
}
public float SegmentHeight {
set {
if (value > 0){
_SegmentHeight = value;
} else {
throw new ArgumentException(" < 0 ");
}
}
get {
return _SegmentHeight;
}
}
public int NumberOfSegmentsAtXAxis {
set {
if (value > 0){
_NumberOfSegmentsAtXAxis = value;
} else {
throw new ArgumentException(" < 0 ");
}
}
get {
return _NumberOfSegmentsAtXAxis;
}
}
public int NumberOfSegmentsAtZAxis {
set {
if (value > 0){
_NumberOfSegmentsAtZAxis = value;
} else {
throw new ArgumentException(" < 0 ");
}
}
get {
return _NumberOfSegmentsAtZAxis;
}
}
public LowPolyMapSettings(int NumberOfSegmentsAtXAxis,int NumberOfSegmentsAtZAxis,
float SegmentWidth, float SegmentHeight){
this.NumberOfSegmentsAtXAxis = NumberOfSegmentsAtXAxis;
this.NumberOfSegmentsAtZAxis = NumberOfSegmentsAtZAxis;
this.SegmentWidth = SegmentWidth;
this.SegmentHeight = SegmentHeight;
}
}
I can instantiate this class in the inspector -.- how to do this ?
Since this is a $$anonymous$$onoBehaviour you can make instances of it, by adding it as a component to a game object.
If you want to create a class that doesn't need to be attached to a game object, you should look at ScriptableObjects. This article provides editor scripts for making assets from ScriptableObjects:.
Not sure what you mean by "instantiate this class in the inspector"
for example if u have a Rect u can instantie thisin the inspector and set their proberties ... with my class dat dont work :(
Answer by kacyesp · Aug 26, 2014 at 05:35 PM
What you labeled as "Fields" in your comments are actually properties. Properties do not appear in the inspector. Public instance variables ( a.k.a. fields ) do.
Whichever variables you want to appear in the inspector, just change them to public.
but then its posible to set values < 0...so how can i chekt this ?
let me explain it a bit more this class should descibe some settings (later there will be more)
in an other class i need an instace of this class, and i want to set this vairables in the inspector
You can validate all the fields in the Start function since it gets called as soon as the script is initialized ( and enabled ). p.s. if you accept my answer, it would be my 2nd one of all time ;D
Answer by jokim · Aug 26, 2014 at 07:19 PM
As kacyesp said, Your fields are actually properties, which won't be serialized automatically by Unity.
There's a way to make editor scripts to actually allow properties to be serialized, which is not so easy to implement (in my opinion, but then again, I guess that's my lazy side - i would consider doing it if you plan on serializing lots of properties.)
Here's a link to how to get this to work.
I personally haven't tried it out and I don't quite get the quirks of it yet. But i believe it does expose properties.
Your other solution is to use public variables - again like kacyesp said.
BUT - Instead of going with Start() or OnEnable(), I suggest you use OnValidate(), Which is called whenever there's a modification made in the inspector. Look it up
Answer by AyAMrau · Aug 26, 2014 at 07:37 PM
You can display the private fields in the editor by using [SerializedProperty] attribute:
[SerializedProperty]
private float _SegmentWidth = 1f;
You have to put it over every variable you want to display. But that let's you keep the encapsulation while allowing editor access.
As for validation, I think you may be able to get away with making a custom property drawer that will work something like [Range(min, max)] but where you can set just the min Property Drawer Scripting Reference
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Calling functions from other namespaces and scripts. 0 Answers
Conversion of a static class to struct? 1 Answer