[Unity]Unity开发NGUI代码实现ScrollView(滚动视图)
2015-08-20 11:32
716 查看
Unity开发NGUI代码实现ScrollView(滚动视图)
下载NGUI包导入NGUI3.9.1版本package
链接: http://pan.baidu.com/s/1mgksPBU 密码: bacy
导入NGUI包
创建MainCameraScript.cs脚本
MainCameraScript.cs
using UnityEngine; using System.Collections; public class MainCameraScript : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } }
创建NGUI根节点的方法
private GameObject Window{ set; get;} void CreateUI() { //创建根节点 this.Window = NGUITools.CreateUI(false).gameObject; }
在Window上添加滚动子视图
void CreateUI() { //创建根节点 this.Window = NGUITools.CreateUI(false).gameObject; //在根节点上创建一个UIScrollView子控件 UIScrollView scrollView = NGUITools.AddChild<UIScrollView>(this.Window); }
在滚动视图上添加Grid表格调整布局
void CreateUI() { //创建根节点 this.Window = NGUITools.CreateUI(false).gameObject; //在根节点上创建一个UIScrollView子控件 UIScrollView scrollView = NGUITools.AddChild<UIScrollView>(this.Window); //在滚动视图上添加UIGrid子控件,来调整布局 UIGrid grid = NGUITools.AddChild<UIGrid>(scrollView.gameObject); //设置grid表格的布局方向 grid.arrangement = UIGrid.Arrangement.Horizontal; grid.cellWidth = 100.0f; grid.cellHeight = 100.0f; grid.animateSmoothly = false; }
添加创建Label的方法
/// <summary> /// 创建一个小Label控件 /// </summary> /// <returns>The label item.</returns> UILabel CreateLabelItem(string name,GameObject parent) { //创建一个Lalel控件 UILabel label = NGUITools.AddChild<UILabel> (parent); //修改Label的字体及颜色 Font f = (Font)Resources.Load ("Arial", typeof(Font)); label.bitmapFont = NGUITools.AddMissingComponent<UIFont>(label.gameObject); label.bitmapFont.dynamicFont = f; label.color = Color.red; //设置Label要显示的文字 label.text = name; //添加滚动ScrollView时要用到的碰撞器和脚本 label.autoResizeBoxCollider = true; NGUITools.AddMissingComponent<UIDragScrollView> (label.gameObject); NGUITools.AddMissingComponent<BoxCollider> (label.gameObject); //重新调整碰撞器的大小 label.ResizeCollider (); return label; }
在Grid上添加10个Label控件
void CreateUI() { //创建根节点 this.Window = NGUITools.CreateUI(false).gameObject; //在根节点上创建一个UIScrollView子控件 UIScrollView scrollView = NGUITools.AddChild<UIScrollView>(this.Window); //在滚动视图上添加UIGrid子控件,来调整布局 UIGrid grid = NGUITools.AddChild<UIGrid>(scrollView.gameObject); //设置grid表格的布局方向 grid.arrangement = UIGrid.Arrangement.Horizontal; grid.cellWidth = 100.0f; grid.cellHeight = 100.0f; grid.animateSmoothly = false; //在Grid表格上添加20个Label对象 for (int i = 0; i < 10; i++) { CreateLabelItem (Random.Range (100, 999).ToString(), grid.gameObject); } //重新排版 grid.Reposition (); }
整个MainCameraScript.cs的代码如下
using UnityEngine; using System.Collections; public class MainCameraScript : MonoBehaviour { private GameObject Window{ set; get;} void CreateUI() { //创建根节点 this.Window = NGUITools.CreateUI(false).gameObject; //在根节点上创建一个UIScrollView子控件 UIScrollView scrollView = NGUITools.AddChild<UIScrollView>(this.Window); //在滚动视图上添加UIGrid子控件,来调整布局 UIGrid grid = NGUITools.AddChild<UIGrid>(scrollView.gameObject); //设置grid表格的布局方向 grid.arrangement = UIGrid.Arrangement.Horizontal; grid.cellWidth = 100.0f; grid.cellHeight = 100.0f; grid.animateSmoothly = false; //在Grid表格上添加20个Label对象 for (int i = 0; i < 10; i++) { CreateLabelItem (Random.Range (100, 999).ToString(), grid.gameObject); } //重新排版 grid.Reposition (); } /// <summary> /// 创建一个小Label控件 /// </summary> /// <returns>The label item.</returns> UILabel CreateLabelItem(string name,GameObject parent) { //创建一个Lalel控件 UILabel label = NGUITools.AddChild<UILabel> (parent); //修改Label的字体及颜色 Font f = (Font)Resources.Load ("Arial", typeof(Font)); label.bitmapFont = NGUITools.AddMissingComponent<UIFont>(label.gameObject); label.bitmapFont.dynamicFont = f; label.color = Color.red; //设置Label要显示的文字 label.text = name; //添加滚动ScrollView时要用到的碰撞器和脚本 label.autoResizeBoxCollider = true; NGUITools.AddMissingComponent<UIDragScrollView> (label.gameObject); NGUITools.AddMissingComponent<BoxCollider> (label.gameObject); //重新调整碰撞器的大小 label.ResizeCollider (); return label; } // Use this for initialization void Start () { CreateUI (); } // Update is called once per frame void Update () { } }
效果如下
相关文章推荐
- 使用.NET Reflector 查看Unity引擎里面的DLL文件
- Unity绘制GUI连连看(尚未完善效果和重置)
- unity第二讲
- Unity3D 中 protobuf-net
- Unity中OnGUI绘制贪吃蛇
- Unity3D shader简介
- unity中的[xxxxxx]特性(Attributes)
- unity中的[xxxxxx]特性(Attributes)
- unity中鼠标拖拽物体移动
- unity3d 学习笔记_____Native2d 刚体、冲击、联合使用
- 全面理解Unity加载和内存管理
- Unity 3D中的内存管理
- Unity3d基础组件 (Component) 和脚本 (Script) 关系
- Unity项目中UI同学需知的程序相关要点
- Unity Shader 学习笔记 (八) 语义词与语义绑定
- 浅尝Unity 3D的Asset Bundle知识(七)-----依赖的利用
- Unity 3D Android对dll加密和重编译mono源码进行解密
- unity3d 系统方法执行顺序
- Unity 2D 教程汇总
- Unity 2D 英文教程(www.raywenderlich.com)