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 LoftyTheMetroid · Jan 10, 2013 at 05:46 PM · serializedproperty

Modifying SerializedProperty in Inspector

I have an Editor script for a class I've been working on and I'm trying to update it to take advantage of SerializedProperty (which I'm not sure I fully understand).

The issue I'm having is that I have the inspector displaying the same data in different ways depending on how the user would prefer to manipulate the data. However, I can't find a way to have a SerializedProperty be displayed differently in the Inspector without also changing how it stores that data internally.

For example, imagine my data is var length: float, stored as "meters". However, the user has opted to work with centimeters instead.

Previously, I would have done something as follows:

 if (meters)
     length = EditorGUILayout.IntField("Meters:", length);
 else
     length = EditorGUILayout.IntField("Centimeters:", length * 100) / 100;

The best I've done to emulate that with SerializedProperty is something like this:

 if (meters)
 {
     EditorGUILayout.PropertyField(lengthProp, new GUIContent("Meters:"));
 }
 else
 {
     var lengthVal : float = lengthProp.floatValue;
 
     lengthVal = EditorGUILayout.IntField("Centimeters:", lengthVal * 100);
 
     if (!lengthProp.hasMultipleDifferentValues && lengthVal != lengthProp.floatValue)
         lengthProp.floatValue = lengthVal / 100;
 }

But that doesn't seem like an ideal solution and misses some of the advantages of SerializedProperty. Any suggestions on a better approach?

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 Julien-Lynge · Jan 10, 2013 at 07:22 PM

I think you should look at custom editors. Hopefully this page will get you started:

http://answers.unity3d.com/questions/222741/how-to-make-a-custom-editor-in-c.html

In a nutshell, these wrap around your component and have a GUI event associated with them. So you can create whatever interface you'd like, and do whatever math you'd like, at the custom editor level, and then it communicates information back down to the actual component. The user never sees the actual component, just the custom editor.

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 LoftyTheMetroid · Jan 10, 2013 at 07:44 PM 0
Share

That's actually what I'm doing. All of the code above is in an Editor class. $$anonymous$$y current implementation has an inspector interface that hides the actual data and uses math/conversions to display more easily understood and editable values, and then converts the input back into data the original class can use.

What I'm really looking for is how to best implement SerializedProperty in an Editor class in a way that separates "how the data is stored" from "how the data is displayed/manipulated in the Inspector/Editor".

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

9 People are following this question.

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

Related Questions

Mass component editing through SerializedProperty or Setters 0 Answers

Applying a PropertyDrawer to elements of a List<> 1 Answer

How do you actually use CanEditMultipleObjects 1 Answer

How does SerializedProperty iteration work? 0 Answers

ObjectField functions ignoring parameter 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