scene中的设置
代码移动
/// <summary>
/// 公告跑马灯
/// </summary>
private Text NoticeText;
public Vector3 Speed=new Vector3(2,0,0);
public float _MoveEndPoint;
public Vector2 _MoveStartPoint;
public float _ParentWidth;
public float _SelfWidth;
/// <summary>
/// 注册 公告,便于刷新
/// </summary>
/// <param name="text"></param>
void RegisterBroadcast(string text)
{
NoticeText.text = text;
_ParentWidth = NoticeText.transform.parent.GetComponent<RectTransform>().rect.width;
// 宽度获取
// _SelfWidth = NoticeText.transform.GetComponent<RectTransform>().rect.width;
_SelfWidth = NoticeText.preferredWidth;
NoticeText.transform.GetComponent<RectTransform>().pivot = new Vector2(0, 0.5f);
_MoveEndPoint = -_ParentWidth / 2 -_SelfWidth;
_MoveStartPoint=new Vector2(_ParentWidth / 2, 0);
}
private void FixedUpdate()
{
// 公告移动
if (NoticeText.gameObject.activeInHierarchy)
{
NoticeText.transform.localPosition -= Speed;
if (NoticeText.transform.localPosition.x <= _MoveEndPoint)
{
NoticeText.transform.localPosition = _MoveStartPoint;
}
}
}