Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to write a property wrapper for the long (Int64) type

Discussion in 'Scripting' started by Jymmy097, Oct 14, 2014.

  1. Jymmy097

    Jymmy097

    Joined:
    Jul 7, 2014
    Posts:
    67
    Hi everybody,

    I've read some articles (such as Unity manual and some other blogs) because I needed a property wrapper for the long type as Unity does not serialize it.

    I found out that this line of code:

    Code (CSharp):
    1. EditorGUI.PropertyField(Xrect, property.FindPropertyRelative("X"));
    adds a field I can edit... This is a line of code I wrote for UVector3, a class which encapsulates a long vector (unfortunately I couldn't use floats because of precision issues - such as subtracting 1 from a float value does not do anything if the float is big - ), but it does not work as X, Y and Z are long. What have I to insert instead of that "X" in the above line of code in case of long type?

    Thanks in advance....

    Jymmy097
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    That line of code has nothing to do with serializing. It has only to do with an editor script and displaying a property field.

    That "X" in that is the name of the field relative the property currently referenced in 'property'. So if your field is named "_someLongValue", and the property currently referenced is relative to that field, that's what you'd put into it.

    Of course though. If Unity doesn't draw properties that are typed longs. Using that method won't help you any, because it's pretty much the same method that unity uses by default.

    But again, it has NOTHING to do with serialization.
     
  3. Jymmy097

    Jymmy097

    Joined:
    Jul 7, 2014
    Posts:
    67
    Hi and thanks for speed reply, but Unity cannot make long value editable by Inspector. I've already tried to make a wrapper for my UVector3 class:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomEditor(typeof(UVector3))]
    5. public class UVector3Wrapper : PropertyDrawer
    6. {
    7.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    8.     {
    9.         EditorGUI.BeginProperty(position, label, property);
    10.         position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
    11.         int indent = EditorGUI.indentLevel;
    12.         EditorGUI.indentLevel = 0;
    13.         const int Width = 30;
    14.         const int Margin = 5;
    15.         Rect Xrect = new Rect(position.x, position.y, Width, position.height);
    16.         Rect Yrect = new Rect(position.x + Width + Margin, position.y, Width, position.height);
    17.         Rect Zrect = new Rect(position.x + 2 * Width + Margin, position.y, Width, position.height);
    18.  
    19.         EditorGUI.PropertyField(Xrect, property.FindPropertyRelative("X"));
    20.         EditorGUI.PropertyField(Yrect, property.FindPropertyRelative("Y"));
    21.         EditorGUI.PropertyField(Zrect, property.FindPropertyRelative("Z"));
    22.  
    23.         EditorGUI.indentLevel = indent;
    24.         EditorGUI.EndProperty();
    25.     }
    26. }
    27.  
    28.  
    But anything happens. I.E. I cannot see my new three field to edit UVector3....
    PS: I have only created the script and imported it as an asset.

    What's going wrong?

    Thanks a lot!!

    Jymmy097
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    That code isn't going to work because longs aren't serialized. So thusly, they are not going to be part of the 'SerializedObject' or 'SerializedProperty'. It's not a member of that collection.

    What you're going to have to do is implement ISerializationCallbackReceiver, serialize those longs yourself.

    Then in your PropertyDrawer you'r going to have to get a direct reference to your object, and you're going to have to display the longs using probably a TextField, validate the result, and parse it back to a long.