Files
2025-01-25 04:38:09 +08:00

378 lines
10 KiB
C#
Raw Permalink 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 System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
//选择效果脚本不能编辑到UI里边只能动态添加
public class UISpriteSelectEffect: MonoBehaviour
{
private class SpriteMover
{
private UISprite _handleSpr = null;
private Vector3 _startPos = Vector3.zero;
private Vector3 _endPos = Vector3.zero;
private Vector3 _endPosOrgin = Vector3.zero;
private float _timer = 0f;
private float _maxTime = 0f;
private float _changeLerpValue = 0f;
private bool _moving = false;
private bool _isEnd = false;
public bool Moving
{
get
{
return _moving;
}
}
public bool IsEnd
{
get
{
return _isEnd;
}
}
public Color Color
{
set
{
if (_handleSpr != null)
{
_handleSpr.color = value;
}
}
}
public SpriteMover(UISprite spr, Vector3 st, Vector3 ed, float speed)
{
_handleSpr = spr;
_startPos = st;
_endPosOrgin = ed;
var nor = (ed - st).normalized;
var length = Vector3.Distance(st, ed);
var movelength = length + spr.width;
_endPos = _startPos + nor * movelength;
_maxTime = movelength / speed;
_changeLerpValue = length / movelength;
_moving = false;
_handleSpr.fillAmount = 0f;
}
public void StartMove()
{
_handleSpr.transform.localPosition = _startPos;
_handleSpr.fillAmount = 0f;
_handleSpr.invert = false;
_timer = 0f;
_moving = true;
}
public void Update()
{
if (!_moving)
return;
_timer += Time.deltaTime;
float lerp = _timer / _maxTime;
var noncePos = Vector3.Lerp(_startPos, _endPos, lerp);
_handleSpr.transform.localPosition = noncePos;
if (lerp < _changeLerpValue)
{
_handleSpr.invert = false;
_isEnd = false;
_handleSpr.fillAmount = Vector3.Distance(noncePos, _startPos) / _handleSpr.width;
}
else
{
_handleSpr.invert = true;
_isEnd = true;
_handleSpr.fillAmount = (_handleSpr.width - Vector3.Distance(noncePos, _endPosOrgin)) / _handleSpr.width;
}
if (lerp > 1f)
{
_moving = false;
}
}
}
private UISprite _left = null;
private UISprite _right = null;
private UISprite _top = null;
private UISprite _down = null;
[SerializeField]
private float _moveSpeed = 300f;
[SerializeField]
private bool _refresh = false;
[SerializeField]
private Color _startColor = Color.white;
[SerializeField]
private Color _endColor = Color.white;
[SerializeField]
[Range(0.5f, float.MaxValue)]
private float _colorMaxTime = 1f;
private List<SpriteMover> _moverList = new List<SpriteMover>();
private int _moveIndex = 0;
private int _moveIndex2 = 0;
private float _colorTimer = 0f;
private bool _colorAdd = false;
public float MoveSpeed
{
get
{
return _moveSpeed;
}
set
{
_moveSpeed = value;
}
}
public Color StartColor
{
get
{
return _startColor;
}
set
{
_startColor = value;
}
}
public Color EndColor
{
get
{
return _endColor;
}
set
{
_endColor = value;
}
}
public int Width
{
set
{
UIWidget wid = gameObject.GetComponent<UIWidget>();
if (wid != null && wid.width != value)
{
wid.width = value;
_refresh = true;
}
}
}
public int Height
{
set
{
UIWidget wid = gameObject.GetComponent<UIWidget>();
if (wid != null && wid.height != value)
{
wid.height = value;
_refresh = true;
}
}
}
private void Start()
{
_refresh = true;
Refresh();
}
private void Refresh()
{
if (!_refresh)
return;
_refresh = false;
_moverList.Clear();
UIWidget wid = gameObject.GetComponent<UIWidget>();
int minWidth = Math.Min(wid.width, wid.height);
var tleft = transform.Find("left");
_left = tleft != null ? tleft.GetComponent<UISprite>() : null;
if (_left != null)
{
_left.transform.localScale = Vector3.one;
_left.transform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, -90f));
_left.type = UIBasicSprite.Type.Filled;
_left.flip = UIBasicSprite.Flip.Nothing;
_left.fillDirection = UIBasicSprite.FillDirection.Horizontal;
_left.pivot = UIWidget.Pivot.Left;
if (_left.width > minWidth)
{
_left.width = minWidth;
}
SpriteMover mover = new SpriteMover(_left,
new Vector3(-wid.width / 2, -wid.height / 2),
new Vector3(-wid.width / 2, wid.height / 2), _moveSpeed);
_moverList.Add(mover);
}
var ttop = transform.Find("top");
_top = ttop != null ? ttop.GetComponent<UISprite>() : null;
if (_top != null)
{
_top.transform.localScale = Vector3.one;
_top.transform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, 180f));
_top.type = UIBasicSprite.Type.Filled;
_top.flip = UIBasicSprite.Flip.Nothing;
_top.fillDirection = UIBasicSprite.FillDirection.Horizontal;
_top.pivot = UIWidget.Pivot.Left;
if (_top.width > minWidth)
{
_top.width = minWidth;
}
SpriteMover mover = new SpriteMover(_top,
new Vector3(-wid.width / 2, wid.height / 2),
new Vector3(wid.width / 2, wid.height / 2), _moveSpeed);
_moverList.Add(mover);
}
var tright = transform.Find("right");
_right = tright != null ? tright.GetComponent<UISprite>() : null;
if (_right != null)
{
_right.transform.localScale = Vector3.one;
_right.transform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, 90f));
_right.type = UIBasicSprite.Type.Filled;
_right.flip = UIBasicSprite.Flip.Nothing;
_right.fillDirection = UIBasicSprite.FillDirection.Horizontal;
_right.pivot = UIWidget.Pivot.Left;
if (_right.width > minWidth)
{
_right.width = minWidth;
}
SpriteMover mover = new SpriteMover(_right,
new Vector3(wid.width / 2, wid.height / 2),
new Vector3(wid.width / 2, -wid.height / 2), _moveSpeed);
_moverList.Add(mover);
}
var tdown = transform.Find("down");
_down = tdown != null ? tdown.GetComponent<UISprite>() : null;
if (_down != null)
{
_down.transform.localScale = Vector3.one;
_down.transform.localRotation = Quaternion.Euler(new Vector3(0f, 0f, 0f));
_down.type = UIBasicSprite.Type.Filled;
_down.flip = UIBasicSprite.Flip.Nothing;
_down.fillDirection = UIBasicSprite.FillDirection.Horizontal;
_down.pivot = UIWidget.Pivot.Left;
if (_down.width > minWidth)
{
_down.width = minWidth;
}
SpriteMover mover = new SpriteMover(_down,
new Vector3(wid.width / 2, -wid.height / 2),
new Vector3(-wid.width / 2, -wid.height / 2), _moveSpeed);
_moverList.Add(mover);
}
if (_moverList.Count > 0)
{
_moverList[0].StartMove();
}
_moveIndex = 0;
if (_moverList.Count >= 4)
{
_moveIndex2 = 2;
_moverList[2].StartMove();
}
else
{
_moveIndex2 = -1;
}
}
private void Update()
{
Refresh();
_colorTimer += Time.deltaTime;
Color color;
if (_colorAdd)
{
color = Color.Lerp(_startColor, _endColor, _colorTimer / _colorMaxTime);
}
else
{
color = Color.Lerp(_endColor, _startColor, _colorTimer / _colorMaxTime);
}
if (_colorTimer > _colorMaxTime)
{
_colorTimer = 0f;
_colorAdd = !_colorAdd;
}
for (int i = 0; i < _moverList.Count; ++i)
{
_moverList[i].Color = color;
_moverList[i].Update();
}
if(_moverList.Count > _moveIndex)
{
if (_moverList[_moveIndex].IsEnd)
{
++_moveIndex;
if (_moverList.Count > _moveIndex)
{
_moverList[_moveIndex].StartMove();
}
else
{
_moveIndex = 0;
_moverList[_moveIndex].StartMove();
}
}
}
if (_moveIndex2 >= 0 && _moverList.Count > _moveIndex2)
{
if (_moverList[_moveIndex2].IsEnd)
{
++_moveIndex2;
if (_moverList.Count > _moveIndex2)
{
_moverList[_moveIndex2].StartMove();
}
else
{
_moveIndex2 = 0;
_moverList[_moveIndex2].StartMove();
}
}
}
}
}