// 27 Slicer // Copyright 2021 Deftly Games // https://slicer.deftly.games/ using UnityEngine; using Slicer.Core; using System; namespace Slicer { /// /// Modifies the position of the GameObject, as if its position vector is being sliced. /// /// ///

[REFERENCE MANUAL](xref:manual\components\position_slice_modifier) ///
[ExecuteAlways] [DisallowMultipleComponent] [AddComponentMenu("Slicer/Position Slice Modifier")] [HelpURL(SlicerConfiguration.SiteUrl + SlicerConfiguration.ComponentsManualPath + "position_slice_modifier.html")] public class PositionSliceModifier : SliceModifier { /// /// The anchor (offset) position to be used by this modifier. /// [Tooltip("The anchor (offset) position to be used by this modifier.")] public Vector3 Anchor; [HideInInspector] [SerializeField] private Vector3 originalPosition = Vector3.zero; /// /// The original position of the Game Object /// public Vector3 OriginalPosition { get { return originalPosition; } } [HideInInspector] [SerializeField] private Vector3 slicedPosition; /// /// The sliced position of the Game Object /// public Vector3 SlicedPosition { get { return slicedPosition; } } /// public override Hash128 GatherDetails() { var hash = base.GatherDetails(); var anchorHash = HashUtility.CalculateHash(Anchor); HashUtility.AppendHash(anchorHash, ref hash); if (!ModifierEnabled) { if (ShouldUpdate()) { originalPosition = transform.localPosition; } } var positionHash = HashUtility.CalculateHash(originalPosition); HashUtility.AppendHash(positionHash, ref hash); return hash; } /// public override void Modify(Vector3 size, Transform rootTransform, Bounds completeBounds, Bounds slicedBounds) { if (ModifierEnabled) { var anchor = transform.localRotation * Anchor; var newPos = SliceUtility.SliceVector(originalPosition + anchor, size, completeBounds, slicedBounds); slicedPosition = newPos - anchor; transform.localPosition = slicedPosition; } } /// public override void DisableModifier() { base.DisableModifier(); if (transform != null) { transform.localPosition = originalPosition; } } /// public override void EnableModifier() { if (transform != null) { transform.localPosition = slicedPosition; } base.EnableModifier(); } /// public override void FinalizeSlicing() { EnableModifier(); originalPosition = slicedPosition; SlicerController.SafeDestroy(this); } private void OnDestroy() { DisableModifier(); } /// /// Sets the position of the Game Object before slicing. /// /// /// Call for this change to be sliced. /// /// The new unsliced position to set public void SetUnslicedPosition(Vector3 position) { originalPosition = position; if (!ModifierEnabled) { transform.localPosition = originalPosition; } } } }