using Microsoft.MixedReality.Toolkit.UI.BoundsControl; using UnityEngine; /// /// 旋转轴 /// public enum RotateAxis { X, Y, Z } /// /// 限制旋转角度 /// public class LimitRotate : MonoBehaviour { public RotateAxis axis = new RotateAxis(); /// /// 是否选中抓取 /// [HideInInspector] public bool IsSelet = false; /// /// 限制最小角度 /// public float limitMin; /// /// 限制最大角度 /// public float limitMax; /// /// 偏移角度 /// private float offset = 20; public FunctionSync_PositionRoate sync; #region audio public FunctionSync_Audio sync_OpenAudio; public FunctionSync_Audio sync_CloseAudio; #endregion // Start is called before the first frame update void Start() { GetComponent().RotateStarted.AddListener(RotateStarted); GetComponent().RotateStopped.AddListener(RotateStoped); } // Update is called once per frame void Update() { } private void LateUpdate() { //Debug.Log(transform.localEulerAngles); switch (axis) { case RotateAxis.X: transform.localEulerAngles = new Vector3(Mathf.Clamp(transform.localEulerAngles.x, limitMin, limitMax), transform.localEulerAngles.y, transform.localEulerAngles.z); break; case RotateAxis.Y: transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, Mathf.Clamp(transform.localEulerAngles.y, limitMin, limitMax), transform.localEulerAngles.z); break; case RotateAxis.Z: transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, Mathf.Clamp(transform.localEulerAngles.z, limitMin, limitMax)); break; default: break; } } private void RecoverBounds() { GetComponent().Active = true; } public void Limit() { switch (axis) { case RotateAxis.X: if (transform.localEulerAngles.x >= limitMax - offset) { transform.localEulerAngles = new Vector3(limitMax, transform.localEulerAngles.y, transform.localEulerAngles.z); GetComponent().Active = false; AudioPlay(sync_OpenAudio); Invoke("RecoverBounds", 2f); } if (transform.localEulerAngles.x <= limitMin + offset) { transform.localEulerAngles = new Vector3(limitMin, transform.localEulerAngles.y, transform.localEulerAngles.z); GetComponent().Active = false; AudioPlay(sync_CloseAudio); Invoke("RecoverBounds", 2f); } break; case RotateAxis.Y: if (transform.localEulerAngles.y >= limitMax - offset) { transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, limitMax, transform.localEulerAngles.z); GetComponent().Active = false; AudioPlay(sync_OpenAudio); Invoke("RecoverBounds", 2f); } if (transform.localEulerAngles.y <= limitMin + offset) { transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, limitMin, transform.localEulerAngles.z); GetComponent().Active = false; AudioPlay(sync_CloseAudio); Invoke("RecoverBounds", 2f); } break; case RotateAxis.Z: if (transform.localEulerAngles.z >= limitMax - offset) { transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, limitMax); GetComponent().Active = false; AudioPlay(sync_OpenAudio); Invoke("RecoverBounds", 2f); } if (transform.localEulerAngles.z <= limitMin + offset) { transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, limitMin); GetComponent().Active = false; AudioPlay(sync_CloseAudio); Invoke("RecoverBounds", 2f); } break; default: break; } } public void AudioPlay(FunctionSync_Audio audioSource) { audioSource.SetAudio(AudioControlEnum.Play); } public void RotateStarted() { IsSelet = true; sync.GetControl(); } public void RotateStoped() { Limit(); IsSelet = false; sync.ReleaseControl(); } }