Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

         public static void AddKeyIfChanged(this AnimationCurve curve, Keyframe keyframe)
         {
             var keys = curve.keys;
             //If this is the first key on this curve, always add
             //NOTE: Add TWO copies of the first frame, then we adjust the last frame as we move along
             //This guarantees a minimum of two keys in each curve
             if (keys.Length == 0 || !ENABLE_KEYFRAME_REDUCATION)
             {
                 curve.AddKey(keyframe);
                 keyframe.time += float.Epsilon;
                 curve.AddKey(keyframe);
             }
             else
             {
                 //TODO: This method of keyframe reduction causes artifacts in animations that are supposed to deliberately pause
                 //Find the last keyframe
                 Keyframe lastKey = keys[keys.Length - 1];
                 if (lastKey.time >= keyframe.time)
                     Debug.LogError("Keyframes not supplied in consecutive order!!!");

                 //Grab 2 frames ago
                 var last2Key = keys[keys.Length - 2];

                 //If the previous 2 frames were different, add a new frame
                 if (lastKey.value != last2Key.value)
                 {
                     curve.AddKey(keyframe);
                 }
                 //The previous frame is redundant - just move it
                 else
                 {
                     curve.MoveKey(keys.Length - 1, keyframe);
                 }
             }
         }