Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- // this script will record the position and rotation of the
- // GameObject it is placed on, and when disabled (OnDisable()),
- // it will write it to a CSV file of your choosing.
- // @kurtdekker
- public class RollABallRecorder : MonoBehaviour
- {
- public string Filename;
- void Reset()
- {
- Filename = System.String.Format( "Position_Rotation_" + name + ".csv");
- }
- List<string> dataBuffer;
- int frameno;
- void Update ()
- {
- if (dataBuffer == null)
- {
- dataBuffer = new List<string>();
- }
- // grab
- var pos = transform.position;
- var rot = transform.rotation;
- // format the line
- string data = frameno.ToString() + ",";
- data += System.String.Format( "{0},{1},{2},", pos.x, pos.y, pos.z);
- // WARNING: These are NOT eulerAngles; these are the actual internal
- // quaternion values so you can reconstruct the rotation. You can also
- // store the rotation eulerAngles but they are subject to gimbal lock.
- data += System.String.Format( "{0},{1},{2},{3}", rot.x, rot.y, rot.z, rot.w);
- // store
- dataBuffer.Add( data);
- // next!
- frameno++;
- }
- // beware of when this is called. It may suit your needs to be much
- // more explicit in order to avoid overwriting the previous run's data.
- // Here is some timing diagram help:
- // https://docs.unity3d.com/Manual/ExecutionOrder.html
- void OnDisable()
- {
- if (dataBuffer != null)
- {
- System.IO.File.WriteAllLines( Filename, dataBuffer.ToArray());
- dataBuffer = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement