NewN_UAVPlane/Assets/Zion/Scripts/AVPro/AutoHide.cs

148 lines
3.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
public class AutoHide : MonoBehaviour
{
public RectTransform m_rectTransform;
public Toggle m_Toggle;
/// <summary>
/// 是否隐藏
/// </summary>
private bool hide;
/// <summary>
/// 是否显示
/// </summary>
private bool show;
/// <summary>
/// 是否自动隐藏
/// 随Toggle改变
/// </summary>
private bool m_AutoHide ;
/// <summary>
/// 限制显示/隐藏执行
/// </summary>
private bool m_return;
/// <summary>
/// 限制显示/隐藏执行 时间
/// </summary>
private float m_returntime;
/// <summary>
/// 面板位置存储
/// </summary>
private Vector3 trapos;
/// <summary>
/// 显示/隐藏 速度值
/// </summary>
public float t = 0.5f;
/// <summary>
/// 自动隐藏计时
/// </summary>
public float m_autohide;
public UI_Button _VideoDisplay;
public List<UI_Button> m_UI_Buttons = new List<UI_Button>();
// Start is called before the first frame update
void Start()
{
m_rectTransform = GetComponent<RectTransform>();
m_UI_Buttons = GetComponentsInChildren<UI_Button>().ToList();
m_UI_Buttons.ForEach(x =>
{
x.PointerClickEvent.AddListener(UIClickEvent);
});
_VideoDisplay.PointerClickEvent.AddListener(VideoDisplayClickEvent);
m_Toggle.onValueChanged.AddListener((active) =>
{
m_AutoHide = active;
});
m_AutoHide = m_Toggle.isOn;
}
// Update is called once per frame
void Update()
{
ReadyMove();
}
public void ReadyMove()
{
if (m_AutoHide)
{
m_autohide += Time.deltaTime;
if (m_autohide >= 10)
{
hide = true;
m_return = true;
}
}
else
{
hide = false;
show = true;
m_return = true;
}
if (m_return)
{
m_returntime += Time.deltaTime;
if (m_returntime > 2f)
{
m_returntime = 0;
m_return = false;
}
if (hide)
{
show = false;
trapos = m_rectTransform.transform.position;
m_rectTransform.transform.position = Vector3.Lerp(trapos, new Vector3(trapos.x, -80, 0), t);
}
if (show)
{
hide = false;
trapos = m_rectTransform.transform.position;
m_rectTransform.transform.position = Vector3.Lerp(trapos, new Vector3(trapos.x, 0, 0), t);
}
}
}
/// <summary>
/// 点击视频
/// </summary>
public void VideoDisplayClickEvent()
{
hide = false;
show = true;
m_return = true;
m_autohide = 0;
}
/// <summary>
/// 点击UI
/// </summary>
public void UIClickEvent()
{
m_autohide = 0;
}
private void OnEnable()
{
m_autohide = 0;
}
private void OnDestroy()
{
_VideoDisplay.PointerClickEvent.RemoveListener(VideoDisplayClickEvent);
m_UI_Buttons.ForEach(x =>
{
x.PointerClickEvent.RemoveListener(UIClickEvent);
});
}
}