MRTK-TransformConstraint

作者:追风剑情 发布于:2023-7-13 11:28 分类:Unity3d

所有约束的基类。参见 MRTK2-约束管理器

派生类有:

  • FaceUserConstraint
  • FixedDistanceConstraint
  • FixedRotationToUserConstraint
  • FixedRotationToWorldConstraint
  • MaintainApparentSizeConstraint
  • MinMaxScaleConstraint
  • MoveAxisConstraint
  • RotationAxisConstraint

源码分析

using Microsoft.MixedReality.Toolkit.Utilities;
using UnityEngine;

namespace Microsoft.MixedReality.Toolkit.UI
{
    /// <summary>
    /// 所有约束的基类
    /// </summary>
    public abstract class TransformConstraint : MonoBehaviour
    {
        #region Properties

        [SerializeField]
        [EnumFlags]
        [Tooltip("此约束适用于哪种类型的操纵。默认为“单手操纵”和“双手操纵”。")]
        private ManipulationHandFlags handType = ManipulationHandFlags.OneHanded | ManipulationHandFlags.TwoHanded;

        public ManipulationHandFlags HandType
        {
            get => handType;
            set => handType = value;
        }

        [SerializeField]
        [EnumFlags]
        [Tooltip("此约束适用于哪种类型的操纵。默认为“近”和“远”。")]
        private ManipulationProximityFlags proximityType = ManipulationProximityFlags.Near | ManipulationProximityFlags.Far;

        public ManipulationProximityFlags ProximityType
        {
            get => proximityType;
            set => proximityType = value;
        }

        [SerializeField]
        [Tooltip("此约束的执行顺序优先级。较低的数字将在较高的数字之前执行")]
        private int executionOrder = 0;

        public int ExecutionPriority
        {
            get => executionOrder;
            set
            {
                executionOrder = value;

                // 通知所有约束管理器重新排序这些优先级。
                foreach (var mgr in gameObject.GetComponents<ConstraintManager>())
                {
                    mgr.RefreshPriorities();
                }
            }
        }

        protected MixedRealityTransform worldPoseOnManipulationStart;

        public abstract TransformFlags ConstraintType { get; }

        #endregion Properties

        #region Public Methods

        /// <summary>
        /// 操纵开始时调用
        /// </summary>
        public virtual void Initialize(MixedRealityTransform worldPose)
        {
            worldPoseOnManipulationStart = worldPose;
        }

        /// <summary>
        /// 在操作过程中将约束应用于变换的抽象方法
        /// </summary>
        public abstract void ApplyConstraint(ref MixedRealityTransform transform);

        #endregion Public Methods

        #region MonoBeaviour
        protected void OnEnable()
        {
	    //向管理器注册约束
            var managers = gameObject.GetComponents<ConstraintManager>();
            foreach (var manager in managers)
            {
                manager.AutoRegisterConstraint(this);
            }
        }

        protected void OnDisable()
        {
	    //注销约束
            var managers = gameObject.GetComponents<ConstraintManager>();
            foreach (var manager in managers)
            {
                manager.AutoUnregisterConstraint(this);
            }
        }

        #endregion
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号