Attach this script to any gameObject. Note in the inspector 2 rectangular windows with the assigned var names. Click on a window. You are now in the curve editor. Select one of the preset curves at the bottom, then add / remove / move / rotate nodes to form curves.
I personally like to keep my values ‘normalized’ i.e. on the positive axis between 0 and 1. But as long as you know what range you are accessing and keep that in mind when forming your curves, there shouldn’t be a problem. Infact from memory I once called a value x that had no curve that high, and it returned 0, but definitely no error. Have a play around. If you read x as time % 1 and affect a transform of a cube, you’ll have a visual representation of how the curve is read. ut basically for a 2D graph it is give me Y for value X
#pragma strict
public var curveForceA : AnimationCurve;
public var curveForceB : AnimationCurve;
public var valueX : float;
function Update()
{
if ( Input.GetMouseButtonUp(0) )
{
var curveA : float = curveForceA.Evaluate( valueX );
Debug.Log( " Force A returned " + curveA );
var curveB : float = curveForceB.Evaluate( valueX );
Debug.Log( " Force B returned " + curveB );
}
}
I took a while typing the description =]
I have written an example of a curve reader, this can be done without playing the scene. YOu need to change the script, typecast the variables to the names of your scripts, and also name the curves the same as the var names in the script to be copied.
So for my example, the script to be copied is CurvesExample, it is the above script.
I have an empty gameObject with a script called CurvesBackup1, this is all that is on it :
#pragma strict
public var curveForceA : AnimationCurve;
public var curveForceB : AnimationCurve;
function Start() {
}
function Update() {
}
And finally an empty gameobject with the script reader. Drag and drop the example object and the backup object in the inspector, then right-click on the inspector and select “Read Curves”
The curve window don’t update straight away, you have to click off the object and then back on. Check the backup object has the curves copied to it, including the changes in tangent handles.
To write a curve to a script, just reverse-engineer the below to read from backup, and write to the script using. Here is the read script :
#pragma strict
#if UNITY_EDITOR
public var scriptExample : CurvesExample;
public var scriptBackup : CurvesBackup1;
public var curveForceA : AnimationCurve;
public var curveForceB : AnimationCurve;
private var ks : Keyframe[];
@ContextMenu ("Read Curves")
function ReadCurves()
{
Debug.Log("Reading Curves from ContextMenu");
// curveForceA
ks = new Keyframe[ scriptExample.curveForceA.length ];
for ( var i:int = 0; i < scriptExample.curveForceA.length; i ++ )
{
ks _= Keyframe( scriptExample.curveForceA*.time,*_
_ scriptExample.curveForceA*.value,
scriptExample.curveForceA.inTangent,
scriptExample.curveForceA.outTangent );
}*_
* curveForceA = new AnimationCurve( ks ); // Read and Store Curves from scriptExample*
* scriptBackup.curveForceA = new AnimationCurve( ks ); // Write Curves to scriptBackup*
* // curveForceB*
* ks = new Keyframe[ scriptExample.curveForceB.length ];*
* for ( i = 0; i < scriptExample.curveForceB.length; i ++ )*
* {*
ks = Keyframe( scriptExample.curveForceB*.time,*
_ scriptExample.curveForceB*.value,
scriptExample.curveForceB.inTangent,
scriptExample.curveForceB.outTangent );
}*_
* curveForceB = new AnimationCurve( ks ); // Read and Store Curves from scriptExample*
* scriptBackup.curveForceB = new AnimationCurve( ks ); // Write Curves to scriptBackup*
}
#endif
to use @ContextMenu, right-click where it says Curves Reader (Script) in bold :
[3681-readcurves.png|3681]