13NGUI插件

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;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,300評(píng)論 25 708
  • 第75篇 《四月云》 天色漸暗日漸消,碧天一去人心憔; 抬頭望云著墨色,渺渺閑游失魂竅。 本是風(fēng)輕云淡時(shí),微寒驟起...
    好郝說(shuō)話閱讀 309評(píng)論 0 4
  • 今晚翻了一會(huì)兒知乎關(guān)于王陽(yáng)明的討論,看到觀點(diǎn)相同的會(huì)欣喜,不同的會(huì)有些不滿。 退出知乎,想了一會(huì)兒我發(fā)現(xiàn)無(wú)論喜還是...
    林不懂閱讀 253評(píng)論 0 0
  • 按照別人的指引盲目前進(jìn), 很可能一步踏空,摔得慘重。 坦白說(shuō),回來(lái)工作大半年了,在機(jī)關(guān)單位我做得只能勉強(qiáng)及格,并不...
    吳可可可er閱讀 459評(píng)論 0 0
  • 最近焦慮的狀態(tài)持續(xù)好久了,一個(gè)明顯的標(biāo)志是喜歡問(wèn)怎么辦。 以前本是一個(gè)看起來(lái)心態(tài)陽(yáng)光的人,給人沒(méi)心沒(méi)肺的假象,開(kāi)導(dǎo)...
    depp船長(zhǎng)先生閱讀 351評(píng)論 0 1

友情鏈接更多精彩內(nèi)容