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

来源:互联网 发布:五行量化指标源码 编辑:程序博客网 时间:2024/06/06 07:19

只是测试用,有很多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
原创粉丝点击