Unity3D ----- 制作信息滚动提示(NGUI)

 

先上效果图:

                 

 

 

 

我是用触发器做的。在信息滚动区的上方放了一个触发器,如上图所示,然后利用触发函数,在信息框触发事件时消除,并实例新的对话框。

挂在聊天框父物体的脚本:

using UnityEngine;
using System.Collections;

public class BGController : MonoBehaviour {

    public GameObject chatPre;     // 聊天预设

    private UIGrid grid;
    private GameObject[] talks = new GameObject[4];

    void Awake()
    {
        grid = transform.GetComponent<UIGrid>();
        CreateCells();
    }
    private void CreateCells()
    {
        grid.maxPerLine = 1;
        grid.cellWidth = Global.CWidth;
        grid.cellHeight = Global.CHeight;

        for (int i = 0; i < talks.Length; i++) 
        {
            GameObject go = Instantiate(chatPre)as GameObject;
            go.transform.SetParent(this.transform);               // 设置父物体
            go.transform.localScale = Vector3.one;
            grid.pivot = UIWidget.Pivot.Center;
            grid.AddChild(go.transform,true);

            CellModel m = CellModel.Create("系统:",i.ToString());
            Cell c = go.GetComponent<Cell>();
            c.Model = m;

            talks[i] = go;
        }
    }

    // 创建新的对话,需要相应的对话接口
    public void AddNewCell()
    {
        GameObject go = Instantiate(chatPre)as GameObject;
        go.transform.SetParent(transform);
        go.transform.localPosition = new Vector3(0,-45,0);
        go.transform.localScale = Vector3.one;
        CellModel m = CellModel.Create(System.DateTime.Now.Day.ToString(),System.DateTime.Now.ToString());
        Cell c = go.GetComponent<Cell>();
        c.Model = m;
    }
}

挂在cell预设体上的脚本:

using UnityEngine;
using System.Collections;

public class Cell : MonoBehaviour {

    private BGController bg;
    private Rigidbody rig;

    private CellModel model;

    public UILabel title;
    public UILabel chat;

    public CellModel Model {
        get {
            return model;
        }
        set {
            model = value;
            UpdateChatView();
        }
    }

    void Awake()
    {
        bg = GameObject.FindGameObjectWithTag("Container").GetComponent<BGController>();
        rig = GetComponent<Rigidbody>();
        rig.useGravity = false;

//        title.text = "";
//        chat.text = "";
    }

    void Update () 
    {
        transform.localPosition += transform.up * Global.moveSpeed * Time.deltaTime;
//        UpdateChatView();
    }

    void OnTriggerEnter(Collider other)
    {
        if(other.transform.name.Equals("Trigger"))
        {
//            Debug.Log(other.transform.name);
            Destroy(this.gameObject);
            bg.AddNewCell();
        }
    }
    // 更新聊天框内容(可加接口)
    private void UpdateChatView()
    {
        title.text = model.title;
        chat.text = model.chat;
    }
}

 有两个全局脚本(CellModel和Global)

CellModel存放了cell的模型

using UnityEngine;
using System.Collections;

public class CellModel {

    public string title;
    public string chat;

    private CellModel(){}

    private CellModel(string title,string chat)
    {
        this.title = title;
        this.chat = chat;
    }

    public static CellModel Create(string title,string chat)
    {
        return new CellModel(title,chat);
    }
}

Global存放全局静态变量

 

using UnityEngine;
using System.Collections;

public class Global {

    public static float moveSpeed = 20f;

    public static float CWidth = 270;
    public static float CHeight = 30;

}

 

posted @ 2015-12-15 16:36  王赛  阅读(1030)  评论(0编辑  收藏  举报