Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by nockieboy · Sep 14, 2015 at 09:01 PM · custom inspectorcustom classproperty drawer

Custom Class Property Drawer

I've created a simple custom class - basically it's a variable that can handle a range. It's from a blog by grapefruit games here.

The issue I'm having is that I can't get the custom property drawer to work. I'm getting odd errors, like:

 Assets/Editor/RangeAttributeDrawer.cs(44,108): error CS1526: A new expression requires () or [] after type
 Assets/Editor/RangeAttributeDrawer.cs(44,113): error CS8032: Internal compiler error during parsing, Run with -v for details

Here's both scripts - the custom drawer AND the class I'm trying to create the custom drawer for:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 [CustomPropertyDrawer (typeof(MinMaxRangeAttribute))]
 class RangeAttributeDrawer : PropertyDrawer
 {
 
     // These constants describe the height of the help box and the text field.
     const int helpHeight = 30;
     const int textHeight = 16;
     
     // Provide easy access to the RangeAttribute for reading information from it.
     MinMaxRangeAttribute rangeAttribute { get { return ((MinMaxRangeAttribute)attribute); } }
     
     // Here you must define the height of your property drawer. Called by Unity.
     public override float GetPropertyHeight (SerializedProperty property, GUIContent label)
     {
         return base.GetPropertyHeight (property, label) + 16;
     }
     
     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
     {
         // Now draw the property as a Slider or an IntSlider based on whether it’s a float or integer.
         var range = attribute as MinMaxRangeAttribute;
         SerializedProperty minValue = property.FindPropertyRelative ("minVal");
         SerializedProperty maxValue = property.FindPropertyRelative ("maxVal");
         var newMin = minValue.floatValue;
         var newMax = maxValue.floatValue;
             
         var xDivision = position.width * 0.33f;
         var yDivision = position.height * 0.5f;
         EditorGUI.LabelField (new Rect (position.x, position.y, xDivision, yDivision), label);
 
 
         SerializedProperty minLimit = property.FindPropertyRelative ("minLimit");
         SerializedProperty maxLimit = property.FindPropertyRelative ("maxLimit");
 
         EditorGUILayout.LabelField ("Min Val:", minValue.ToString ());
         EditorGUILayout.LabelField ("Max Val:", maxValue.ToString ());
 
         EditorGUI.LabelField( new Rect( position.x, position.y + yDivision, position.width, yDivision ), range.minLimit.ToString( "0.##" ) );
         EditorGUI.LabelField( new Rect( position.x + position.width - 28f, position.y + yDivision, position.width, yDivision ), range.maxLimit.ToString( "0.##" ) );
         EditorGUI.MinMaxSlider( new Rect( position.x + 24f, position.y + yDivision, position.width – 48f, yDivision ), ref newMin, ref newMax, range.minLimit, range.maxLimit );
         
         EditorGUI.LabelField( new Rect( position.x + xDivision, position.y, xDivision, yDivision ), "From: " );
         newMin = Mathf.Clamp( EditorGUI.FloatField( new Rect( position.x + xDivision + 30, position.y, xDivision – 30, yDivision ), newMin ), range.minLimit, newMax );
         EditorGUI.LabelField( new Rect( position.x + xDivision * 2f, position.y, xDivision, yDivision ), "To: " );
         newMax = Mathf.Clamp( EditorGUI.FloatField( new Rect( position.x + xDivision * 2f + 24, position.y, xDivision – 24, yDivision ), newMax ), newMin, range.maxLimit );
         
         minValue.floatValue = newMin;
         maxValue.floatValue = newMax;
     }
 }

And the custom attribute here:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class MinMaxRangeAttribute : PropertyAttribute
 {
 
     public float minLimit;
     public float minVal;
     public float maxVal;
     public float maxLimit;
 
     public MinMaxRangeAttribute (float minL, float minV, float maxV, float maxL)
     {
         this.minLimit = minL;
         this.minVal = minV;
         this.maxVal = maxV;
         this.maxLimit = maxL;
     }
 
     public float RandomVal ()
     {
         return Random.Range (minVal, maxVal);
     }
 
     public bool IsWithinRange (float value)
     {
         if (value <= maxLimit && value >= minLimit) {
             return true;
         } else {
             return false;
         }
     }
 }

