ObjectIsSameWithInprecision









How do I use Object Is Same With Inprecision
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: NetworkingPeer.cs Copy
3396     private bool AlmostEquals(object[] lastData, object[] currentContent)
3397     {
3398         if (lastData == null && currentContent == null)
3399         {
3400             return true;
3401         }
3402
3403         if (lastData == null || currentContent == null || (lastData.Length != currentContent.Length))
3404         {
3405             return false;
3406         }
3407
3408         for (int index = 0; index < currentContent.Length; index++)
3409         {
3410             object newObj = currentContent[index];
3411             object oldObj = lastData[index];
3412             if (!this.ObjectIsSameWithInprecision(newObj, oldObj))
3413             {
3414                 return false;
3415             }
3416         }
3417
3418         return true;
3419     }
File name: NetworkingPeer.cs Copy
3425     private bool DeltaCompressionWrite(PhotonView view, Hashtable data)
3426     {
3427         if (view.lastOnSerializeDataSent == null)
3428         {
3429             return true; // all has to be sent
3430         }
3431
3432         // We can compress as we sent a full update previously (readers can re-use previous values)
3433         object[] lastData = view.lastOnSerializeDataSent;
3434         object[] currentContent = data[(byte)1] as object[];
3435
3436         if (currentContent == null)
3437         {
3438             // no data to be sent
3439             return false;
3440         }
3441
3442         if (lastData.Length != currentContent.Length)
3443         {
3444             // if new data isn't same length as before, we send the complete data-set uncompressed
3445             return true;
3446         }
3447
3448         object[] compressedContent = new object[currentContent.Length];
3449         int compressedValues = 0;
3450
3451         List valuesThatAreChangedToNull = new List();
3452         for (int index = 0; index < compressedContent.Length; index++)
3453         {
3454             object newObj = currentContent[index];
3455             object oldObj = lastData[index];
3456             if (this.ObjectIsSameWithInprecision(newObj, oldObj))
3457             {
3458                 // compress (by using null, instead of value, which is same as before)
3459                 compressedValues++;
3460                 // compressedContent[index] is already null (initialized)
3461             }
3462             else
3463             {
3464                 compressedContent[index] = currentContent[index];
3465
3466                 // value changed, we don't replace it with null
3467                 // new value is null (like a compressed value): we have to mark it so it STAYS null instead of being replaced with previous value
3468                 if (newObj == null)
3469                 {
3470                     valuesThatAreChangedToNull.Add(index);
3471                 }
3472             }
3473         }
3474
3475         // Only send the list of compressed fields if we actually compressed 1 or more fields.
3476         if (compressedValues > 0)
3477         {
3478             data.Remove((byte)1); // remove the original data (we only send compressed data)
3479
3480             if (compressedValues == currentContent.Length)
3481             {
3482                 // all values are compressed to null, we have nothing to send
3483                 return false;
3484             }
3485
3486             data[(byte)2] = compressedContent; // current, compressted data is moved to key 2 to mark it as compressed
3487             if (valuesThatAreChangedToNull.Count > 0)
3488             {
3489                 data[(byte)3] = valuesThatAreChangedToNull.ToArray(); // data that is actually null (not just cause we didn't want to send it)
3490             }
3491         }
3492
3493         return true; // some data was compressed but we need to send something
3494     }
File name: NetworkingPeer.cs Copy
3546     bool ObjectIsSameWithInprecision(object one, object two)
3547     {
3548         if (one == null || two == null)
3549         {
3550             return one == null && two == null;
3551         }
3552
3553         if (!one.Equals(two))
3554         {
3555             // if A is not B, lets check if A is almost B
3556             if (one is Vector3)
3557             {
3558                 Vector3 a = (Vector3)one;
3559                 Vector3 b = (Vector3)two;
3560                 if (a.AlmostEquals(b, PhotonNetwork.precisionForVectorSynchronization))
3561                 {
3562                     return true;
3563                 }
3564             }
3565             else if (one is Vector2)
3566             {
3567                 Vector2 a = (Vector2)one;
3568                 Vector2 b = (Vector2)two;
3569                 if (a.AlmostEquals(b, PhotonNetwork.precisionForVectorSynchronization))
3570                 {
3571                     return true;
3572                 }
3573             }
3574             else if (one is Quaternion)
3575             {
3576                 Quaternion a = (Quaternion)one;
3577                 Quaternion b = (Quaternion)two;
3578                 if (a.AlmostEquals(b, PhotonNetwork.precisionForQuaternionSynchronization))
3579                 {
3580                     return true;
3581                 }
3582             }
3583             else if (one is float)
3584             {
3585                 float a = (float)one;
3586                 float b = (float)two;
3587                 if (a.AlmostEquals(b, PhotonNetwork.precisionForFloatSynchronization))
3588                 {
3589                     return true;
3590                 }
3591             }
3592
3593             // one does not equal two
3594             return false;
3595         }
3596
3597         return true;
3598     }

ObjectIsSameWithInprecision 128 lượt xem

Gõ tìm kiếm nhanh...