博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity3D ----- 制作信息滚动提示(NGUI)
阅读量:4327 次
发布时间:2019-06-06

本文共 3285 字,大约阅读时间需要 10 分钟。

 

先上效果图:

                 

 

 

 

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

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

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
(); 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
(); 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
(); 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
(); rig = GetComponent
(); 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;}

 

转载于:https://www.cnblogs.com/wangsaiws/p/5048708.html

你可能感兴趣的文章
Java定时任务调度工具Timer Quartz
查看>>
WPF,Silverlight与XAML读书笔记第三十五 - 可视化效果之图像
查看>>
Nginx中location配置[转]
查看>>
编程重点
查看>>
00-A-springmvc分布式项目项目结构
查看>>
vs调试时报503错误
查看>>
SVN使用&CVS使用
查看>>
redis
查看>>
Oracle存储过程中如何使用游标
查看>>
揭开NodeJS的神秘面纱!
查看>>
Number Triangles
查看>>
Ext分页实现(前台与后台)
查看>>
转 迭代器模式
查看>>
CYQ.Data V5 MAction新增加SetExpression方法说明
查看>>
数据安全&MD5加密
查看>>
bzoj 2594: 水管局长数据加强版 Link-Cut-Tree
查看>>
世界是数字的观后感
查看>>
由DBCursor的“can't switch cursor access methods”异常引发的思考
查看>>
LUOGU P1438 无聊的数列 (差分+线段树)
查看>>
引用和指针的区别
查看>>