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 /
avatar image
0
Question by Damnsatinist · Nov 01, 2019 at 09:51 PM · serializationcustom editorparametersconstructorcustom class

Serialized Parameter Constructor?

I have a custom (non monobehaviour) value type called "Trait" that I want to initialize from the Unity Inspector, but it seems that only the default constructor can get called from the inspector, and I want to keep the internal state of this type unexposed and immutable. ie, the type looks like this:


 [Serializable]
 public readonly struct Trait : IComparable, IComparable<Trait>, IEquatable<Trait>, IConvertible, IFormattable
 {
     private readonly float MaxVal, MinVal, Value;
 
     public Trait(float value, float max, float min)
     {
         if (max < min) throw new ArgumentException("Max val cannot be less than min val.");
         MaxVal = max;
         MinVal = min;
         if (value > max) Value = max;
         else if (value < min) Value = min;
         else Value = value;
     }
    // Operations, conversions, etc ...
 }



The point is that the representation guarantees that the current value can never go beyond the supplied max and min values.

In any case, the way I have it implemented, I cant initialize the value from the inpsector. I can think of a couple possible solutions, but none of them seem ideal:


  1. Remove the readonly modifier and add private serialized properties that are used in the default constructor. This wouldn't horribly undermine the representation, but it would cause me to lose whatever performance is gained from having the struct be readonly. Also the compiler complains because it cant guarantee that those values have been assigned to...

  2. Create a custom inspector/editor for this type that creates three properties and then uses them to call the parameterized constructor. I'm not sure this is possible? serializedObject is read only from the Editor class, so it doesnt seem like theyd want you to reinstantiate it. Plus it is a unity Object, which wont work with this non-monobehaviour.

Any suggestions / ideas?

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
Best Answer

Answer by Damnsatinist · Nov 16, 2019 at 07:16 AM

Well, I didn't solve it the way I would've liked but it does work:

 public struct Trait : ... ... ...
 {
      public float MaxVal { get => maxVal; }
      public float MinVal { get => MinVal; }
      [SerializeField] private float maxVal, minVal, value;

      // etc
 }
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

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

Related Questions

Saving editor-only variables 0 Answers

Can’t serialize custom class variable in ScriptableObject.Or add instance of a class to a list with overridden method in custom editor. 1 Answer

Serialising AnimatorControllerParameter object 0 Answers

URGENT - Cannot be serialized because it does not have a default public constructor.... 1 Answer

Serialize custom multidimensional array from Inspector 3 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