大概UI就是这个样子。 uiSystemTips 是一个panel 你调节到小如图 让 item 也就是 uilable 在uipanel上滚动 滚动有tweenPosition动画实现。
(1) (2) 的脚本都对, 只是实现下效果不一样而已。
(1) 不管是多长的lab,都从最右侧开始往左滚动,直至lab尾部 消失在panel上 销毁
(2) 如果lab短 居中 静止2秒 销毁 否则 执行(1)
0. 走马灯UI控制脚本(1)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using CommonEnum;
using PBClientClass;
using System;
public class UISystemTips : UIPanelBase {
int _TexBgWidth = 488;
float _Speed = 10;
Vector3 _Pos = new Vector3 ();
string _CurTips = "";
UILabel _TipsLab;
UITexture _TipsLabBg;
TweenPosition tween;
UISystemTipsView _View;
void Awake()
{
_View = gameObject.AddComponent();
_TipsLab = _View.Anchor_item_UILabel;
_TipsLabBg = _View.Anchor_itemTexBg_UITexture;
tween = _View.Anchor_item_TweenPosition;
_TexBgWidth = _TipsLabBg.width;
_Pos = _TipsLab.transform.localPosition;
ClientEventManager.GetInstance().RegisterEventFunction(NTFrame.GameEvent.GameEventID.GEOnNewChatInfo, OnNewChatCome);
}
public override void OnRemove()
{
ClientEventManager.GetInstance().UnRegisterEventFunction(NTFrame.GameEvent.GameEventID.GEOnNewChatInfo, OnNewChatCome);
}
public override void OnShow(params object[] paramsList)
{
_Speed = 0.01f;
_CurTips = "";
OnPlayAnim();
}
public override void OnClose()
{
}
void OnNewChatCome(ClientEvent eve)
{
if (gameObject == null || gameObject.activeSelf == false)
return;
if (eve == null || eve.ParamCount < 1)
{
return;
}
ChatManager.ChatInfoItem iteminfo = eve.GetParameter(0);
if (iteminfo == null || iteminfo._PB == null)
return;
bool isSysTem = iteminfo.isSystemTips();
if (isSysTem == false)
return;
SetData(iteminfo._PB.content);
}
void OnClear()
{
if (tween != null)
tween.enabled = false;
UITools.ClearLabel(_TipsLab);
UITools.setActive(_TipsLab,false);
UITools.setActive(_TipsLabBg,false);
}
public void OnClearUI()
{
OnClear();
}
public static void SetData(string str)
{
}
void OnPlayAnim()
{
List tips = ChatManager.GetInstance().GetTipsList();
if (tips == null)
tips = new List ();
if (tips.Count == 0)
{
CloseSelf();
}
else
{
string str = tips[0];
PlayAnim(str);
_CurTips = str;
}
}
void PlayAnim(string str)
{
if (tween == null)
return;
OnClear();
UITools.SetLabText(_TipsLab,str);
UITools.setActive(_TipsLab,true);
UITools.setActive(_TipsLabBg,true);
int labWidth = UITools.GetWidthByLab(_TipsLab);
int add = labWidth + _TexBgWidth;
float v = add*_Speed;
_TipsLab.pivot = UIWidget.Pivot.Left;
UITools.SetTransformPosition(_TipsLab,_Pos);
tween.from = _Pos;
tween.to = new Vector3 (_Pos.x-add,_Pos.y,_Pos.z);
tween.duration = v;
tween.delay = 0;
tween.ResetToBeginning();
tween.PlayForward();
EventDelegate eventDelegate = new EventDelegate(this, "OnFinished");
EventDelegate.Add(tween.onFinished, OnFinished);
}
void OnFinished()
{
ChatManager.GetInstance().OnRemovedTips(_CurTips);
OnClearUI();
OnPlayAnim();
}
}
1. 走马灯UI控制脚本(2)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using CommonEnum;
using PBClientClass;
using System;
public class UISystemTips : UIPanelBase {
int _MaxWidth = 616;
int _MaxHeight = 32;
Vector3 _Pos = new Vector3 ();
float _leftPosX = 0.0f;
string _CurTips = "";
UILabel _TipsLab;
UITexture _TipsLabBg;
TweenPosition tween;
UISystemTipsView _View;
void Awake()
{
_View = gameObject.AddComponent();
_TipsLab = _View.Anchor_item_UILabel;
_TipsLabBg = _View.Anchor_itemTexBg_UITexture;
tween = _View.Anchor_item_TweenPosition;
_leftPosX = -258f;
_Pos = _TipsLab.transform.localPosition;
ClientEventManager.GetInstance().RegisterEventFunction(NTFrame.GameEvent.GameEventID.GEOnNewChatInfo, OnNewChatCome);
}
public override void OnRemove()
{
ClientEventManager.GetInstance().UnRegisterEventFunction(NTFrame.GameEvent.GameEventID.GEOnNewChatInfo, OnNewChatCome);
}
public override void OnShow(params object[] paramsList)
{
_CurTips = "";
OnPlayAnim();
}
public override void OnClose()
{
}
void OnNewChatCome(ClientEvent eve)
{
if (gameObject == null || gameObject.activeSelf == false)
return;
if (eve == null || eve.ParamCount < 1)
{
return;
}
ChatManager.ChatInfoItem iteminfo = eve.GetParameter(0);
if (iteminfo == null || iteminfo._PB == null)
return;
bool isSysTem = iteminfo.isSystemTips();
if (isSysTem == false)
return;
SetData(iteminfo._PB.content);
}
void OnClear()
{
if (tween != null)
tween.enabled = false;
UITools.ClearLabel(_TipsLab);
UITools.setActive(_TipsLab,false);
UITools.setActive(_TipsLabBg,false);
}
public void OnClearUI()
{
OnClear();
}
public static void SetData(string str)
{
}
void OnPlayAnim()
{
List tips = ChatManager.GetInstance().GetTipsList();
if (tips == null)
tips = new List ();
if (tips.Count == 0)
{
CloseSelf();
}
else
{
string str = tips[0];
PlayAnim(str);
_CurTips = str;
}
}
void PlayAnim(string str)
{
if (tween == null)
return;
OnClear();
UITools.SetLabText(_TipsLab,str);
UITools.setActive(_TipsLab,true);
UITools.setActive(_TipsLabBg,true);
bool isPlayAnim = false;
int labWidth = UITools.GetWidthByLab(_TipsLab);
isPlayAnim = labWidth > _MaxWidth;
if (isPlayAnim == false)
{
_TipsLab.pivot = UIWidget.Pivot.Center;
UITools.SetTransformPosition(_TipsLab,new Vector3 (_Pos.x,_Pos.y,_Pos.z));
tween.from = _Pos;
tween.to = _Pos;
tween.duration = 0;
tween.delay = 1;
}
else
{
_TipsLab.pivot = UIWidget.Pivot.Left;
UITools.SetTransformPosition(_TipsLab,new Vector3(_leftPosX,_Pos.y,_Pos.z));
int left = labWidth - _MaxWidth;
tween.from = new Vector3 (_leftPosX,_Pos.y,_Pos.z);
tween.to = new Vector3 (_leftPosX-left,_Pos.y,_Pos.z);
tween.duration = 10;
tween.delay = 1;
}
tween.ResetToBeginning();
tween.PlayForward();
EventDelegate eventDelegate = new EventDelegate(this, "OnFinished");
EventDelegate.Add(tween.onFinished, OnFinished);
}
void OnFinished()
{
ChatManager.GetInstance().OnRemovedTips(_CurTips);
OnClearUI();
OnPlayAnim();
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using PBClientClass;
using System.IO;
using CommonEnum;
using System;
public class ChatManager : NTFrame.Singleton
{
List _TipsList = new List ();// tips记录
public class ChatInfoItem
{
public PBChatWithChannelRes _PB;
public int _TimeStamp;
public bool isSystemTips()
{
if (_PB == null)
return false;
bool isd = _PB.msgType == 1;
return isd;
}
}
//新的聊天信息,不包括私聊
public void OnNewChat(PBChatWithChannelRes pb)
{
if (pb == null)
{
return;
}
ChatInfoItem item = new ChatInfoItem();
ClientEvent eve = new ClientEvent();
eve.AddParameter(item);
ClientEventManager.GetInstance().ActiveEvent(NTFrame.GameEvent.GameEventID.GEOnNewChatInfo, eve);
OnTryOpenTips(item);
}
// 尝试打开 走马灯 UI
void OnTryOpenTips(ChatInfoItem item)
{
if (item == null)
return;
if (item._PB == null)
return;
bool istips = item.isSystemTips();
if (istips == false)
return;
string str = item._PB.content;
OnAddTips(str);
UIUtil.ShowUISystemTips(str);
}
// 尝试打开 走马灯 UI
public void OnTryOpenTips()
{
if (_TipsList == null)
_TipsList = new List ();
if (_TipsList.Count == 0)
return;
UIUtil.ShowUISystemTips(_TipsList[0]);
}
// 系统消息 走马灯 string列表
public List GetTipsList()
{
return _TipsList;
}
// 清空list
public void OnClearTipsList()
{
if (_TipsList == null)
_TipsList = new List ();
_TipsList.Clear();
}
// 添加一条新的系统消息
public void OnAddTips(string str)
{
if (_TipsList == null)
_TipsList = new List ();
_TipsList.Add(str);
}
// 删除一条系统消息
public void OnRemovedTips(string str)
{
if (_TipsList == null)
_TipsList = new List ();
for (int i = 0;i < _TipsList.Count;i ++)
{
if (_TipsList[i] == str)
{
_TipsList.Remove(str);
return ;
}
}
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using PBClientClass;
using CommonEnum;
public static class UIUtil
{
public static void ShowUISystemTips(string str)
{
if (GameUIManager.Instance.IsActive(UIName.UISystemTips) == false)
{
GameUIManager.GetInstance().ShowPanel(UIName.UISystemTips, str);
}
else
{
UISystemTips.SetData(str);
}
}
}