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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by daniel-eherbert · Mar 30, 2015 at 03:29 AM · c#rangepropertiesserializable

Is it possible to define a Range for a float in a System.Serializable class?

Admittedly C# is still something I'm learning the ins and outs of, hopefully this is possible.

Say we have a serializable class:

 [System.Serializable]
 class CustomFloatParameter : System.Object {
      [Range(0f, 10f)]
      public float value = 1f;
      public float timesTwo { get { return value * 2; } }
 }


And we define that in a MonoBehaviour

 class SomeComponent : MonoBehaviour {
     public CustomFloatParameter someProperty;
 }

Is there a way I can set what 0f and 10f might be in the [Range] where the field is defined?

Complete stab in the dark which I do not expect to work:

     [Range(5f, 20f)]
     public CustomFloatParameter someProperty;


(Similar to this, is there a way I can set the default value (1f) as well?)

Comment
Add comment · Show 3
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 daniel-eherbert · Mar 29, 2015 at 11:47 PM 0
Share

I've just noticed http://docs.unity3d.com/ScriptReference/PropertyAttribute.html

Is this the sort of thing I'm looking for?

avatar image Huacanacha · Mar 30, 2015 at 04:23 AM 0
Share

http://docs.unity3d.com/ScriptReference/RangeAttribute-ctor.html

Looks like that will control the values available by slider in the inspector. That won't prevent the value being set outside that range in script (do those checks yourself).

avatar image daniel-eherbert · Mar 30, 2015 at 04:54 AM 0
Share

That it would. I guess I'm after a custom RangeAtttribute/PropertyAttribute style feature

Better clarifying my sample above, maybe it would look like:

 [System.Serializable]
 class CustomFloatParameter : System.Object {
     [Range(A, B)]
     public float value = C;
     public float timesTwo { get { return value * 2; } }
 }
 
 
 ......
 
 class SomeComponent : $$anonymous$$onoBehaviour {
     [CustomFloat(0f, 10f, 2f)]
     CustomFloatParameter myCustomFloat;
 
 }


A would become 0f, B would become 10f, and C would become 2f;

Though, I guess from what I've ready about PropertyAttributes and PropertyDrawers today I wouldn't have to worry about the [Range(..)] in CustomFloatParameter, the custom drawer code would handle that appropriately.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by idbrii · Feb 04, 2018 at 12:29 AM

This was kindof demonstrated with the RangedFloat in this Unite 2016 talk on Scriptable Object .

You create your own attribute that will look inside your object and set its range.

The code for that talk is on bitbucket. Looks like this is the relevant commit. Look at MinMaxRangeAttribute, RangedFloat, and RangedFloatDrawer. I think the real magic happens in RangedFloatDrawer.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Is this a good way to handle range? 1 Answer

How to make a dynamic list of a serializable variable? 1 Answer

pre generated static Object in C# 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