- Home /
decimals not allowed to update as global varibles from Unity to C# script?,Why are decimals not allowed to apply as global variable to be changed in Unity?
I am going through a training program in Unity and the person is using float for the variables, while I want to use the more precise decimal. I have worked so far, until he has now gotten to the section of Global variables.
I do like that I can use the objects in Unity Script applied to the object to fill in the value. BUT this does not work if I use decimal. It works for Integers, Floats, and Doubles no problem. but why not Decimals? Is this because only C# 4 is in the compatibility to Unity? My version of Unity is 2018.1.0f2,I am going through a training program in Unity and the person is using float for the variables, while I want to use the more precise decimal. I have worked so far, until he has now gotten to the section of Global variables.
I do like that I can use the objects in Unity Script applied to the object to fill in the value. BUT this does not work if I use decimal. It works for Integers, Floats, and Doubles no problem. but why not Decimals?
I'm sorry, I'm not understanding what you're getting at. Are you trying to store a global variable, or are you saying that you want to use a Decimal value in the editor? I'm not following.
Answer by FlaSh-G · May 28, 2018 at 12:02 PM
You're not talking about global variables, but public/serializable fields, are you?
Unity can only serialize a specific set of types:
All classes inheriting from UnityEngine.Object, for example GameObject, Component, MonoBehaviour, Texture2D, AnimationClip.
All basic data types like int, string, float, bool.
Some built-in types like Vector2, Vector3, Vector4, Quaternion, Matrix4x4, Color, Rect, LayerMask.
Arrays of a serializable type
List of a serializable type)
Enums
Structs
Source: https://docs.unity3d.com/ScriptReference/SerializeField.html
decimal
is not one of them, so it can't be serialized jsut like that. To be honest, I'm not sure how the current state is with adding new types to this all.
Either way - if it's not serializable, it will not be shown in the inspector. I'd recommend thinking through why you want this extreme precision anyways.
Answer by Nerull22 · May 28, 2018 at 04:41 PM
I'm going to answer in regards to Flash-G's answer posted above. I don't disagree with his statement, but I think there are routes that were left out of the answer.
Nothing he said was inaccurate, you can not just serialize a decimal data type without some work. What you can do though, is you can create your own version of decimal and store that. I posted some basic code that can achieve this. You would need to write a custom property drawer to get it to operate the way that you want. This is untested and people should correct me where I'm making false assumptions here, but this is where I would start with the problem.
using System;
using System.Globalization;
using UnityEngine;
[Serializable]
public class UnityDecimal : ISerializationCallbackReceiver
{
private decimal _decimalValue;
[SerializeField]
private string _storedValue;
public decimal Value
{
get
{
return _decimalValue;
}
set
{
_decimalValue = value;
}
}
public void OnBeforeSerialize()
{
_storedValue = Convert.ToString(_decimalValue, CultureInfo.InvariantCulture);
}
public void OnAfterDeserialize()
{
_decimalValue = Convert.ToDecimal(_storedValue);
}
}
This is an interesting approach. An additional custom editor class would make sure only valid values would be entered, and implicit casts could make it nice to use.
However, I'll still stick with my "Are you sure you even need the extra precision" before I'd implement all this :)
Edit: Oh, and to be able to display and change the proper value in play mode, the editor class would have to do some additional back and forth. At this point, purchasing Odin might also be an option :)
I agree, there is work to be done here definitely. And should absolutely validate that it's necessary before doing work you don't need. Definitely good asterisk here. :)
Considering I am still new into the C# program$$anonymous$$g I think trying to create a special class would be a bit beyond. I do appreciate the info though!
:)
Answer by DocQwerty · May 28, 2018 at 10:53 PM
Well the training class did not indicate public/serializable fields, but if that is what they are called, I will forget it by tomorrow. :) Basically they the field "public double BaseStoreCost;" was listed in c# and on the Unity graphics engine you can see it in a box to edit.
the basic program I am learning to build is fine with float/double. but I eventually want to get it to need extremely precise numbers. I tinkered with c and c++ in the past and found how unreliable precision is with float and double. When I found decimal I was happy.
I did figure out a way around my issue though. I used float/double as my basis for the serialized field and converted it to decimal for the final storage.
Your answer
Follow this Question
Related Questions
AssemblyInfo or alternative - How to add version number 1 Answer
Can I open a version controlled project with Unity Free? 1 Answer
Download link for previous versions of unity3d (3.0) 1 Answer
Asset Store Prefabs Required Unity Version. 0 Answers
How to check what version of an asset store product you have? 0 Answers