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;
///
/// 是否隐藏
///
private bool hide;
///
/// 是否显示
///
private bool show;
///
/// 是否自动隐藏
/// 随Toggle改变
///
private bool m_AutoHide ;
///
/// 限制显示/隐藏执行
///
private bool m_return;
///
/// 限制显示/隐藏执行 时间
///
private float m_returntime;
///
/// 面板位置存储
///
private Vector3 trapos;
///
/// 显示/隐藏 速度值
///
public float t = 0.5f;
///
/// 自动隐藏计时
///
public float m_autohide;
public UI_Button _VideoDisplay;
public List m_UI_Buttons = new List();
// Start is called before the first frame update
void Start()
{
m_rectTransform = GetComponent();
m_UI_Buttons = GetComponentsInChildren().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);
}
}
}
///
/// 点击视频
///
public void VideoDisplayClickEvent()
{
hide = false;
show = true;
m_return = true;
m_autohide = 0;
}
///
/// 点击UI
///
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);
});
}
}