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:

         private void BuildSpriteChangeCurve(ref AnimationClip clip, KeyValuePair> timeline)
         {
             // First you need to create Editor Curve Binding
             EditorCurveBinding curveBinding = new EditorCurveBinding();

             // I want to change the sprites of the sprite renderer, so I put the typeof(SpriteRenderer) as the binding type.
             curveBinding.type = typeof(SpriteRenderer);

             // Regular path to the GameObject that will be changed
             curveBinding.path = timeline.Key;

             // This is the property name to change the sprite of a sprite renderer
             curveBinding.propertyName = "m_Sprite";

             // An array to hold the object keyframes
             ObjectReferenceKeyframe[] keyFrames = new ObjectReferenceKeyframe[timeline.Value.Count];

             int i = 0;
             foreach (var key in timeline.Value)
             {
                 keyFrames[i] = new ObjectReferenceKeyframe();
                 // set the time
                 keyFrames[i].time = key.Time;
                 // set reference for the sprite you want
                 keyFrames[i].value = key.Sprite;
                 i++;

             }

             AnimationUtility.SetObjectReferenceCurve(clip, curveBinding, keyFrames);
         }