这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

88 行
2.2 KiB

using UnityEngine;
using UnityEngine.Events;
using System.Collections;
public class UICreditsRoller : MonoBehaviour
{
[SerializeField, Tooltip("Set speed of a rolling effect")] private float _speedPreset = 100f; //normal rolling speed
[SerializeField, Tooltip("This is actuall speed of rolling")] private float _speed = 100f; //actual speed of rolling
[SerializeField] private bool _rollAgain = false;
[Header("References")]
[SerializeField] private InputReader _inputReader = default;
[SerializeField] private RectTransform _textCredits = default;
[SerializeField] private RectTransform _mask = default;
public event UnityAction OnRollingEnded;
private float _expectedFinishingPoint;
public void StartRolling()
{
_speed = _speedPreset;
StartCoroutine(InitialOffset()); //This offset is needed to get true informations about rectangle and his mask
}
private void OnEnable()
{
_inputReader.MoveEvent += OnMove;
}
private void OnDisable()
{
_inputReader.MoveEvent -= OnMove;
}
void Update()
{
//This make rolling effect
if (_textCredits.anchoredPosition.y < _expectedFinishingPoint)
{
_textCredits.anchoredPosition = new Vector2(_textCredits.anchoredPosition.x, _textCredits.anchoredPosition.y + _speed * Time.deltaTime);
}
else if (_expectedFinishingPoint != 0) //this happend when rolling reach to end
{
RollingEnd();
}
}
private IEnumerator InitialOffset()
{
yield return new WaitForSecondsRealtime(0.02f);
_inputReader.EnableGameplayInput();
_expectedFinishingPoint = (_textCredits.rect.height + _mask.rect.height) / 2;
_textCredits.anchoredPosition = new Vector2(_textCredits.anchoredPosition.x, -((_textCredits.rect.height + _mask.rect.height) / 2));
}
private void OnMove(Vector2 direction)
{
if (direction.y == 0f) //no horizontal movment
{
_speed = _speedPreset;
}
else if (direction.y > 0f) //upward movment
{
_speed = _speed * 2;
}
else //downward movment
{
_speed = -_speedPreset;
}
}
private void RollingEnd()
{
if (_rollAgain)
{
//reset postion of an element
_textCredits.anchoredPosition = new Vector2(_textCredits.anchoredPosition.x, -((_textCredits.rect.height + _mask.rect.height) / 2));
}
else
{
OnRollingEnded.Invoke();
}
}
}