NGUI是一款常用插件,方便我們利用一些常見(jiàn)的代碼,UI通過(guò)繼承進(jìn)行我們想要的開(kāi)發(fā)效果###
一
1.如何讓物體可以被拖拽.--------添加NGUI里面的UIDragDropItem腳本.
2.如何監(jiān)聽(tīng)物品被拖拽的事件.----重寫(xiě)父類的特定方法,進(jìn)行判斷.
3.根據(jù)事件,進(jìn)行邏輯判斷.---調(diào)用父類的重寫(xiě)方法,對(duì)條件進(jìn)行限定,得到想要的邏輯.

效果展示
Drop_Tes代碼展示
public class Drop_Test : UIDragDropItem {
protected override void OnDragDropStart()
{
base.OnDragDropStart();
Debug.Log("開(kāi)始拖拽了。。。。。。。。。。上");
}
}
二、制作背景,格子以及物品的Prefab.
1.制作Prefab方式.
2.制作Prefab的細(xì)節(jié)考慮.
3.制作Prefab的好處.----------可以使游戲?qū)ο蠛唾Y源重復(fù)利用,相同的游戲?qū)ο罂梢允褂猛粋€(gè)預(yù)設(shè)體創(chuàng)建
對(duì)預(yù)設(shè)體進(jìn)行修改后,所有的游戲?qū)ο蠖紩?huì)相應(yīng)的改變.

圖集的制作

圖集方便我們對(duì)素材進(jìn)行管理
三、實(shí)現(xiàn)拖拽物品.
1.如何讓物品可以被拖拽.
2.如何監(jiān)聽(tīng)物品被拖拽的過(guò)程.
3.交換物品的邏輯.

效果展示
腳本實(shí)現(xiàn)代碼:每個(gè)物體都要加碰撞器,用來(lái)檢測(cè).
public class DropItem_06 : UIDragDropItem{
protected override void OnDragDropRelease(GameObject surface)
{
base.OnDragDropRelease(surface);
Debug.Log(surface.name);
//如果 surface==null,讓物品位置回到原位
if (surface == null || surface.tag == "bg")
{
transform.localPosition = Vector3.zero;
}
else if (surface.tag == "cell")
{
transform.parent = surface.transform;
transform.localPosition = Vector3.zero;
}
//如果下面是物品,就應(yīng)該交換位置
else
{
Transform temp = surface.transform.parent;
surface.transform.parent = transform.parent;
transform.parent = temp;
surface.transform.localPosition= Vector3.zero;
transform.transform.localPosition= Vector3.zero;
}
}
}
四、背包撿起物品的功能,完成物品的累加和新添.
1.如何將物體添加到背包中.
2.如何將物品進(jìn)行累加.
3.累加和新添的邏輯.

效果展示
代碼展示:
public class Bag : MonoBehaviour {
public GameObject[] cells;
GameObject one;
int num;
UILabel lab;
// Use this for initialization
void Start () {
one = Resources.Load<GameObject>("one");
num = 0;
}
// Update is called once per frame
void Update () {
// NGUITools.AddChild();
if (Input.GetKeyDown(KeyCode.A))
{
for (int i = 0; i < cells.Length; i++)
{
if (cells[i].transform.childCount ==1)//為什么是1,有l(wèi)abel這個(gè)子物體
{
NGUITools.AddChild(cells[i], one);
break;
}
else {
if (cells[i].transform.GetChild(1).name==one.name+"(Clone)")
{
lab = cells[i].GetComponentInChildren<UILabel>();
num++;
lab.text = num.ToString();
break;
}
}
}
}
}
}
五、血條,進(jìn)度條,控制扣血.(Slider)
1.NGUI空間介紹之Slider.
2.NGUI制作血條,進(jìn)度條等.
3.控制血條扣血.

結(jié)果展示
腳本展示
public class Blood : MonoBehaviour {
private UISlider bloodValue;
// Use this for initialization
public GameObject UIBlood;
void Start () {
// UIBlood = this.gameObject;
bloodValue = UIBlood.GetComponent<UISlider>();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.A))
{
bloodValue.value -= 0.05f;
}
}
}
六、創(chuàng)建背景圖,并且鋪滿整個(gè)屏幕的功能.
1.創(chuàng)建背景圖.
2.背景圖鋪滿整個(gè)屏幕的兩種方式.通過(guò)錨點(diǎn)與通過(guò)代碼控制.

