Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by OctoMan · Oct 13, 2014 at 02:53 PM · inspectorvisibledecimal

How to make decimal variables visible in Inspector?

I want to change a decimal variables in runtime in the inspector or just watch it without using

 Debug.Log(variable);

Is this anyhow possible?

Comment
Add comment · Show 7
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 OctoMan · Oct 13, 2014 at 02:49 PM 0
Share

It's a public variable already, but it's still not visible in the inspector. And yes i use c#.

 public decimal playtime = 0$$anonymous$$;


avatar image _dns_ · Oct 13, 2014 at 03:40 PM 0
Share

Hi, I think Unity can't serialize the decimal type, that's why you can't see it in the inspector. Float and double are supported but if you have to use decimal, I think you can write your own serializer for it to work in Unity. You can also write custom inspectors or PropertyDrawer that could help display/edit this type, like it's done here: http://wiki.unity3d.com/index.php/EnumFlagPropertyDrawer

avatar image OctoMan · Oct 13, 2014 at 04:09 PM 0
Share

I have tried to make a serialize container but still the decimals wont show up.

 [System.Serializable]
 public class Decimals
 {
     public decimal playtime, bla, blu;
 }


 public class Game$$anonymous$$anagerScript : $$anonymous$$onoBehaviour 
 {
     public Decimals showall;
 }

Also my program$$anonymous$$g skills aint that good.

avatar image _dns_ · Oct 13, 2014 at 04:19 PM 0
Share

Well, what you wrote is correct to serialize a custom class non derived from $$anonymous$$onobehavior. The problem here is that Unity can't serialize the decimal type at all. Finding a way to serialize this type is more complicated, you can read this blog about Unity serialization here: http://blogs.unity3d.com/2014/06/24/serialization-in-unity/ . They give an example of custom serialization and information on how to do this. This is not an quick/easy task, maybe some scripts in the asset store already manage this case. $$anonymous$$ay I ask why you want to use decimal type ?

avatar image OctoMan · Oct 13, 2014 at 05:19 PM 0
Share

Hmm i see, to bad it's not implemented yet. $$anonymous$$aybe in a future update or stuff.

I need to handle big numbers for my current project. So decimals are the best choice here it seems.

I work on an Idle Game where i get alot money in mid game+ and also have to spend it. Where floats and longs are to short to handle i use decimals which can hold 28-29 digits.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Bunny83 · Oct 13, 2014 at 06:52 PM

Like dns said, Unity can't serialize the decimal type on it's own. If you want Unity to actually "store" (serialize) the value, you have to provide your own way to serialize the value. This wrapper class does this:

 //SerializableDecimal.cs
 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class SerializableDecimal : ISerializationCallbackReceiver
 {
     public decimal value;
     [SerializeField]
     private int[] data;
 
     public void OnBeforeSerialize ()
     {
         data = decimal.GetBits(value);
     }
     public void OnAfterDeserialize ()
     {
         if (data != null && data.Length == 4)
         {
             value = new decimal(data);
         }
     }
 }

The field "value" is what you would use at runtime. Whenever Unity wants to serialize this custom class, it first copies the 4 internal uint fields into fields that unity can serialize. Now Unity will store those 4 values which made up a decimal value. When deserializing it restores those values.

Of course the inspector can only "show" serialized data, so it will show by default the data array with the 4 internal fields (hi, mid, lo and flags) which are difficult to edit. To get a visual representation you have to provide a PropertyDrawer like this:

 //SerializableDecimalDrawer.cs
 using UnityEngine;
 using UnityEditor;
 
 [CustomPropertyDrawer(typeof(SerializableDecimal))]
 public class SerializableDecimalDrawer : PropertyDrawer
 {
     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
     {
         var obj = property.serializedObject.targetObject;
         var inst = (SerializableDecimal)this.fieldInfo.GetValue(obj);
         var fieldRect = EditorGUI.PrefixLabel(position, label);
         string text = GUI.TextField(fieldRect, inst.value.ToString());
         if (GUI.changed)
         {
             decimal val;
             if(decimal.TryParse(text, out val))
             {
                 inst.value = val;
                 property.serializedObject.ApplyModifiedProperties();
             }
         }
     }
 }

With those two classes you have a decimal type that is serialized and can be edited / viewed in the inspector.

Keep in mind that the PropertyDrawer class need to be inside an "/Editor/" folder since it's an editor class.

edit
Just noticed that i probably should also give a usage example ;) Here it is:

 public class DecimalTestScript : MonoBehaviour
 {
     public SerializableDecimal val;
     void Start ()
     {
         Debug.Log("Val: " + val.value);
     }
 }

So the only difference from using a "normal" decimal value is, that you have to use:

     val.value

instead of

     val


Of course you can implement implicit and explicit cast operators to use a SerializableDecimal just like a decimal, but such operators don't work in all cases.

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 OctoMan · Oct 13, 2014 at 07:39 PM 0
Share

Wow thanks alot for that much effort, i didn't expect so much trouble with such small task. I thought it would be way easier. I may test it out later.

avatar image Bunny83 · Oct 13, 2014 at 08:23 PM 0
Share

I just realized i had a copy&paste error ^^ I somehow inserted the same script twice. I've updated the scripts. I also searched a bit the documentation and found the GetBits method, so there's no need for using reflection. Now the SerializableDecimal class is much shorter.

avatar image OctoMan · Oct 13, 2014 at 08:55 PM 0
Share

Looks way shorter now :P . Thanks again.

avatar image _dns_ · Oct 13, 2014 at 11:06 PM 0
Share

Cool, that's a nice and short example of a custom serialization with PropertyDrawer! This could also go on the Wiki as ISerializationCallbackReceiver was introduced with Unity 4.5 and is not very well documented yet!

Ok, now, what about this new Quantum bit type ? ;-)

avatar image
0

Answer by coderD1mka · Nov 16, 2020 at 06:41 PM

It does not work if I use SerializableDecimal class within a ScriptableObject inherited class =(

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 Cole_Slater · Nov 06, 2021 at 05:26 AM 0
Share

I had a similar issue trying to get this solution to work inside ScriptableObjects, but then figured out I couldn't really use property drawers altogether in my case, so came up with a workaround that works for really any numeric data type not supported natively by Unity's serializer. The solution isn't perfect but it works :)

 [System.Serializable]
 public class SerializableDecimal : ISerializationCallbackReceiver
 {
     [SerializeField]  private string inspectorField = "0";
     [HideInInspector] public decimal value;
 
     public void OnBeforeSerialize() {}
 
     public void OnAfterDeserialize()
     {
         if (!decimal.TryParse(inspectorField, out value))
             inspectorField = "0";
     }
 }

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

7 People are following this question.

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

Related Questions

C# public decimal variable not showing up in inspector 2 Answers

Array elements are empty in the inspector? 1 Answer

Custom Inspector Variables 1 Answer

Programmatically focus textfield in Editor 0 Answers

How to: c# public variable not show up 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