@@ -1823,6 +1823,14 @@ public void ShouldSerializeDeserializeWithCustomSerializeRegistration()
18231823 var json = NetJSON . Serialize ( obj ) ;
18241824 }
18251825
1826+ [ TestMethod ]
1827+ public void SerializeVector3Struct ( )
1828+ {
1829+ var value = new Vector3 ( 10 , 20 , 30 ) ;
1830+ var json = NetJSON . Serialize ( value ) ;
1831+ var value2 = NetJSON . Deserialize < Vector3 > ( json ) ;
1832+ }
1833+
18261834 public static void SerializeCustomObject ( CustomObject customObject , StringBuilder sb , NetJSONSettings settings )
18271835 {
18281836 var result = $@ "{{""ID"": { customObject . ID } , ""Name"": ""{ customObject . Name } ""}}";
@@ -2994,4 +3002,232 @@ public class PayloadClassWithWithReadOnlyBackingField
29943002
29953003 public string Value => _value ;
29963004 }
3005+
3006+ public struct Vector3
3007+ {
3008+ public const float kEpsilon = 1E-05f ;
3009+ public float x ;
3010+ public float y ;
3011+ public float z ;
3012+ public float this [ int index ]
3013+ {
3014+ get
3015+ {
3016+ switch ( index )
3017+ {
3018+ case 0 :
3019+ return this . x ;
3020+ case 1 :
3021+ return this . y ;
3022+ case 2 :
3023+ return this . z ;
3024+ default :
3025+ throw new IndexOutOfRangeException ( "Invalid Vector3 index!" ) ;
3026+ }
3027+ }
3028+ set
3029+ {
3030+ switch ( index )
3031+ {
3032+ case 0 :
3033+ this . x = value ;
3034+ break ;
3035+ case 1 :
3036+ this . y = value ;
3037+ break ;
3038+ case 2 :
3039+ this . z = value ;
3040+ break ;
3041+ default :
3042+ throw new IndexOutOfRangeException ( "Invalid Vector3 index!" ) ;
3043+ }
3044+ }
3045+ }
3046+
3047+ public float sqrMagnitude
3048+ {
3049+ get
3050+ {
3051+ return this . x * this . x + this . y * this . y + this . z * this . z ;
3052+ }
3053+ }
3054+ public static Vector3 zero
3055+ {
3056+ get
3057+ {
3058+ return new Vector3 ( 0f , 0f , 0f ) ;
3059+ }
3060+ }
3061+ public static Vector3 one
3062+ {
3063+ get
3064+ {
3065+ return new Vector3 ( 1f , 1f , 1f ) ;
3066+ }
3067+ }
3068+ public static Vector3 forward
3069+ {
3070+ get
3071+ {
3072+ return new Vector3 ( 0f , 0f , 1f ) ;
3073+ }
3074+ }
3075+ public static Vector3 back
3076+ {
3077+ get
3078+ {
3079+ return new Vector3 ( 0f , 0f , - 1f ) ;
3080+ }
3081+ }
3082+ public static Vector3 up
3083+ {
3084+ get
3085+ {
3086+ return new Vector3 ( 0f , 1f , 0f ) ;
3087+ }
3088+ }
3089+ public static Vector3 down
3090+ {
3091+ get
3092+ {
3093+ return new Vector3 ( 0f , - 1f , 0f ) ;
3094+ }
3095+ }
3096+ public static Vector3 left
3097+ {
3098+ get
3099+ {
3100+ return new Vector3 ( - 1f , 0f , 0f ) ;
3101+ }
3102+ }
3103+ public static Vector3 right
3104+ {
3105+ get
3106+ {
3107+ return new Vector3 ( 1f , 0f , 0f ) ;
3108+ }
3109+ }
3110+ [ Obsolete ( "Use Vector3.forward instead." ) ]
3111+ public static Vector3 fwd
3112+ {
3113+ get
3114+ {
3115+ return new Vector3 ( 0f , 0f , 1f ) ;
3116+ }
3117+ }
3118+ public Vector3 ( float x , float y , float z )
3119+ {
3120+ this . x = x ;
3121+ this . y = y ;
3122+ this . z = z ;
3123+ }
3124+ public Vector3 ( float x , float y )
3125+ {
3126+ this . x = x ;
3127+ this . y = y ;
3128+ this . z = 0f ;
3129+ }
3130+
3131+
3132+
3133+ public void Set ( float new_x , float new_y , float new_z )
3134+ {
3135+ this . x = new_x ;
3136+ this . y = new_y ;
3137+ this . z = new_z ;
3138+ }
3139+ public static Vector3 Scale ( Vector3 a , Vector3 b )
3140+ {
3141+ return new Vector3 ( a . x * b . x , a . y * b . y , a . z * b . z ) ;
3142+ }
3143+ public void Scale ( Vector3 scale )
3144+ {
3145+ this . x *= scale . x ;
3146+ this . y *= scale . y ;
3147+ this . z *= scale . z ;
3148+ }
3149+ public static Vector3 Cross ( Vector3 lhs , Vector3 rhs )
3150+ {
3151+ return new Vector3 ( lhs . y * rhs . z - lhs . z * rhs . y , lhs . z * rhs . x - lhs . x * rhs . z , lhs . x * rhs . y - lhs . y * rhs . x ) ;
3152+ }
3153+ public override int GetHashCode ( )
3154+ {
3155+ return this . x . GetHashCode ( ) ^ this . y . GetHashCode ( ) << 2 ^ this . z . GetHashCode ( ) >> 2 ;
3156+ }
3157+ public override bool Equals ( object other )
3158+ {
3159+ if ( ! ( other is Vector3 ) )
3160+ {
3161+ return false ;
3162+ }
3163+ Vector3 vector = ( Vector3 ) other ;
3164+ return this . x . Equals ( vector . x ) && this . y . Equals ( vector . y ) && this . z . Equals ( vector . z ) ;
3165+ }
3166+ public static Vector3 Reflect ( Vector3 inDirection , Vector3 inNormal )
3167+ {
3168+ return - 2f * Vector3 . Dot ( inNormal , inDirection ) * inNormal + inDirection ;
3169+ }
3170+
3171+ public override string ToString ( )
3172+ {
3173+ return String . Format ( "({0:F1}, {1:F1}, {2:F1})" , new object [ ]
3174+ {
3175+ this . x ,
3176+ this . y ,
3177+ this . z
3178+ } ) ;
3179+ }
3180+ public string ToString ( string format )
3181+ {
3182+ return String . Format ( "({0}, {1}, {2})" , new object [ ]
3183+ {
3184+ this . x . ToString ( format ) ,
3185+ this . y . ToString ( format ) ,
3186+ this . z . ToString ( format )
3187+ } ) ;
3188+ }
3189+ public static float Dot ( Vector3 lhs , Vector3 rhs )
3190+ {
3191+ return lhs . x * rhs . x + lhs . y * rhs . y + lhs . z * rhs . z ;
3192+ }
3193+
3194+
3195+ public static float SqrMagnitude ( Vector3 a )
3196+ {
3197+ return a . x * a . x + a . y * a . y + a . z * a . z ;
3198+ }
3199+
3200+ public static Vector3 operator + ( Vector3 a , Vector3 b )
3201+ {
3202+ return new Vector3 ( a . x + b . x , a . y + b . y , a . z + b . z ) ;
3203+ }
3204+ public static Vector3 operator - ( Vector3 a , Vector3 b )
3205+ {
3206+ return new Vector3 ( a . x - b . x , a . y - b . y , a . z - b . z ) ;
3207+ }
3208+ public static Vector3 operator - ( Vector3 a )
3209+ {
3210+ return new Vector3 ( - a . x , - a . y , - a . z ) ;
3211+ }
3212+ public static Vector3 operator * ( Vector3 a , float d )
3213+ {
3214+ return new Vector3 ( a . x * d , a . y * d , a . z * d ) ;
3215+ }
3216+ public static Vector3 operator * ( float d , Vector3 a )
3217+ {
3218+ return new Vector3 ( a . x * d , a . y * d , a . z * d ) ;
3219+ }
3220+ public static Vector3 operator / ( Vector3 a , float d )
3221+ {
3222+ return new Vector3 ( a . x / d , a . y / d , a . z / d ) ;
3223+ }
3224+ public static bool operator == ( Vector3 lhs , Vector3 rhs )
3225+ {
3226+ return Vector3 . SqrMagnitude ( lhs - rhs ) < 9.99999944E-11f ;
3227+ }
3228+ public static bool operator != ( Vector3 lhs , Vector3 rhs )
3229+ {
3230+ return Vector3 . SqrMagnitude ( lhs - rhs ) >= 9.99999944E-11f ;
3231+ }
3232+ }
29973233}
0 commit comments