1
0
Fork 0
LaboratoryProtection/Assets/UnityTest/功能模块/_5模型预览/Scripts/ModelViewBridge.cs

112 lines
3.1 KiB
C#

using Cysharp.Threading.Tasks;
using Cysharp.Threading.Tasks.Linq;
using UniRx;
using UniRx.Triggers;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ModelViewBridge : MonoBehaviour
{
public int index;
public float speed = 150;
public bool isClamp = false;
public ModelViewLogic modelViewLogic;
[SerializeField]
private FloatReactiveProperty _angle;
[SerializeField]
private Button[] _btns;
[SerializeField]
private GameObject _view;
private void Reset()
{
this._btns = this.GetComponentsInChildren<Button>(true);
this._view = this.transform.Find("image_view").gameObject;
this.modelViewLogic = this.GetComponentInChildren<ModelViewLogic>(true);
}
private async void Start()
{
_angle = new FloatReactiveProperty(0);
var token = this.GetCancellationTokenOnDestroy();
// 旋转按钮逻辑
this.UpdateAsObservable()
.Where(_ => Input.GetMouseButton(0) == true)
.Subscribe(_ => {
Debug.Log(EventSystem.current.currentSelectedGameObject);
var current = EventSystem.current.currentSelectedGameObject;
if (current == _btns[1].gameObject)
{
var value = this._angle.Value - speed * Time.deltaTime;
if (isClamp == true)
{
this._angle.Value = Mathf.Clamp(value, -180, 180);
}
else
{
this._angle.Value = value;
}
}
else if (current == _btns[0].gameObject)
{
var value = this._angle.Value + speed * Time.deltaTime;
if (isClamp == true)
{
this._angle.Value = Mathf.Clamp(value, -180, 180);
}
else
{
this._angle.Value = value;
}
}
})
.AddTo(this);
// 同步逻辑
this._angle
.Subscribe(_ => {
// 旋转
this.modelViewLogic.SetRotate(new Vector3(0, _, 0));
})
.AddTo(this);
await this.modelViewLogic.InitAsync(token);
this.modelViewLogic.ShowModel(this.index);
this._view.SetActive(true);
}
public void StartLogic()
{
this.modelViewLogic.HideAll();
this.modelViewLogic.ResetView();
}
public void NormalLogic()
{
this.modelViewLogic.HideAll();
this.modelViewLogic.ResetView();
}
private async void OnEnable()
{
await this.modelViewLogic.InitAsync(this.GetCancellationTokenOnDestroy());
this.modelViewLogic.ShowModel(this.index);
this._angle.Value = 0;
}
private void OnDisable()
{
if (this.modelViewLogic != true)
{
return;
}
this.modelViewLogic.HideAll();
//this.modelViewLogic.ResetView();
}
}