I was part-way through finishing the custom drawer, but those errors are stopping me from making progress (so there may be a few mis-named variables in there from the cut-n-paste code from the blog.)

Can anyone help me out by checking the code for errors or cleaning it up for me? I'm halfway to wondering if the MonoDevelop editor is just not happy with the cut n paste and is making up errors to wind me up...

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Baste · Sep 14, 2015 at 10:54 PM

You've supplied 3 arguments to the new Rect function on line 44:

 EditorGUI.MinMaxSlider( new Rect( position.x + 24f, position.y + yDivision, position.width – 48f, yDivision ), ref newMin, ref newMax, range.minLimit, range.maxLimit );

And in two other places further down. The compiler's spitting out real bad error messages here for some reason.

Also, get Visual Studio (Community Edition's free). I copy-pasted your code, got a red line under the line with the error, moused over it and got a popup saying "UnityEngine.Rect does not contain a constructor that takes 3 arguments". MonoDevelop left you with no clues whatsoever and you had to come here for help.

Comment
Add comment · Show 4 · 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 nockieboy · Sep 15, 2015 at 02:54 PM 0
Share

I'm using Visual Studio on the desktop, but ironically I'm developing this problem project on my laptop and haven't upgraded to Unity 5.2 yet. I will do it tonight and see if it helps.

Looking at your reply though, you say I've supplied 3 arguments to the Rect function - I can't see how? Looking at the code above, I've supplied the following arguments:

 1) position.x + 24f
 2) position.y + yDivision
 3) position.width - 48f
 4) yDivision

That's four by my counting?

avatar image Baste · Sep 15, 2015 at 03:28 PM 2
Share

Turns out default Visual studio wasn't enough, and I was a bit too quick on the draw.

Looking at this from work, and using VS with Resharper, I got a better answer:

 new Rect( position.x + 24f, position.y + yDivision, position.width – 48f, yDivision )

 Unexpected character '–'

Are you writing your code in notepad or wordpad or $$anonymous$$S word? Or any other text program that automatically turns the $$anonymous$$us symbol '-' into the dash symbol '–'? That's the error, anyways; you don't have a $$anonymous$$us there, but another symbol that looks almost completely identical, but isn't the same one.

Lemme put them next to each other for you:

This is a $$anonymous$$us symbol: -
This is not a $$anonymous$$us symbol: –

Replacing all of the dashes with $$anonymous$$uses makes your stuff compile. You should also probably figure out what's causing them to show up, and fix that. If you're just going to code quickly in a text editor, use Notepad++, not Notepad, as the former actually respects what keys you press.

avatar image nockieboy Baste · Sep 15, 2015 at 04:28 PM 0
Share

That was it - sorted out the strange error by replacing the dashes with proper $$anonymous$$uses in the code. The dashes were showing up as normal $$anonymous$$uses...

The code is throwing up some other wierd stuff now - to the point that I'm binning it and starting from scratch on my own. Heading off to find the custom drawer Unity tutorial video.

Thanks for your help Baste!

avatar image nockieboy · Sep 15, 2015 at 03:57 PM 0
Share

Ahh.. yes, I can see how that happened. I was copy and pasting the code straight from the blog, so that's clearly where the $$anonymous$$uses have been reformatted as dashes in the blog, and thus causing errors in $$anonymous$$onoDevelop. I'll try it out shortly, although I'm sure I'd removed the dashes in $$anonymous$$onoDevelop and tried $$anonymous$$uses in their place and it was still unhappy... Anyhoo - I'll get back with the results shortly.

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

Property drawer with multiple fields: only first field has prefab override implemented 1 Answer

Draw rect color disappearing when any input is detected 1 Answer

How do you update a property's int value based on two (or more) other properties in the Inspector? 0 Answers

How can I color a PrefixLabel? 0 Answers

Issues with setting values in custom class 0 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