namespace VRTK.Prefabs.Interactions.Interactables { using UnityEngine; using Malimbe.MemberClearanceMethod; using Malimbe.XmlDocumentationAttribute; using Malimbe.PropertySerializationAttribute; using Malimbe.BehaviourStateRequirementMethod; /// /// Caches common properties for an to be restored at a later point in time. /// public class InteractablePropertyCache : MonoBehaviour { /// /// The source to cache properties for. /// [Serialized, Cleared] [field: DocumentedByXml] public InteractableFacade Source { get; set; } /// /// The cached local position of the /// protected Vector3? cachedLocalPosition; /// /// The cached local rotation of the /// protected Quaternion cachedLocalRotation; /// /// The cached local scale of the /// protected Vector3 cachedLocalScale; /// /// The cached kinematic state of the the /// protected bool cachedRigidbodyKinematicState; /// /// Sets the property from a given . /// /// The source to set to. [RequiresBehaviourState] public virtual void SetSource(GameObject source) { if (source == null) { return; } Source = source.GetComponent(); } /// /// Caches the position. /// [RequiresBehaviourState] public virtual void CachePosition() { if (Source == null) { return; } cachedLocalPosition = Source.transform.localPosition; } /// /// Caches the rotation. /// [RequiresBehaviourState] public virtual void CacheRotation() { if (Source == null) { return; } cachedLocalRotation = Source.transform.localRotation; } /// /// Caches the scale. /// [RequiresBehaviourState] public virtual void CacheScale() { if (Source == null) { return; } cachedLocalScale = Source.transform.localScale; } /// /// Caches the rigidbody kinematic state.. /// [RequiresBehaviourState] public virtual void CacheRigidbodyKinematicState() { if (Source == null) { return; } cachedRigidbodyKinematicState = Source.ConsumerRigidbody != null ? Source.ConsumerRigidbody.isKinematic : false; } /// /// Caches all of the properties. /// [RequiresBehaviourState] public virtual void CacheAll() { CachePosition(); CacheRotation(); CacheScale(); CacheRigidbodyKinematicState(); } /// /// Restores the cached position. /// [RequiresBehaviourState] public virtual void RestorePosition() { if (Source == null) { return; } Source.transform.localPosition = (Vector3)cachedLocalPosition; } /// /// Restores the cached rotation. /// [RequiresBehaviourState] public virtual void RestoreRotation() { if (Source == null) { return; } Source.transform.localRotation = cachedLocalRotation; } /// /// Restores the cached scale. /// [RequiresBehaviourState] public virtual void RestoreScale() { if (Source == null) { return; } Source.transform.localScale = cachedLocalScale; } /// /// Restores the cached rigidbody kinematic state. /// [RequiresBehaviourState] public virtual void RestoreRigidbodyKinematicState() { if (Source == null) { return; } Source.ConsumerRigidbody.isKinematic = cachedRigidbodyKinematicState; } /// /// Restores the all of cached properties. /// [RequiresBehaviourState] public virtual void RestoreAll() { RestorePosition(); RestoreRotation(); RestoreScale(); RestoreRigidbodyKinematicState(); } } }