第一種方式:通過(guò)錨點(diǎn)

Paste_Image.png
代碼控制管理:
public class backGround : MonoBehaviour {
UISprite backImg;
UIRoot root;
// Use this for initialization
void Start () {
backImg = GetComponent<UISprite>();
root = GameObject.FindGameObjectWithTag("UIroot").GetComponent<UIRoot>();
adject();
}
// Update is called once per frame
void Update () {
}
void adject()
{
float ajHight = (float)root.activeHeight / Screen.height;
int height = Mathf.CeilToInt(Screen.height * ajHight);
int width = Mathf.CeilToInt(Screen.width * ajHight);
backImg.height = height;
backImg.width = width;
//為什么不通過(guò)直接把值root的高度和寬度值賦給圖片的,要通過(guò)一個(gè)上面
//的一個(gè)比例系數(shù)這樣做呢!因?yàn)橹挥衦oot.activeHeight這個(gè)屬性,沒(méi)有寬度屬性。。所以同過(guò)比例系數(shù)獲取
// backImg.height = root.activeHeight;
//backImg.width=root.a
}
}
七、UI背景無(wú)縫移動(dòng)的實(shí)現(xiàn).
1.一些API的使用.
2.無(wú)縫移動(dòng)的關(guān)鍵點(diǎn).
理論是,只要我們思想符合邏輯,就不會(huì)有這種問(wèn)題,由于圖片的壓縮,擴(kuò)大在我們處理的時(shí)候.
那個(gè)寬度就不會(huì)是我們想要的結(jié)果。。。。。就會(huì)出現(xiàn)有縫隙的問(wèn)題,所以要人為的處理一下.

效果展示

問(wèn)題所示
代碼管理控制:
public class backGround : MonoBehaviour {
public UISprite other;
UISprite backImg;
UISprite picture;
UIRoot root;
// Use this for initialization
void Start () {
picture = GetComponent<UISprite>();
backImg = GetComponent<UISprite>();
root = GameObject.FindGameObjectWithTag("UIroot").GetComponent<UIRoot>();
adject();
//if (other.transform.localPosition.x<this.transform.localPosition.x)
//{
// other.transform.localPosition = new Vector3(-picture.width+15,0,0);
//}
}
// Update is called once per frame
void Update () {
transform.Translate(Time.deltaTime,0,0);
if (transform.localPosition.x>=picture.width)
{
transform.localPosition = new Vector3(-picture.width+35,0,0);//處理縫隙問(wèn)題
}
}
void adject()
{
float ajHight = (float)root.activeHeight / Screen.height;
int height = Mathf.CeilToInt(Screen.height * ajHight);
int width = Mathf.CeilToInt(Screen.width * ajHight);
backImg.height = height;
backImg.width = width;
//為什么不通過(guò)直接把值root的高度和寬度值賦給圖片的,要通過(guò)一個(gè)上面
//的一個(gè)比例系數(shù)這樣做呢!因?yàn)橹挥衦oot.activeHeight這個(gè)屬性,沒(méi)有寬度屬性。。所以同過(guò)比例系數(shù)獲取
// backImg.height = root.activeHeight;
//backImg.width=root.a
}
}
八、創(chuàng)建商鋪店里面的UI顯示.
1.常用控件的熟悉.
2.對(duì)開(kāi)發(fā)UI的了解.
3.把代碼與實(shí)際應(yīng)用相結(jié)合.
實(shí)現(xiàn)的功能就是我們?cè)谏啼侟c(diǎn)點(diǎn)擊按鈕之后,跳到相應(yīng)物品身上去,就行物品個(gè)數(shù)輸入.

物品商店的創(chuàng)建

輸入框的制作

實(shí)現(xiàn)效果展示

事件的處理
代碼管理:
public class BuyBtn : MonoBehaviour {
public GameObject SureBuyWindow;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void BuyWindowShow()
{
if (SureBuyWindow.activeSelf == false)
{
SureBuyWindow.SetActive(true);
}
else
{
SureBuyWindow.SetActive(false);
}
this.gameObject.GetComponent<UIButton>().enabled = false;
}
}