@@ -305,6 +305,11 @@ public virtual int Compare(Keyframe a, Keyframe b)
305
305
306
306
public class KeyTangentComparer : IComparer < Keyframe >
307
307
{
308
+ AnimationCurve curveA = null ;
309
+ AnimationCurve curveB = null ;
310
+ Dictionary < Keyframe , int > keyFrameToIndexA ;
311
+ Dictionary < Keyframe , int > keyFrameToIndexB ;
312
+
308
313
public int CompareKeyTangents ( Keyframe a , Keyframe b )
309
314
{
310
315
bool result = true ;
@@ -313,21 +318,55 @@ public int CompareKeyTangents(Keyframe a, Keyframe b)
313
318
#if DEBUG_UNITTEST
314
319
Debug . Log ( string . Format ( "{2} a.time: {0}, b.time: {1}" , a . time , b . time , result ) ) ;
315
320
#endif
316
- // TODO : use AnimationUtility.GetLeftTangentMode
317
- // requires reference to AnimationCurve and keyindex
318
- result &= ( a . tangentMode == b . tangentMode ) ;
321
+
322
+ result &= ( AnimationUtility . GetKeyLeftTangentMode ( curveA , keyFrameToIndexA [ a ] ) == AnimationUtility . GetKeyLeftTangentMode ( curveB , keyFrameToIndexB [ b ] ) ) ;
323
+ result &= ( AnimationUtility . GetKeyRightTangentMode ( curveA , keyFrameToIndexA [ a ] ) == AnimationUtility . GetKeyRightTangentMode ( curveB , keyFrameToIndexB [ b ] ) ) ;
319
324
#if DEBUG_UNITTEST
320
- Debug . Log ( string . Format ( "{2} a.tangentMode={0} b.tangentMode={1}" ,
321
- ( ( AnimationUtility . TangentMode ) a . tangentMode ) . ToString ( ) ,
322
- ( ( AnimationUtility . TangentMode ) b . tangentMode ) . ToString ( ) , result ) ) ;
325
+ Debug . Log ( string . Format ( "comparison result = {0}\n " +
326
+ "keyframe a left tangent mode = {1}\n " +
327
+ "keyframe b left tangent mode = {2}\n " +
328
+ "keyframe a right tangent mode = {3}\n " +
329
+ "keyframe b right tangent mode = {4}\n " ,
330
+ result ,
331
+ ( ( AnimationUtility . TangentMode ) AnimationUtility . GetKeyLeftTangentMode ( curveA , keyFrameToIndexA [ a ] ) ) . ToString ( ) ,
332
+ ( ( AnimationUtility . TangentMode ) AnimationUtility . GetKeyLeftTangentMode ( curveB , keyFrameToIndexB [ b ] ) ) . ToString ( ) ,
333
+ ( ( AnimationUtility . TangentMode ) AnimationUtility . GetKeyRightTangentMode ( curveA , keyFrameToIndexA [ a ] ) ) . ToString ( ) ,
334
+ ( ( AnimationUtility . TangentMode ) AnimationUtility . GetKeyRightTangentMode ( curveB , keyFrameToIndexB [ b ] ) ) . ToString ( ) ) ) ;
323
335
#endif
324
336
return result ? 0 : 1 ;
325
337
}
326
338
327
339
public int Compare ( Keyframe a , Keyframe b )
328
340
{
341
+ if ( curveA == null || curveB == null )
342
+ {
343
+ #if DEBUG_UNITTEST
344
+ Debug . Log ( string . Format ( "The animation curves were not set for this KeyTangentComparer});
345
+ #endif
346
+
347
+ return 1 ;
348
+ }
329
349
return CompareKeyTangents ( a , b ) ;
330
350
}
351
+
352
+ public void SetAnimationCurves ( AnimationCurve a , AnimationCurve b )
353
+ {
354
+ curveA = a;
355
+ curveB = b;
356
+
357
+ // Keep a dictionary of Keyframe->index for both curves
358
+ keyFrameToIndexA = new Dictionary < Keyframe , int > ( ) ;
359
+ foreach ( int index in Enumerable . Range ( 0 , a . keys . Length ) )
360
+ {
361
+ keyFrameToIndexA [ a . keys [ index ] ] = index ;
362
+ }
363
+
364
+ keyFrameToIndexB = new Dictionary < Keyframe , int > ( ) ;
365
+ foreach ( int index in Enumerable . Range ( 0 , b . keys . Length ) )
366
+ {
367
+ keyFrameToIndexB [ b . keys [ index ] ] = index ;
368
+ }
369
+ }
331
370
}
332
371
333
372
public class AnimTester
@@ -552,6 +591,13 @@ public static void KeysTest (AnimationCurve expectedAnimCurve, AnimationCurve ac
552
591
Assert . That ( actualAnimCurve . length , Is . EqualTo ( expectedAnimCurve . length ) , string . Format ( "{0} number of keys doesn't match" , message ) ) ;
553
592
554
593
Assert . That ( actualAnimCurve . keys , Is . EqualTo ( expectedAnimCurve . keys ) . Using < Keyframe > ( keyComparer ) , string . Format ( "{0} key doesn't match" , message ) ) ;
594
+
595
+ // Setup our tangent comparer if it is the one we are using
596
+ var tangentComparer = keyComparer as KeyTangentComparer ;
597
+ if ( tangentComparer != null )
598
+ {
599
+ tangentComparer . SetAnimationCurves ( actualAnimCurve , expectedAnimCurve ) ;
600
+ }
555
601
}
556
602
557
603
public static void KeysTest ( float [ ] expectedKeyTimes , float [ ] expectedKeyValues , AnimationCurve actualAnimCurve , string message , IComparer < Keyframe > keyComparer = null )
0 commit comments