Files
Main/Assets/GameAssets/Resources/GameUI/Common/ScrolMoveEffect/ScrolMoveEffect.cs

428 lines
16 KiB
C#
Raw Normal View History

2025-01-25 04:38:09 +08:00
//**********************************************//
//作者:#AUTHOR#
//日期:#DATE#
//简述:#DESCRIPTION#
//*********************************************//
using UnityEngine;
using System.Collections;
using Thousandto.Core.Base;
using System;
namespace Thousandto.Plugins.Common
{
/// <summary>
/// 类ScrolMoveEffect说明
/// </summary>
public class ScrollMoveEffect
{
#region //私有变量
private int _moveSpeed = 400; //滚动默认速度
private float _endScrolPos = 0.0f; //滑动列表滑动到目标点位置上下滚动对应Y左右滚动对应X
private float _originPos = 0.0f; //滑动列表初始位置上下滚动对应Y左右滚动对应X
private float _scrolOffset = 30.0f; //滑动列表偏移量
private float _aginOffset = 10.0f; //二次反弹
private float _cellLengh = 0.0f; //滚动一格的距离
private float _cellDis = 0.0f; //滚动格子的间距(一定要是SetScrol函数第二个参数的宽或者高)
private float _initDis = 0.0f; //滑动列表第一个元素初始的(高/宽)
private bool _isBack = false;
private bool _isDown = false;
private bool _isAgin = false;
private bool _isRight = false;
private MoveType _type = MoveType.Ver; //默认垂直方向移动
private Transform _scrolTrans = null;
private Transform _disTrans = null; //滑动列表滚动是防止玩家操作
private UIScrollView _scrol = null; //需要移动的ScrolView
#endregion
#region//公共属性
public float EndScrolPos
{
set
{
_endScrolPos = value;
}
}
#endregion
#region//公共接口
//设置滚动参数
/// <summary>
///
/// </summary>
/// <param scrollView="trans"></param>
/// <param 遮罩自行添加不添加传null="disTrans"></param>
/// <param 需要移动的单位图片="spr"></param>
/// <param 单位图片之间的距离="dis"></param>
/// <param 水平/垂直="type"></param>
/// <param 第一个图片元素偏移="initDis"></param>
public void SetScrol( Transform trans , Transform disTrans, UISprite spr, float dis, int type, float initDis = 0.0f )
{
if( trans != null )
{
_scrolTrans = trans;
_scrol = UnityUtils.RequireComponent<UIScrollView>( _scrolTrans.gameObject );
_cellDis = dis;
_initDis = initDis;
}
if( spr != null )
{
if( type == ( int )MoveType.Ver )
{
_cellLengh = spr.height;
_originPos = _scrolTrans.localPosition.y;
}
else
{
_cellLengh = spr.width;
_originPos = _scrolTrans.localPosition.x;
_type = MoveType.Hor;
}
}
_disTrans = disTrans;
}
//
/// <summary>
/// 开始滚动
/// </summary>
/// <param 移动几个单位="count"></param>
/// <param 是否是第一次移动="isFirst"></param>
public void StarMoveEffect( int count = 0, bool isFirst = false )
{
float pos = 0.0f;
if( _type == MoveType.Hor )
{
pos = _scrolTrans.localPosition.x;
SetHorEndPos( count, isFirst, pos );
}
else
{
pos = _scrolTrans.localPosition.y;
SetVerEndPos( count, isFirst, pos );
}
if( isFirst )
{
_scrol.ResetPosition();
}
if( _disTrans )
{
_disTrans.gameObject.SetActive( true );
}
}
public void Update()
{
//更新滑动列表位置
if( _endScrolPos != 0.0f )
{
if( _type == MoveType.Ver )
{
UpdateVer();
}
else
{
UpdateHor();
}
}
}
#endregion
#region//回调函数
#endregion
#region//私有函数
/// <summary>
///
/// </summary>
/// <param 移动几个单位="count"></param>
/// <param 是否是第一次移动="isFirst"></param>
/// <param 滑动列表滚动绝对距离="pos"></param>
private void SetVerEndPos( int count, bool isFirst, float pos )
{
float dis = 0.0f;
if( isFirst )
{
_endScrolPos = count * ( _cellLengh + _cellDis - _initDis );
//保证第一个动画
if( _endScrolPos == 0.0f )
{
_endScrolPos = 0.001f;
}
}
else
{
if( _originPos > pos )
{
dis = Math.Abs( pos - _originPos );
_endScrolPos = ( _cellLengh + _cellDis - _initDis ) + dis;
}
else if( _originPos == pos )
{
_endScrolPos = _cellLengh - _initDis;
}
else if( _originPos < pos )
{
_isDown = true;
dis = Math.Abs( pos - _originPos );
_endScrolPos = dis - ( _cellLengh + _cellDis - _initDis );
}
}
}
/// <summary>
///
/// </summary>
/// <param 移动几个单位="count"></param>
/// <param 是否是第一次移动="isFirst"></param>
/// <param 滑动列表滚动绝对距离="pos"></param>
private void SetHorEndPos( int count, bool isFirst, float pos )
{
float dis = 0.0f;
if( isFirst )
{
_endScrolPos = count * ( _cellLengh + _cellDis - _initDis );
//保证第一个动画
if( _endScrolPos == 0.0f )
{
_endScrolPos = 0.001f;
}
}
else
{
if( _originPos > pos )
{
dis = Math.Abs( pos - _originPos );
_endScrolPos = dis - ( _cellLengh + _cellDis - _initDis );
_isRight = true;
}
else if( _originPos == pos )
{
_endScrolPos = _cellLengh - _initDis;
}
else if( _originPos < pos )
{
dis = Math.Abs( pos - _originPos );
_endScrolPos = dis + ( _cellLengh + _cellDis - _initDis );
}
}
}
//垂直更新
private void UpdateVer()
{
if( _scrol )
{
if( !_isBack )
{
if( !_isDown )
{
_scrol.MoveRelative( new Vector3( 0, Time.deltaTime * _moveSpeed, 0 ) );
}
else
{
_scrol.MoveRelative( new Vector3( 0, -Time.deltaTime * _moveSpeed, 0 ) );
}
_endScrolPos -= Time.deltaTime * _moveSpeed;
if( _moveSpeed < 600 )
{
_moveSpeed += 20;
}
if( _endScrolPos == 0.0f )
{
_endScrolPos = -0.0001f;
}
if( _endScrolPos < -_scrolOffset )
{
_isBack = true;
_moveSpeed = 200;
}
}
else
{
//反弹
if( _endScrolPos >= 0 && !_isAgin )
{
_scrol.MoveRelative( new Vector3( 0, _endScrolPos, 0 ) );
_endScrolPos = _aginOffset;
_moveSpeed = 30;
_isAgin = true;
}
else if( _endScrolPos < 0 && !_isAgin )
{
if( _moveSpeed > 400 )
{
_moveSpeed -= 15;
}
if( _moveSpeed < 100 )
{
_moveSpeed = 100;
}
if( !_isDown )
{
_scrol.MoveRelative( new Vector3( 0, -Time.deltaTime * _moveSpeed, 0 ) );
}
else
{
_scrol.MoveRelative( new Vector3( 0, Time.deltaTime * _moveSpeed, 0 ) );
}
_endScrolPos += Time.deltaTime * _moveSpeed;
}
else
{
//二次反弹
if( _endScrolPos <= 0 )
{
_scrol.MoveRelative( new Vector3( 0, _endScrolPos, 0 ) );
_isAgin = false;
_isBack = false;
_endScrolPos = 0.0f;
_moveSpeed = 400;
if( _isDown )
{
_isDown = false;
}
if( _scrolTrans )
{
_originPos = _scrolTrans.localPosition.y;
}
if( _disTrans )
{
_disTrans.gameObject.SetActive( false );
}
}
else
{
if( _moveSpeed > 0 )
{
_moveSpeed -= 1;
}
if( _moveSpeed <= 15 )
{
_moveSpeed = 15;
}
if( !_isDown )
{
_scrol.MoveRelative( new Vector3( 0, Time.deltaTime * _moveSpeed, 0 ) );
}
else
{
_scrol.MoveRelative( new Vector3( 0, -Time.deltaTime * _moveSpeed, 0 ) );
}
_endScrolPos -= Time.deltaTime * _moveSpeed;
}
}
}
}
}
//水平更新
private void UpdateHor()
{
if( _scrol )
{
if( !_isBack )
{
if( !_isRight )
{
_scrol.MoveRelative( new Vector3( -Time.deltaTime * _moveSpeed, 0, 0 ) );
}
else
{
_scrol.MoveRelative( new Vector3( Time.deltaTime * _moveSpeed, 0, 0 ) );
}
_endScrolPos -= Time.deltaTime * _moveSpeed;
if( _moveSpeed < 600 )
{
_moveSpeed += 20;
}
if( _endScrolPos == 0.0f )
{
_endScrolPos = -0.0001f;
}
if( _endScrolPos < -_scrolOffset )
{
_isBack = true;
_moveSpeed = 200;
}
}
else
{
//反弹
if( _endScrolPos >= 0 && !_isAgin )
{
_scrol.MoveRelative( new Vector3( _endScrolPos, 0, 0 ) );
_endScrolPos = _aginOffset;
_moveSpeed = 30;
_isAgin = true;
}
else if( _endScrolPos < 0 && !_isAgin )
{
if( _moveSpeed > 400 )
{
_moveSpeed -= 15;
}
if( _moveSpeed < 100 )
{
_moveSpeed = 100;
}
if( !_isRight )
{
_scrol.MoveRelative( new Vector3( Time.deltaTime * _moveSpeed, 0, 0 ) );
}
else
{
_scrol.MoveRelative( new Vector3( -Time.deltaTime * _moveSpeed, 0, 0 ) );
}
_endScrolPos += Time.deltaTime * _moveSpeed;
}
else
{
//二次反弹
if( _endScrolPos <= 0 )
{
_scrol.MoveRelative( new Vector3( _endScrolPos, 0, 0 ) );
_isAgin = false;
_isBack = false;
_endScrolPos = 0.0f;
_moveSpeed = 400;
if( _isRight )
{
_isRight = false;
}
if( _scrolTrans )
{
_originPos = _scrolTrans.localPosition.x;
}
if( _disTrans )
{
_disTrans.gameObject.SetActive( false );
}
}
else
{
if( _moveSpeed > 0 )
{
_moveSpeed -= 1;
}
if( _moveSpeed <= 15 )
{
_moveSpeed = 15;
}
if( !_isRight )
{
_scrol.MoveRelative( new Vector3( -Time.deltaTime * _moveSpeed, 0, 0 ) );
}
else
{
_scrol.MoveRelative( new Vector3( Time.deltaTime * _moveSpeed, 0, 0 ) );
}
_endScrolPos -= Time.deltaTime * _moveSpeed;
}
}
}
}
}
#endregion
private enum MoveType
{
Hor = 0, //水平方向移动
Ver = 1, //垂直方向移动
}
}
}