[Unity]Unity開發NGUI代碼實現ScrollView(滾動視圖)

Unity開發NGUI代碼實現ScrollView(滾動視圖)

下載NGUI包 導入NGUI3.9.1版本package dom

  • 連接: http://pan.baidu.com/s/1mgksPBU 密碼: bacy

導入NGUI包  ide

建立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上添加滾動子視圖 this

void CreateUI()
    { //建立根節點 this.Window = NGUITools.CreateUI(false).gameObject; //在根節點上建立一個UIScrollView子控件 UIScrollView scrollView = NGUITools.AddChild<UIScrollView>
(this.Window);

    }

在滾動視圖上添加Grid表格調整佈局 spa

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的方法 code

/// <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的代碼以下 ip

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 () {
    
    }
}

效果以下  開發

相關文章
相關標籤/搜索