Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NeverEndingPrjct · Aug 26, 2014 at 02:48 PM · c#class

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 ?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NeverEndingPrjct · Aug 26, 2014 at 02:56 PM 0
Share

schouldnt extends monobehavior oO

avatar image AyAMrau · Aug 26, 2014 at 02:59 PM 0
Share

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:.

avatar image Paulo-Henrique025 · Aug 26, 2014 at 03:04 PM 0
Share

Not sure what you mean by "instantiate this class in the inspector"

avatar image NeverEndingPrjct · Aug 26, 2014 at 05:10 PM 0
Share

for example if u have a Rect u can instantie thisin the inspector and set their proberties ... with my class dat dont work :(

3 Replies

· Add your reply
  • Sort: 
avatar image
3

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NeverEndingPrjct · Aug 26, 2014 at 06:17 PM 0
Share

but then its posible to set values < 0...so how can i chekt this ?

avatar image NeverEndingPrjct · Aug 26, 2014 at 06:26 PM 0
Share

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

avatar image kacyesp · Aug 26, 2014 at 07:02 PM 0
Share

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

avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

C# Unity 3d Don't Destroy Class On Load 1 Answer

C# Accessing Variables from Class Variable 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges