Unity3D插件之NGUI 應(yīng)用案例(菜單開發(fā)、技能CD特效、注冊功能、聊天系統(tǒng)、背包系統(tǒng)、開發(fā)血條)

框架視圖

菜單開發(fā)
菜單開發(fā)
技能CD特效
技能CD特效
注冊功能
注冊功能
聊天系統(tǒng)
聊天系統(tǒng)
背包系統(tǒng)
背包系統(tǒng)
開發(fā)血條
開發(fā)血條

關(guān)鍵代碼

AgeLimit

using UnityEngine;
using System.Collections;

public class AgeLimit : MonoBehaviour {
    private UIInput uiInput;
    void Start () {
        uiInput = this.GetComponent<UIInput>();
    }
    
    public void OnAgeChange () {
         
        int intValue = int.Parse(uiInput.value);
        if (intValue<18)
        {
            uiInput.value = "18";
        }

        if (intValue>120)
        {
            uiInput.value = "120";
        }
        
    }
}

GameSetting

using UnityEngine;
using System.Collections;


//設(shè)計(jì)游戲相關(guān)參數(shù)

//定義游戲難度枚舉類型
public enum GameGrade
{
    Easy,
    NORMAL,
    DIFFICULT

}

//定義游戲控制類型
public enum ControlType
{
    KEYBOARD,
    TOUCH,
    MOUSE
}
public class GameSetting : MonoBehaviour {

    //定義
    public float volume = 1;
    public GameGrade grade = GameGrade.NORMAL;
    public ControlType controlType = ControlType.KEYBOARD;
    //是否滿屏
    public bool isFullscreen = true;


    //聲明持有動畫位移的引用
    public TweenPosition startPanelTween;
    public TweenPosition optionPanelTween;


    //創(chuàng)建公開的方法來監(jiān)聽值的改變
    //監(jiān)聽聲音的改變
    public void OnVolumeChanged() {
        //測試
        // print("OnVolumeChanged");
        //獲取改變的值
        volume = UIProgressBar.current.value;//按 f12跟蹤
    }
    //監(jiān)聽游戲等級的改變
    public void OnGameGradeChanged() {
        print("OnGameGradeChange" + UIPopupList.current.value);
        switch(UIPopupList.current.value.Trim())
        {//去掉空格
        
            case  "容易":
                grade = GameGrade.Easy;
                break;
            case "一般":
                grade = GameGrade.NORMAL;
                break;
            case "困難":
                grade = GameGrade.DIFFICULT;
                break;

        }

    }

    //監(jiān)聽操作類型的改變
    public void OnControlTypeChanged()
    {
        print("OnControlTypeChanged" + UIPopupList.current.value);
        switch (UIPopupList.current.value.Trim())//去掉空格
        {
            case "鍵盤":
                controlType = ControlType.KEYBOARD;
                break;
            case "觸摸":
                controlType = ControlType.TOUCH;
                break;
            case "鼠標(biāo)":
                controlType = ControlType.MOUSE;
                break;

        }
    }
    //監(jiān)聽是否滿屏
    public void OnIsFullScreenChanged(){
        print("OnIsFullScreenChanged"+UIToggle.current.value);
        isFullscreen = UIToggle.current.value;
    }


    //監(jiān)聽選項(xiàng)按鈕被點(diǎn)擊方法
    public void OnOptionButtonClick() {
        //動畫向前播放;
        startPanelTween.PlayForward();
        optionPanelTween.PlayForward();
    }

    //監(jiān)聽完成按鈕被按下
    public void OnCompleteSettingButtonClick() {
        //動畫反轉(zhuǎn)播放;
        optionPanelTween.PlayReverse();
        startPanelTween.PlayReverse();
    }
}

Knapsack

using UnityEngine;
using System.Collections;

public class Knapsack : MonoBehaviour {
   //持有16個(gè)格子的引用
    public GameObject[] ceil;

    //持有3個(gè)裝備名字的引用
    public string[] equipmentsName;

    //持有物品的引用
    public GameObject item;

   

    //模擬按下空格鍵即生成物品
     void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            PickUp();
        }
    }



    //模擬撿起的方法;
    public void PickUp() {
        //隨機(jī)撿起一件裝備
        //聲明一個(gè)下標(biāo)
        int index = Random.Range(0,equipmentsName.Length);
        //獲取撿起裝備的名字
        string name = equipmentsName[index];

        //是否找到
        bool isFind = false;
        //遍歷格子已經(jīng)有物品
        for (int i = 0; i < ceil.Length; i++)
        {
            //判斷當(dāng)前已經(jīng)有物品
            if (ceil[i].transform.childCount > 0)
            {
                //獲取子類下面組件;
                KnapsackItem item = ceil[i].GetComponentInChildren<KnapsackItem>();
                //判斷當(dāng)前游戲物體名字跟我們撿起來的物品是否一樣;
                if (item.sprite.spriteName==name)
                {
                    //標(biāo)志位設(shè)置為true;
                    isFind = true;
                    //調(diào)用數(shù)量增加方法
                    item.AddCount();
                    //數(shù)量增加完畢即中斷
                    break;
                }


            }
        }

        if (isFind==false)
        {


            //遍歷 如果格子沒物品即添加 添加完成后中斷;
            for (int i = 0; i < ceil.Length; i++)
            {
                //判斷是否有物品
                if (ceil[i].transform.childCount == 0)
                {
                    //添加物品
                    GameObject go = NGUITools.AddChild(ceil[i], item);
                    //獲取隨機(jī)生成圖片名字
                    go.GetComponent<UISprite>().spriteName = name;
                    //設(shè)置好位置;
                    go.transform.localPosition = Vector3.zero;


                    //添加完畢中斷
                    break;
                }
            }

            //滿格即中斷
           

            
        }

    }
}

KnapsackItem

using UnityEngine;
using System.Collections;

public class KnapsackItem : UIDragDropItem {

    //聲明公開的類型引用
    public UISprite sprite;

    public UILabel label;

    //聲明計(jì)數(shù);
    private int count = 1;

    //物品數(shù)量增加
    public void AddCount() {
        count++;
        label.text = count + "";
    }

    protected override void OnDragDropRelease(GameObject surface)
    {
        base.OnDragDropRelease(surface);
        //判斷格子下面是否有子物體;
        //Debug.Log(surface.transform.childCount);


        //Debug.Log("111");
        //把移動的物體的父類設(shè)置成被碰撞到的格子
        //this.transform.parent = surface.transform;
        //與碰撞的物體位置保持一致
        // this.transform.position = surface.transform.position;
        //this.transform.localPosition = Vector3.zero;


        //1.判斷格子下面是否有物體 沒有就按放
        //2.判斷便簽
        if (surface.tag=="Ceil")
        {  
            //安置物品
            this.transform.parent = surface.transform;
            this.transform.localPosition = Vector3.zero;

        }
        else if (surface.tag=="KnapsackItem")
        {
            //交換位置
            //1.保存起當(dāng)前有物品的父類;
            Transform parent = surface.transform.parent;
            //把本物品的安置到另一個(gè)格子里面去
            surface.transform.parent = this.transform.parent;
            surface.transform.localPosition = Vector3.zero;

            //把拖拽的物品按放發(fā)到本格子去
            this.transform.parent = parent;
            this.transform.localPosition = Vector3.zero;
        }


    }
}

MyChatInput

using UnityEngine;
using System.Collections;

public class MyChatInput : MonoBehaviour {
    
    //聲明持有引用
    private UIInput input;


    //聲明公開持有引用

    public UITextList textList;


    //模擬生成名字
    //定義一個(gè)數(shù)組
   private string[] names = new string[] {"TonyWan","QQ","facebook","tube" };

    void Start () {

        input = this.GetComponent<UIInput>();
    }
    


    public void OnChatSubmit() {
        //獲取相應(yīng)的輸入值;
        string chatMessage = input.value;
        //隨機(jī)生成后名字
        string name =names[Random.Range(0,4)];
        //吧獲得值添加到文本區(qū)
        textList.Add(name+"說:"+chatMessage);
        //提交完之后把輸入值清空
        input.value = "";

    }
}

MyDropDragItem

using UnityEngine;
using System.Collections;

public class MyDropDragItem :UIDragDropItem {

    //修飾符 受保護(hù)的 重寫父類的移動 拖拽 松開的方法
    protected override void OnDragDropRelease(GameObject surface)
    {
        base.OnDragDropRelease(surface);//父類方法

        //然后可以進(jìn)行代碼處理
        Debug.Log(surface);
    }
}

Skill

using UnityEngine;
using System.Collections;

public class Skill : MonoBehaviour {
    //定義冷卻時(shí)間
    private float coldTime = 2;
    //標(biāo)志位 是否可以釋放技能
   // private bool isCanRelease = true;


    //定義一個(gè)持有的應(yīng)用 為了得到fillAmount;
    private UISprite sprite;

    //標(biāo)志位 是否正在冷卻
    private bool isColding=false;
    void Start () {
        //通過查找的方法獲取相應(yīng)組件;
        sprite = transform.Find("Sprite").GetComponent<UISprite>();

    }
    
    
    void Update () {
        //按鍵A表示釋放技能
        if (Input.GetKeyDown(KeyCode.A)&&isColding==false)
        {
            //1.釋放技能 創(chuàng)建粒子系統(tǒng) 顯示技能特效
            //2.UI顯示技能冷卻效果
            sprite.fillAmount = 1;
            isColding = true;
        }
        if (isColding)
        {
            //當(dāng)開始可以冷卻時(shí)候 計(jì)算fillAmount遞減值;
            sprite.fillAmount -= (1f / coldTime) * Time.deltaTime;
            //當(dāng)值顯示小于0.05的時(shí)候 表示不可以冷卻
            if (sprite.fillAmount<=0.05f)
            {
                isColding= false;
                //顯示黑色遮罩不可以用
                sprite.fillAmount = 0;
            }
        }
    }
}

TestHudText

using UnityEngine;
using System.Collections;

public class TestHudText : MonoBehaviour {


    //聲明一個(gè)持有引用
    //private HUDText text;
    public HUDText text;

    //void Start () {
 //       //獲取組件
 //       text = this.GetComponent<HUDText>();
    //}
    
    
    void Update () {
        //受傷害
        if (Input.GetMouseButtonDown(0))
        {
            
            text.Add(-10,Color.red,1f);//參數(shù)1:傷害值 累加 參數(shù)2:顏色 參數(shù)3:停留時(shí)間
        }

        //治療值
        if (Input.GetMouseButtonDown(1))
        {
            text.Add(10,Color.green,1f);
        }
    }
}

TestTextList

using UnityEngine;
using System.Collections;

public class TestTextList : MonoBehaviour {
    
    //聲明一個(gè)text持有的引用
    private UITextList textList;

    //聲明一個(gè)行號
    private int lineNumber=0;
    void Start () {

        textList = this.GetComponent<UITextList>();
    }
    
    
    void Update () {

        //模擬鼠標(biāo)按鍵添加字段
        if (Input.GetMouseButtonDown(0))
          {
            textList.Add("TonyWan"+lineNumber++);
          }
    

    }
}

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

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

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