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 () {
}
}
效果如下