long data type does not appear in Inspector

Hi,

the following does not seem to appear in the Inspector:

public long MyVariable;

Doubles do appear though, which is rather odd.

I’m using Unity 2.5.1f5 (24931) on a Mac.
Is there a workaround for this?

I got a long variable.

Yep, it’s a long variable. However, it doesn’t appear in the Inspector window.

When I do this:

    public int MyVariable1;
    public long MyVariable2;
    public double MyVariable3;

I only see “My Variable 1” and “My Variable 3” in the inspector. Is this a bug?

It’s not a bug, it’s just not implemented, same with byte, uint, etc. Ideally they would all be exposed, but someone has to code it, and the more obscure types aren’t that high of a priority I guess. (Well, byte actually is exposed as a boolean…now that is a bug…)

–Eric

byte shows up as bool, ouch.

Are there any workarounds?

Anyway, at least for this thread I’m off to the feedback/wishlist page for a feature request.

You could write a custom editor (custom inspector editor) that is invoked when an object with your class attached is selected, and implement a text field and long conversions there.

I am not sure if the methods to do that are fully documented yet, and if not it could break with any new release, but it’s something if you absolutely need long editing.

The Notes extension I threw up on the wiki ages ago may help you here: http://www.unifycommunity.com/wiki/index.php?title=Notes

-Jeremy

Edit: I haven’t tested the code with 2.5, so as-is it may not work, but it shows the general idea.

Hi Jeremy,

I already have a custom inspector for the long, http://forum.unity3d.com/viewtopic.php?t=30337.

I can edit the long variable just fine; however, since it’s not recognized by the Inspector, once you start the game all long variables reset to whatever default value was set in the .cs file. =(

Ah gotcha. Sorry, didn’t see that other post.

-Jeremy

1 Like

I am wondering when is inspector support for long and other unsupported data types going to be implemented. I really need this. Thanks.

Long int and float ( double ) are common enough that I too was surprised to not see them show up
in the inspector. Can we consider this thread a formal request for that feature in the next release??

Nope, you’d need to go to feedback.unity3d.com for that.

–Eric

Done and done. :sunglasses:

You may wait a feedback on your feedback for ages. Just write a custom inspector for your script.

What a shame about this topic, still not working …

Hey modiX,

You can solve this problem pretty easily by making a simple class.

public class sLong : object, ISerializationCallbackReceiver
{
    private long _value;
    [SerializeField]
    [HideInInspector]
    private string _savedValue;

    #region ISerializationCallbackReceiver implementation

    public void OnBeforeSerialize ()
    {
        _savedValue = _value.ToString();
    }

    public void OnAfterDeserialize ()
    {
        if(!string.IsNullOrEmpty(_savedValue))
        {
            _value = long.Parse(_savedValue);
        }
    }
    #endregion

    public implicit operator long(sLong lg)
    {
        //Do your logic for converting
    }

    public implicit operator sLong(long lg)
    {
        //Do your logic for converting back
    }
}

From there just make your own property drawer and make it show up how you want it too.

Regards,

1 Like

Hi BMayne,

I’m sorry, I’m pretty new to Unity, so I’ve no idea how I can use the piece of code to let it detect long types and show them in inspector. Right now I solved the problem by creating a text field in the editor class of the script and convert the received string into long to store it.

This is very odd and costs time when I get more long variables. I got the clue you’re hinting a solution, that would detect long type fields and offer me a text field on that position, automatically? If yes, can you please give me a full example? I would really enjoy it.

Thank you.

Hey Modix,

You are correct if you used sLong it would show up in the inspector as a text field automatically and you would be able to use it in code as if it were a long (using implicit operator).

using UnityEngine;
using System.Collections;
using Serializable = System.SerializableAttribute;

[Serializable]
public class sLong : object, ISerializationCallbackReceiver
{
  private long _value;
  [SerializeField]
  [HideInInspector]
  private string _savedValue;

  #region ISerializationCallbackReceiver implementation
  /// <summary>
  /// This is called by Unity a crazy amount of times. This
  /// is not called on the main thread so don't try putting
  /// Unity functions here.
  /// </summary>
  public void OnBeforeSerialize()
  {
    _savedValue = _value.ToString();
  }

  /// <summary>
  /// This is called when Unity asks an object to load itself.
  /// </summary>
  public void OnAfterDeserialize()
  {
    if( !string.IsNullOrEmpty( _savedValue ) )
    {
      //If it's invalid text we don't want to try parsing it.
      long.TryParse( _savedValue, out _value );
    }
  }
  #endregion

  /// <summary>
  /// This is used so you covert this to a
  /// long implicitly (You don't have to use casting).
  /// </summary>
  /// <param name="lg">The sLong you want to convert to a long</param>
  /// <returns></returns>
  public static implicit operator long( sLong lg )
  {
    return lg._value;
  }

  /// <summary>
  /// This is used to you don't have to corvert
  /// a sLong implicity.
  /// </summary>
  /// <param name="lg">the long you want to convert to an sLong</param>
  /// <returns></returns>
  public static implicit operator sLong( long lg )
  {
    return new sLong() { _value = lg };
  }
}

The next part is the property drawer. If you have never used one before they are basically just in inspector for a type or attribute. Unity uses them for a lot of things like [Range()] or [Header()] attributes. This is a pretty simple drawer but it gets the job done.

using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(sLong))]
public class sLongEditor : PropertyDrawer
{
  private const string SLONG_STRING_VALUE_PROPERTY_NAME = "_savedValue";

  public override void OnGUI( Rect position, SerializedProperty property, GUIContent label )
  {
    SerializedProperty stringProp = property.FindPropertyRelative( SLONG_STRING_VALUE_PROPERTY_NAME );

    EditorGUI.PropertyField( position, stringProp, label );
  }

  public override float GetPropertyHeight( SerializedProperty property, GUIContent label )
  {
    return base.GetPropertyHeight( property, label );
  }
}

Keep in mind.

If you type in a bad long value it will reset to zero. This is because I am using tryparse. You could always fix that if you want.

If you want to make any other types you make serializable you can pretty much follow the same code above. I have made one for types (which gets a lot more difficult)

1 Like

Thank you, the PropertyDrawer was exactly what I needed. Works. =)

1 Like