/// <summary> /// Refreshes the sprite's position according to its anchor information. /// </summary> /// <param name="sprite"></param> public static void refreshPosition(this IPositionable sprite) { // Get sprite depth var depth = sprite.position.z; // Get anchor info var anchorInfo = sprite.anchorInfo; // Get parent anchor position var position = parentAnchorPosition(anchorInfo.ParentUIObject, anchorInfo.ParentUIyAnchor, anchorInfo.ParentUIxAnchor); // Add position offset if (anchorInfo.UIPrecision == UIPrecision.Percentage) { position.x += UIRelative.xPercentFrom(anchorInfo.UIxAnchor, parentWidth(anchorInfo.ParentUIObject), anchorInfo.OffsetX); position.y -= UIRelative.yPercentFrom(anchorInfo.UIyAnchor, parentHeight(anchorInfo.ParentUIObject), anchorInfo.OffsetY); } else { position.x += UIRelative.xPixelsFrom(anchorInfo.UIxAnchor, anchorInfo.OffsetX); position.y -= UIRelative.yPixelsFrom(anchorInfo.UIyAnchor, anchorInfo.OffsetY); } // Adjust for anchor offset position.x -= UIRelative.xAnchorAdjustment(anchorInfo.UIxAnchor, sprite.width, anchorInfo.OriginUIxAnchor); position.y += UIRelative.yAnchorAdjustment(anchorInfo.UIyAnchor, sprite.height, anchorInfo.OriginUIyAnchor); // Set depth position.z = depth; // Set new position sprite.position = position; }
/// <summary> /// Returns anchor position for a given parent and fallback to Screen if parent is null. /// </summary> /// <param name="sprite">Provided parent</param> /// <param name="yAnchor">Vertical anchor</param> /// <param name="xAnchor">Horizontal anchor</param> /// <returns>Adjusted anchor position</returns> private static Vector3 parentAnchorPosition(IPositionable sprite, UIyAnchor yAnchor, UIxAnchor xAnchor) { Vector3 position; float width, height; UIxAnchor originUIxAnchor = UIxAnchor.Left; UIyAnchor originUIyAnchor = UIyAnchor.Top; // Determine correct parent values if (sprite == null) { position = Vector3.zero; width = Screen.width; height = Screen.height; } else { position = sprite.position; width = sprite.width; height = sprite.height; originUIxAnchor = sprite.anchorInfo.OriginUIxAnchor; originUIyAnchor = sprite.anchorInfo.OriginUIyAnchor; } // Adjust anchor offset position.x += UIRelative.xAnchorAdjustment(xAnchor, width, originUIxAnchor); position.y -= UIRelative.yAnchorAdjustment(yAnchor, height, originUIyAnchor); return(position); }
/// <summary> /// Refreshes the sprite's anchoring offsets according to its parent and position. /// </summary> /// <param name="sprite"></param> public static void refreshAnchorInformation(this IPositionable sprite) { // Get anchor info UIAnchorInfo anchorInfo = sprite.anchorInfo; // Get anchor positions Vector3 parentPosition = parentAnchorPosition(anchorInfo.ParentUIObject, anchorInfo.ParentUIyAnchor, anchorInfo.ParentUIxAnchor); Vector3 diffAnchor = sprite.position - parentPosition; // Adjust for sprite anchor offset diffAnchor.x += UIRelative.xAnchorAdjustment(anchorInfo.UIxAnchor, sprite.width, anchorInfo.OriginUIxAnchor); diffAnchor.y -= UIRelative.yAnchorAdjustment(anchorInfo.UIyAnchor, sprite.height, anchorInfo.OriginUIyAnchor); // Adjust parent anchor offsets if (anchorInfo.UIPrecision == UIPrecision.Percentage) { anchorInfo.OffsetX = UIRelative.xPercentTo(anchorInfo.UIxAnchor, parentWidth(anchorInfo.ParentUIObject), diffAnchor.x); anchorInfo.OffsetY = -UIRelative.yPercentTo(anchorInfo.UIyAnchor, parentHeight(anchorInfo.ParentUIObject), diffAnchor.y); } else { anchorInfo.OffsetX = UIRelative.xPixelsTo(anchorInfo.UIxAnchor, diffAnchor.x); anchorInfo.OffsetY = -UIRelative.yPixelsTo(anchorInfo.UIyAnchor, diffAnchor.y); } // Set update anchor info sprite.anchorInfo = anchorInfo; }