Files
Main/Assets/GameAssets/Resources/GameUI/Form/UIComponents/Scripts/UIBlinkCompoent.cs
2025-01-25 04:38:09 +08:00

199 lines
5.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using System.Collections;
using Thousandto.Code.Center;
using Thousandto.Plugins.Common;
using Thousandto.Code.Logic;
using System.Collections.Generic;
using Thousandto.Core.Asset;
using System;
using Thousandto.Core.Base;
namespace Thousandto.GameUI.Form
{
//UI闪光控件一条白光在某个UI上的移动效果
public class UIBlinkCompoent: MonoBehaviour
{
#region//私有变量
//根节点
private UIPanel _rootPanel = null;
//移动的对象
private UIWidget _widget = null;
private Transform _moveTrans = null;
private GameObject _moveGo = null;
//移动开始位置
private Vector2 _startPos = Vector2.zero;
//移动结束位置
private Vector2 _endPos = Vector2.zero;
//移动方向
private UIBlinkMoveDir _moveDir = UIBlinkMoveDir.LeftToRight;
//时间计数器
private float _moveTimer = 0f;
//单次移动时间
private float _maxMoveTime = 1f;
//每次移动间隔时间
private float _intervalTime = 0f;
//是否正在移动
private bool _isMoveing = true;
#endregion
#region//属性
public UIBlinkMoveDir MoveDir
{
get
{
return _moveDir;
}
set
{
if(_moveDir != value)
{
_moveDir = value;
_isMoveing = false;
Play();
}
}
}
public float MoveTime
{
get
{
return _maxMoveTime;
}
set
{
_maxMoveTime = value;
}
}
public float IntervalTime
{
get
{
return _intervalTime;
}
set
{
_intervalTime = value;
}
}
//根节点
public UIPanel RootPanel
{
get
{
return _rootPanel;
}
}
#endregion
#region//共有函数
public void Play()
{
FindRes();
_moveTimer = 0f;
var region = _rootPanel.finalClipRegion;
var left = region.x - (region.z / 2) - (_widget.width / 2);
var right = region.x + (region.z / 2) + (_widget.width / 2);
var top = region.y + (region.w / 2) + (_widget.height / 2);
var bottom = region.y - (region.w / 2) - (_widget.height / 2);
switch(_moveDir)
{
case UIBlinkMoveDir.LeftToRight:
{
_startPos = new Vector2(left, region.y);
_endPos = new Vector2(right, region.y);
}
break;
case UIBlinkMoveDir.RightRoLeft:
{
_startPos = new Vector2(right, region.y);
_endPos = new Vector2(left, region.y);
}
break;
case UIBlinkMoveDir.TopToBottom:
{
_startPos = new Vector2(region.x, top);
_endPos = new Vector2(region.x, bottom);
}
break;
case UIBlinkMoveDir.BottomToTop:
{
_startPos = new Vector2(region.x, bottom);
_endPos = new Vector2(region.x, top);
}
break;
}
_isMoveing = true;
_moveGo.SetActive(true);
}
public void Stop()
{
FindRes();
_isMoveing = false;
_moveGo.SetActive(false);
}
public void SetSize(int width, int height)
{
if(_rootPanel != null)
{
var region = new Vector4(_rootPanel.baseClipRegion.x, _rootPanel.baseClipRegion.y, width, height);
_rootPanel.baseClipRegion = region;
Play();
}
}
#endregion
#region//私有函数
private void Awake()
{
FindRes();
if(_isMoveing)
{
_isMoveing = false;
Play();
}
}
private void FindRes()
{
if(_rootPanel == null)
{
_rootPanel = GetComponent<UIPanel>();
}
if(_widget == null)
{
_widget = transform.Find("MoveRoot").GetComponent<UIWidget>();
_moveTrans = _widget.transform;
_moveGo = _widget.gameObject;
}
}
private void Update()
{
if(_isMoveing)
{
_moveTimer += Time.deltaTime;
var curTime = _moveTimer % (_maxMoveTime + _intervalTime);
if(curTime <= _maxMoveTime)
{
_moveGo.SetActive(true);
var lerpValue = curTime / _maxMoveTime;
_moveTrans.localPosition = Vector2.Lerp(_startPos, _endPos, lerpValue);
}
else
{
_moveGo.SetActive(false);
}
}
}
#endregion
}
}