// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using UnityEngine; namespace Microsoft.MixedReality.Toolkit.Experimental.UI { /// /// Represents a position where a object can be docked. /// This component also adds a Collider and a Rigidbody, if they're not already present. /// /// /// [AddComponentMenu("Scripts/MRTK/Experimental/Dock/DockPosition")] [RequireComponent(typeof(Collider), typeof(Rigidbody))] public class DockPosition : MonoBehaviour { /// /// The object that is currently docked in this position (can be null). /// [Experimental] [SerializeField] [Tooltip("The object that is currently docked in this position (can be null).")] private Dockable dockedObject = null; /// /// The object that is currently docked in this position (can be null). /// public Dockable DockedObject { get => dockedObject; set => dockedObject = value; } /// /// True if this position is occupied, false otherwise. /// public bool IsOccupied => dockedObject != null; /// /// Ensure this object has a triggering collider, and ensure that /// this object doesn't block manipulations. /// public void Awake() { // Don't raycast this object to prevent blocking collisions gameObject.layer = LayerMask.NameToLayer("Ignore Raycast"); // Ensure there's a trigger collider for this position // The shape can be customized, but this adds a box as default. var collider = gameObject.GetComponent(); if (collider == null) { collider = gameObject.AddComponent(); } collider.isTrigger = true; // Ensure this collider can be used as a trigger by having // a RigidBody attached to it. var rigidBody = gameObject.EnsureComponent(); rigidBody.isKinematic = true; } /// /// If an object was set to be docked to this at start up, ensure it's docked. /// public void Start() { if (dockedObject != null) { dockedObject.Dock(this); } } } }