using System.Collections.Generic; using UnityEngine; public class AnchoredObject : MonoBehaviour { [SerializeField] List anchors = new List(); /// /// Get an anchor from its ID (index) /// /// ID of the anchor to search for /// The anchor that has the given ID public Anchor GetAnchorFromID(int anchorID) { if (anchorID > anchors.Count) { Debug.LogError("Error: Tried to anchor to an inexisting anchor. Check anchors list."); } return anchors[anchorID]; } /// /// Anchor this object to another /// /// The anchored object to connect to /// ID of the partner anchor to connect to /// ID of anchor this object wants to connect /// Whether to anchor scales too public void AnchorTo(AnchoredObject partner, int partnerAnchorID, int myAnchorID, bool matchScales=true) { AnchorTo( GetAnchorFromID(myAnchorID), partner.GetAnchorFromID(partnerAnchorID) ); } /// /// Anchor this object to another /// /// The anchor to connect /// The anchor of the other object to connect to /// Whether to anchor scales too public void AnchorTo(Anchor myAnchor, Anchor partnerAnchor, bool matchScales=true) { if (matchScales) { // Match scales float sizeDelta = partnerAnchor.transform.lossyScale.x / myAnchor.transform.lossyScale.x; transform.localScale *= sizeDelta; } // Match rotations transform.rotation = partnerAnchor.transform.rotation * Quaternion.AngleAxis(180f, Vector3.up) * Quaternion.Inverse(myAnchor.transform.rotation) * transform.rotation; // Match positions transform.position += partnerAnchor.transform.position - myAnchor.transform.position; } /// /// Connect multiple anchors of this object with multiple anchors. /// If one of the connections fails, the anchoring operation fails. /// /// The anchored object to connect to /// IDs of the partner anchors to connect to /// IDs of the anchors this object wants to connect /// Whether to anchor scales too /// Whether the anchoring succeeded or not public bool AnchorToMultiple(AnchoredObject partner, IList partnerAnchorIDs, IList myAnchorIDs, bool matchScales=true) { if (myAnchorIDs.Count != partnerAnchorIDs.Count) { Debug.LogError("Error: Number of anchors must match."); } List myAnchors = new List(); List partnerAnchors = new List(); for (int i = 0; i < myAnchorIDs.Count; i++) { myAnchors.Add(GetAnchorFromID(myAnchorIDs[i])); partnerAnchors.Add(partner.GetAnchorFromID(partnerAnchorIDs[i])); } return AnchorToMultiple(myAnchors, partnerAnchors, matchScales); } /// /// Connect multiple anchors of this object with multiple anchors. /// If one of the connections fails, the anchoring operation fails. /// /// The anchors this object wants to connect /// The anchors this object wants to connect to /// Whether to anchor scales too /// Whether the anchoring succeeded or not public bool AnchorToMultiple(IList myAnchors, IList targetAnchors, bool matchScales=true) { if (myAnchors.Count != targetAnchors.Count) { Debug.LogError("Error: Number of anchors must match."); return false; } if (myAnchors.Count == 0) { Debug.LogError("Amount of anchors to connect must be at least 1."); return false; } // Connect the first anchors. Then, test if other anchors match. AnchorTo(myAnchors[0], targetAnchors[0], matchScales); for (int i=1; i