Created
June 11, 2021 03:17
-
-
Save liveasnotes/3e0a654bc6450153eeeb679845d073ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // cf. | |
| // https://gametukurikata.com/program/ghost | |
| // https://xr-hub.com/archives/20326 | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| namespace liveasnotes | |
| { | |
| public class LogTransform : MonoBehaviour | |
| { | |
| // if you want to deactivate this component, remove or just deactivate. | |
| // it is simple for machine that is in runtime rather than using bool variable. | |
| // todo | |
| // public bool AutoSetDataSavePath | |
| // public bool ReplayContinuously; //for use of movePosition | |
| // public bool SaveLogAsAnimation; | |
| public bool ReplayMode; | |
| public enum ActivationMode | |
| { | |
| Record, | |
| Replay, | |
| } | |
| public ActivationMode Mode; | |
| public bool ResetReplayIndex; | |
| public bool LoopReplayIndex; | |
| public Transform ReferenceTransform; | |
| public Transform TargetTransform; | |
| public Vector3 PositionOffset; | |
| public string DataSavePath; | |
| string dataSaveFullPath | |
| { | |
| get { return Application.dataPath + DataSavePath; } | |
| } | |
| [SerializeField] float activationTimeStep = 0.02f; | |
| int replayIndex = 0; | |
| float activationDeltaTime = 0; | |
| [System.Serializable] | |
| class TransformData | |
| { | |
| public int length = 0; | |
| List<Vector3> positionList = new List<Vector3>(); | |
| List<Quaternion> rotationList = new List<Quaternion>(); | |
| List<Vector3> localScaleList = new List<Vector3>(); | |
| public void Record(Transform reference) | |
| { | |
| positionList.Add(reference.position); | |
| rotationList.Add(reference.rotation); | |
| localScaleList.Add(reference.localScale); | |
| length += 1; | |
| } | |
| public void Make(Transform target, int index) | |
| { | |
| target.position = positionList[index]; | |
| target.rotation = rotationList[index]; | |
| target.localScale = localScaleList[index]; | |
| } | |
| } | |
| TransformData transformData = new TransformData(); | |
| void FixedUpdate() | |
| { | |
| Debug.Log("logging..."); | |
| activationDeltaTime += Time.fixedDeltaTime; | |
| // it's ok if the Fixed Timestep is narrow than the activation interval. | |
| if (activationDeltaTime > activationTimeStep) | |
| { | |
| while (activationDeltaTime > 0) | |
| { | |
| Activate(Mode); | |
| activationDeltaTime -= activationTimeStep; | |
| } | |
| activationDeltaTime = 0; | |
| } | |
| } | |
| void Activate(liveasnotes.LogTransform.ActivationMode mode) | |
| { | |
| if (mode == ActivationMode.Replay) | |
| { | |
| if (replayIndex < transformData.length) | |
| { | |
| transformData.Make(TargetTransform, replayIndex); | |
| replayIndex += 1; | |
| } | |
| else | |
| { | |
| if (LoopReplayIndex) | |
| { | |
| replayIndex = 0; | |
| } | |
| } | |
| } | |
| else | |
| // Record Mode | |
| { | |
| if (ResetReplayIndex) | |
| { | |
| replayIndex = 0; | |
| transformData.Make(TargetTransform, 0); | |
| } | |
| transformData.Record(ReferenceTransform); | |
| } | |
| } | |
| public void SaveData() | |
| { | |
| // "is not null" cause error because unity's default compiler is C# 8.0 and "not null" is available with C# 9.0 | |
| // cf. https://docs.unity3d.com/Manual/CSharpCompiler.html | |
| if (transformData != null && dataSaveFullPath != null) | |
| { | |
| string data = JsonUtility.ToJson(transformData); | |
| System.IO.File.WriteAllText(dataSaveFullPath, data); | |
| } | |
| } | |
| public void LoadData() | |
| { | |
| if (System.IO.File.Exists(dataSaveFullPath)) | |
| { | |
| string readAllText = System.IO.File.ReadAllText(dataSaveFullPath); | |
| JsonUtility.FromJsonOverwrite(readAllText, transformData); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment