下載NGUI包
導入NGUI3.9.1版本package 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根節點的方法 this
private GameObject Window{ set; get;} void CreateUI() { //建立根節點 this.Window = NGUITools.CreateUI(false).gameObject; }
在Window上添加滾動子視圖 spa
private UIScrollView scrollView; void CreateUI() { //建立根節點 this.Window = NGUITools.CreateUI(false).gameObject; //在根節點上建立一個UIScrollView子控件 this.scrollView = NGUITools.AddChild<UIScrollView>(this.Window); }
添加下載圖片的方法 rest
private Texture texture; private UITexture imageView; //一個協程用到的函數,下載圖片 IEnumerator DownloadTexture() { WWW www = new WWW ("http://p.baidupcs.com/file/00fb26654a14f822a8b8d4f99bffff4f?bkt=p2-nj-562&fid=4213918792-250528-600499188078355&time=1440052584&sign=FDTAXERBH-DCb740ccc5511e5e8fedcff06b081203-JvUURjnKMtJDPCeN7DC01lNSiAM%3D&to=pb&fm=Nan,B,G,bs&sta_dx=0&sta_cs=0&sta_ft=png&sta_ct=0&fm2=Nanjing02,B,G,bs&newver=1&newfm=1&secfm=1&flow_ver=3&expires=8h&rt=sh&r=853121535&mlogid=2433540290&vuk=4213918792&vbdid=724775054&fin=icon.png&fn=icon.png&uta=0&rtype=0&iv=1&isw=0"); yield return www; this.texture = www.texture; }
重置ScollView上的Panel對象 code
/// <summary> /// 重置Panel對象 /// </summary> /// <param name="panel">Panel.</param> void ResetPanel(UIPanel panel) { //縮小,而且居中顯示 panel.transform.localPosition = new Vector3(0,0,0); //設置移動的位置 panel.clipOffset = new Vector2(0,0); //設置大小 panel.baseClipRegion = new Vector4 (0, 0, 100, 100); }
設定滾動視圖的一些屬性 orm
private UIScrollView scrollView; void CreateUI() { //建立根節點 this.Window = NGUITools.CreateUI(false).gameObject; //在根節點上建立一個UIScrollView子控件 this.scrollView = NGUITools.AddChild<UIScrollView>(this.Window); //設定隨意移動 this.scrollView.movement = UIScrollView.Movement.Unrestricted; //重置panel對象 ResetPanel (this.scrollView.GetComponent<UIPanel> ()); //建立ImageView CreateImage(); }
建立一個須要顯示下載圖片的ImageView 協程
private UITexture imageView; void CreateImage() { this.imageView = NGUITools.AddChild<UITexture> (this.scrollView.gameObject); this.imageView.SetRect (-50, -50, 100, 100); //給圖片對象添加缺失組件 NGUITools.AddMissingComponent<UIDragScrollView> (this.imageView.gameObject); NGUITools.AddMissingComponent<BoxCollider> (this.imageView.gameObject); UIButton button = NGUITools.AddMissingComponent<UIButton> (this.imageView.gameObject); //添加按鈕的觸發事件,單擊圖片放大、縮小 button.onClick.Add (new EventDelegate (() => { //小圖片 if (this.imageView.width == 100) { //放大 this.imageView.SetRect(-512,-512,1024,1024); } else//大圖片 { ResetPanel(this.scrollView.GetComponent<UIPanel>()); this.imageView.SetRect(-50,-50,100,100); } })); //設定碰撞器對象能夠自動縮放 this.imageView.autoResizeBoxCollider = true; this.imageView.ResizeCollider (); }
整個MainCameraScript.cs的代碼以下 對象
using UnityEngine; using System.Collections; public class MainCameraScript : MonoBehaviour { private GameObject Window{ set; get;} private Texture texture; IEnumerator DownloadTexture() { WWW www = new WWW ("http://p.baidupcs.com/file/00fb26654a14f822a8b8d4f99bffff4f?bkt=p2-nj-562&fid=4213918792-250528-600499188078355&time=1440052584&sign=FDTAXERBH-DCb740ccc5511e5e8fedcff06b081203-JvUURjnKMtJDPCeN7DC01lNSiAM%3D&to=pb&fm=Nan,B,G,bs&sta_dx=0&sta_cs=0&sta_ft=png&sta_ct=0&fm2=Nanjing02,B,G,bs&newver=1&newfm=1&secfm=1&flow_ver=3&expires=8h&rt=sh&r=853121535&mlogid=2433540290&vuk=4213918792&vbdid=724775054&fin=icon.png&fn=icon.png&uta=0&rtype=0&iv=1&isw=0"); yield return www; this.texture = www.texture; } private UIScrollView scrollView; void CreateUI() { //建立根節點 this.Window = NGUITools.CreateUI(false).gameObject; //在根節點上建立一個UIScrollView子控件 this.scrollView = NGUITools.AddChild<UIScrollView>(this.Window); //設定隨意移動 this.scrollView.movement = UIScrollView.Movement.Unrestricted; //重置panel對象 ResetPanel (this.scrollView.GetComponent<UIPanel> ()); //建立ImageView CreateImage(); } private UITexture imageView; void CreateImage() { this.imageView = NGUITools.AddChild<UITexture> (this.scrollView.gameObject); this.imageView.SetRect (-50, -50, 100, 100); //給圖片對象添加缺失組件 NGUITools.AddMissingComponent<UIDragScrollView> (this.imageView.gameObject); NGUITools.AddMissingComponent<BoxCollider> (this.imageView.gameObject); UIButton button = NGUITools.AddMissingComponent<UIButton> (this.imageView.gameObject); //添加按鈕的觸發事件,單擊圖片放大、縮小 button.onClick.Add (new EventDelegate (() => { //小圖片 if (this.imageView.width == 100) { //放大 this.imageView.SetRect(-512,-512,1024,1024); } else//大圖片 { ResetPanel(this.scrollView.GetComponent<UIPanel>()); this.imageView.SetRect(-50,-50,100,100); } })); //設定碰撞器對象能夠自動縮放 this.imageView.autoResizeBoxCollider = true; this.imageView.ResizeCollider (); } /// <summary> /// 重置Panel對象 /// </summary> /// <param name="panel">Panel.</param> void ResetPanel(UIPanel panel) { //縮小,而且居中顯示 panel.transform.localPosition = new Vector3(0,0,0); //設置移動的位置 panel.clipOffset = new Vector2(0,0); //設置大小 panel.baseClipRegion = new Vector4 (0, 0, 100, 100); } // Use this for initialization void Start () { CreateUI (); StartCoroutine ("DownloadTexture"); } // Update is called once per frame void Update () { //設置圖片 if (this.texture != null && this.imageView.mainTexture == null) { this.imageView.mainTexture = this.texture; } } }
效果以下
事件