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 MaT227 · Jan 08, 2015 at 01:48 PM · c#editorvariableinspectorcustom-inspector

Unity Editor - Class variable value contrain

I am using a Custom Editor Inspector for my MonoBehaviour class and I would like to know I it's possible to constrain a variable. Let me illustrate this with an example.

Let's imagine I have an integer value but I would like to authorize only some values in the Inspector like 1, 5, 99, whatever.

I would like to know if there is way to do that for any type of variables using editor features. I know that I can define an Enum or use a Dictionnary with some index but is there another way ?

Thanks a lot.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Landern · Jan 08, 2015 at 02:02 PM

Use a List of int or an array(int nums[]), get the selected index and return the number from the List/array/Collection.

Comment
Add comment · Show 1 · 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 MaT227 · Jan 08, 2015 at 03:36 PM 0
Share

I've received interesting answer here too : http://forum.unity3d.com/threads/unity-editor-class-variable-value-contrain.289844/

avatar image
2

Answer by MakinStuffLookGood · Jan 08, 2015 at 02:42 PM

You can certainly use a custom editor to constrain your variable however you wish. Here is an example of a simple class and editor, where we constrain the public int field to even inputs:

Foo.cs

 using UnityEngine;
 
 public class Foo : MonoBehaviour
 {
     public int Bar;
 }

FooEditor.cs

 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(Foo))]
 public class FooEditor : Editor
 {
     public override void OnInspectorGUI()
     {
         Foo foo = (Foo)target;
 
         foo.Bar = EditorGUILayout.IntField("Bar", foo.Bar);
         foo.Bar = ConstrainBar(foo.Bar);
         
         EditorUtility.SetDirty(foo);
     }
     
     int ConstrainBar (int bar)
     {
         return (int)Mathf.Round((float)bar/2.0f) * 2;
     }
 }
 

We don't really constrain the variable, but instead create an editor that will apply a function to the variable when it is set in the editor. If you want to constrain the variable in code as well, you could have a simple property and apply the constraints in the setter.

Unfortunately, properties (getters/setters) don't play nice with the inspector by default. There are some hackneyed ways of exposing a property in the inspector, but it's often unnecessary.

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
0

Answer by Mmmpies · Jan 08, 2015 at 01:56 PM

Setup an enumerator like this

 enum TheNumbers
 {
   1,
   5,
   99
 }

or

 enum TheOtherNumbers
 {
   one,
   five,
   nintynine
 }
Comment
Add comment · Show 2 · 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 MaT227 · Jan 08, 2015 at 02:00 PM 0
Share

I've already thought about making an enum :) but I thought that there was better way to do it using editor features and functions.

avatar image Landern · Jan 08, 2015 at 02:00 PM 0
Share

enum theNumbers { 1, 5, 99 }

Ah, no, by default an enum type is an int, just like with variables, you can't start it with a number and it represents a number. Given this example if i cast an int to theNumbers.1 it would be 0. But of course that wouldn't work.

Enums: http://msdn.microsoft.com/en-us/library/sbbt4032.aspx

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

28 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 avatar image avatar image

Related Questions

Custom Inspector - How to add functionality? 1 Answer

calculate things... and display in inspector 3 Answers

Unity Custom Editor Texture Warning 2 Answers

How to change inspector with non-Monobehaviour objects ? 1 Answer

Public Scene Variables not appearing in the inspector 2 Answers


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