游戏中广播消息公告(测试)

只是测试用,有很多bug,以后用到这个功能在完善吧

    public GameObject RollingParent;
    public int index = 0;
    public string []aa;
    void Start ()
    {   

    RollingParent = GameObject.Find ("Canvas/Panel");

    aa = new string[3]{"无形之刃,最为致命","我用双手,成就你的梦想","断剑重铸之日,骑士归来之时"};//模拟服务器发送的消息列表

    var button = transform.gameObject.GetComponent<Button>();
    if (button != null)
       {
            button.onClick.RemoveAllListeners();
            button.onClick.AddListener(SetDateTxt);
       }

    }

    public void SetDateTxt()
    {   
        if (index > aa.Length) {
            return;
        }

        RollingParent.transform.FindChild("Rolling").gameObject.SetActive (true);

        Text text = RollingParent.transform.FindChild ("Rolling/Text").GetComponent<Text> () ;
        if (text)

            text.gameObject.GetComponent<Text>().text = aa[index];
            text.gameObject.gameObject.AddComponent<RollingTest> ();
            index ++;
    }

------------------------

上面的代码只是用来测试模拟服务器发送字符串列表 在Rolling的子物体挂载RollingTest.cs脚本

思路很简单:就是获取开始和终点坐标 ,然后如果坐标不想等就让文本移动而已。

代码如下:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class RollingTest : MonoBehaviour {
    
    public float mSpeed=50.0f;
    public     Transform targetTransform;
    public     Transform startTransform;
    public float time;
    
    void Awake ()
    {
        transform.parent.transform.gameObject.SetActive (true);
        targetTransform = transform.parent.Find ("End");
        startTransform =   transform.parent.Find ("Start");
    }
    void Start () 
    {    
        time = Mathf.Abs (startTransform.position.x - targetTransform.position.x) / mSpeed;
        Invoke ("setCurRollingState",time);
    }
    
    void Update () 
    {
        transform.Translate (Vector3.left * Time.deltaTime * mSpeed);
        if (transform.position.x <= targetTransform.position.x) {
            transform.position = startTransform.position;
        }
    }
    
    void setCurRollingState(){
        transform.parent.transform.gameObject.SetActive (false);
        Destroy (transform.GetComponent<RollingTest> ());
    }
    
}
蛮牛已经有更好的办法,是用Dotween处理动画,并动态加载列表。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值