1)重置uilabel的中心點(label中的文字左對齊):widget面板中修改pivot(overflow設(shè)置為resizefreely)

2)在固定區(qū)域內(nèi)顯示一段文字

3)AtlasMaker制作圖集??
在NGUI下open Atlas Maker? 新建一個圖集 選中texture中的圖片進行導(dǎo)入操作,增 直接選中圖片,刪 點八叉 生成的圖集放在新建的Atlasmaker文件夾中,包含有三個文件,mat,prefab和png


4)UISprite顯示圖片
創(chuàng)建UISprite組件,NGUI-Create-Sprite?
選擇圖集

5)UISprite 面板屬性
Sliced:九宮模式,適用于按鈕背景圖處理,不規(guī)則的背景圖,先選擇sliced的type ,再點擊選中圖片的edit進行切割


filled:進度模式 ,可展現(xiàn)讀條動畫

6)UILabel和UISprite制作
先新建NGUI的2D根節(jié)點,在UI Root下(紫色框內(nèi))右鍵添加背景Sprite,在背景Sprite上右鍵添加他的sprite和label child?

7)使用代碼動態(tài)創(chuàng)建UI Sprite UI物體
using UnityEngine;
using System.Collections;
public class CreateUISprite : MonoBehaviour {
? ? //使用代碼動態(tài)創(chuàng)建UI Sprite UI物體
? ? private Transform m_Transform;
void Start () {
? ? ? ? m_Transform = gameObject.GetComponent<Transform>();//查找進行賦值
? ? ? ? //創(chuàng)建(實例化)一個GO對象
? ? ? ? GameObject uiSprite = new GameObject("ElfAC");
? ? ? ? //將uisprite設(shè)置為UI Root的子物體
? ? ? ? uiSprite.GetComponent<Transform>().SetParent(m_Transform);
? ? ? ? //重置scale
? ? ? ? uiSprite.GetComponent<Transform>().localScale = Vector3.one;
? ? ? ? //添加組件
? ? ? ? UISprite sprite = uiSprite.AddComponent<UISprite>();
? ? ? ? //讀取圖集 Resources.Load<T>("")該api需要 下面的名字對應(yīng)的資源存放在Resources這個文件夾中
? ? ? ? UIAtlas atlas = Resources.Load<UIAtlas>("GameAtlas") ;
? ? ? ? //給組件指定圖集
? ? ? ? sprite.atlas = atlas;
? ? ? ? //給組件指定圖片
? ? ? ? sprite.spriteName = "11000414";
}
void Update () {
}
}

8)UI Button 面板控制
1、按鈕制作
先創(chuàng)建一個2D UI面板? 添加UI Sprite 來展現(xiàn)一張圖片或者一段文字 ;
給ui圖片添加box collider組件,確定可以點擊的區(qū)域(右鍵 attach找);
右鍵attach添加button script 完成


9)UIButton代碼控制
1、按鈕點擊事件綁定
法一:面板屬性欄綁定
①創(chuàng)建一個代碼文件,定義一個公開的方法,掛載到一個游戲物體上。
②將該腳本拖拽到UIButton的OnClick事件上。

法二:代碼綁定
①創(chuàng)建一個代碼文件,掛載到按鈕物體上(同法一)。
②定義一個叫做OnClick()的方法。
using UnityEngine;
using System.Collections;
public class MyButton : MonoBehaviour {
? ? public void ButtonClick()
? ? {
? ? ? ? Debug.Log("我被點擊了........");
? ? }
? ? private void OnClick()
? ? {
? ? ? ? Debug.Log("代碼綁定的點擊.......");
? ? }
}
2、按鈕交互聲音
①Attach-->Play Sound Script添加組件
②Audio Clip指定一個聲音文件
③指定Trigger出發(fā)該聲音播放的時間,常用的是OnClick。

10)UI動態(tài)加載
1、將制作好的UI面板拖拽成為一個Prefab資源,放到Resources文件夾下;
2、使用Resources.Load()將該資源加載到內(nèi)存中;
3、實例化該UI資源對象,放到UIRoot下完成顯示;
相關(guān)API:NGUITools.AddChild(父對象,子對象);
NGUI提供的一個實例化物體,設(shè)置子物體的一個內(nèi)置函數(shù),操作UI盡量使用該NGUI封裝函數(shù)。
using UnityEngine;
using System.Collections;
public class UIManager : MonoBehaviour {
? ? private GameObject prefab_Info;
void Start () {
? ? ? ? prefab_Info = Resources.Load<GameObject>("Info");
? ? ? ? NGUITools.AddChild(gameObject, prefab_Info);
}
}
