Unity3D ----- 制作信息滚动提示(NGUI)
2015-12-15 16:36
423 查看
先上效果图:
我是用触发器做的。在信息滚动区的上方放了一个触发器,如上图所示,然后利用触发函数,在信息框触发事件时消除,并实例新的对话框。
挂在聊天框父物体的脚本:
挂在cell预设体上的脚本:
有两个全局脚本(CellModel和Global)
CellModel存放了cell的模型
Global存放全局静态变量
我是用触发器做的。在信息滚动区的上方放了一个触发器,如上图所示,然后利用触发函数,在信息框触发事件时消除,并实例新的对话框。
挂在聊天框父物体的脚本:
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; }
相关文章推荐
- StrangeIoC —— Unity MVC 专属框架
- Unity3D方法来隐藏和显示对象
- Unity 动态生成 xml文档
- Unity Shader 顶点程序 实现漫反射与法向量的非等比转换
- Unity 获取指定资源目录下的所有文件
- Unity3D AI角色移动和自动跟随
- Unity Sprite切割导出
- Unity3D 巡逻敌兵探测玩家逻辑实现
- Unity3D 摄像机平滑跟随角色
- Unity3D角色换装实现原理及步骤
- unity3d爬坑手记3
- 【Unity Shaders】Lighting Models —— 灯型号Lit Sphere
- 在Unity3D中使用ScriptableObject进行序列化
- unity发布程序的选项
- 使用vs调试unity脚本的方法
- Unity日志工具——封装,跳转
- Unity的Vsync Count垂直同步原理剖析
- UNITY 5.2 Quaternion 类
- unity5.x的bug
- Unity3d 配置OpenCV(EmguCV) 周围环境