142 lines
5.0 KiB
C#
142 lines
5.0 KiB
C#
using Microsoft.MixedReality.Toolkit.UI.BoundsControl;
|
|
using UnityEngine;
|
|
/// <summary>
|
|
/// 旋转轴
|
|
/// </summary>
|
|
public enum RotateAxis
|
|
{
|
|
X,
|
|
Y,
|
|
Z
|
|
}
|
|
/// <summary>
|
|
/// 限制旋转角度
|
|
/// </summary>
|
|
public class LimitRotate : MonoBehaviour
|
|
{
|
|
public RotateAxis axis = new RotateAxis();
|
|
/// <summary>
|
|
/// 是否选中抓取
|
|
/// </summary>
|
|
[HideInInspector]
|
|
public bool IsSelet = false;
|
|
/// <summary>
|
|
/// 限制最小角度
|
|
/// </summary>
|
|
public float limitMin;
|
|
/// <summary>
|
|
/// 限制最大角度
|
|
/// </summary>
|
|
public float limitMax;
|
|
/// <summary>
|
|
/// 偏移角度
|
|
/// </summary>
|
|
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<BoundsControl>().RotateStarted.AddListener(RotateStarted);
|
|
GetComponent<BoundsControl>().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<BoundsControl>().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<BoundsControl>().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<BoundsControl>().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<BoundsControl>().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<BoundsControl>().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<BoundsControl>().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<BoundsControl>().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();
|
|
}
|
|
}
|