Unity小功能

Unity3D插件之NGUI 应用案例(菜单开发、技能CD特效、

2017-04-11  本文已影响378人  TonyWan_AR

框架视图

菜单开发
菜单开发
技能CD特效
技能CD特效
注册功能
注册功能
聊天系统
聊天系统
背包系统
背包系统
开发血条
开发血条

关键代码

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;


//设计游戏相关参数

//定义游戏难度枚举类型
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;


    //创建公开的方法来监听值的改变
    //监听声音的改变
    public void OnVolumeChanged() {
        //测试
        // print("OnVolumeChanged");
        //获取改变的值
        volume = UIProgressBar.current.value;//按 f12跟踪
    }
    //监听游戏等级的改变
    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;

        }

    }

    //监听操作类型的改变
    public void OnControlTypeChanged()
    {
        print("OnControlTypeChanged" + UIPopupList.current.value);
        switch (UIPopupList.current.value.Trim())//去掉空格
        {
            case "键盘":
                controlType = ControlType.KEYBOARD;
                break;
            case "触摸":
                controlType = ControlType.TOUCH;
                break;
            case "鼠标":
                controlType = ControlType.MOUSE;
                break;

        }
    }
    //监听是否满屏
    public void OnIsFullScreenChanged(){
        print("OnIsFullScreenChanged"+UIToggle.current.value);
        isFullscreen = UIToggle.current.value;
    }


    //监听选项按钮被点击方法
    public void OnOptionButtonClick() {
        //动画向前播放;
        startPanelTween.PlayForward();
        optionPanelTween.PlayForward();
    }

    //监听完成按钮被按下
    public void OnCompleteSettingButtonClick() {
        //动画反转播放;
        optionPanelTween.PlayReverse();
        startPanelTween.PlayReverse();
    }
}

Knapsack

using UnityEngine;
using System.Collections;

public class Knapsack : MonoBehaviour {
   //持有16个格子的引用
    public GameObject[] ceil;

    //持有3个装备名字的引用
    public string[] equipmentsName;

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

   

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



    //模拟捡起的方法;
    public void PickUp() {
        //随机捡起一件装备
        //声明一个下标
        int index = Random.Range(0,equipmentsName.Length);
        //获取捡起装备的名字
        string name = equipmentsName[index];

        //是否找到
        bool isFind = false;
        //遍历格子已经有物品
        for (int i = 0; i < ceil.Length; i++)
        {
            //判断当前已经有物品
            if (ceil[i].transform.childCount > 0)
            {
                //获取子类下面组件;
                KnapsackItem item = ceil[i].GetComponentInChildren<KnapsackItem>();
                //判断当前游戏物体名字跟我们捡起来的物品是否一样;
                if (item.sprite.spriteName==name)
                {
                    //标志位设置为true;
                    isFind = true;
                    //调用数量增加方法
                    item.AddCount();
                    //数量增加完毕即中断
                    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);
                    //获取随机生成图片名字
                    go.GetComponent<UISprite>().spriteName = name;
                    //设置好位置;
                    go.transform.localPosition = Vector3.zero;


                    //添加完毕中断
                    break;
                }
            }

            //满格即中断
           

            
        }

    }
}

KnapsackItem

using UnityEngine;
using System.Collections;

public class KnapsackItem : UIDragDropItem {

    //声明公开的类型引用
    public UISprite sprite;

    public UILabel label;

    //声明计数;
    private int count = 1;

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

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


        //Debug.Log("111");
        //把移动的物体的父类设置成被碰撞到的格子
        //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.保存起当前有物品的父类;
            Transform parent = surface.transform.parent;
            //把本物品的安置到另一个格子里面去
            surface.transform.parent = this.transform.parent;
            surface.transform.localPosition = Vector3.zero;

            //把拖拽的物品按放发到本格子去
            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;


    //模拟生成名字
    //定义一个数组
   private string[] names = new string[] {"TonyWan","QQ","facebook","tube" };

    void Start () {

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


    public void OnChatSubmit() {
        //获取相应的输入值;
        string chatMessage = input.value;
        //随机生成后名字
        string name =names[Random.Range(0,4)];
        //吧获得值添加到文本区
        textList.Add(name+"说:"+chatMessage);
        //提交完之后把输入值清空
        input.value = "";

    }
}

MyDropDragItem

using UnityEngine;
using System.Collections;

public class MyDropDragItem :UIDragDropItem {

    //修饰符 受保护的 重写父类的移动 拖拽 松开的方法
    protected override void OnDragDropRelease(GameObject surface)
    {
        base.OnDragDropRelease(surface);//父类方法

        //然后可以进行代码处理
        Debug.Log(surface);
    }
}

Skill

using UnityEngine;
using System.Collections;

public class Skill : MonoBehaviour {
    //定义冷却时间
    private float coldTime = 2;
    //标志位 是否可以释放技能
   // private bool isCanRelease = true;


    //定义一个持有的应用 为了得到fillAmount;
    private UISprite sprite;

    //标志位 是否正在冷却
    private bool isColding=false;
    void Start () {
        //通过查找的方法获取相应组件;
        sprite = transform.Find("Sprite").GetComponent<UISprite>();

    }
    
    
    void Update () {
        //按键A表示释放技能
        if (Input.GetKeyDown(KeyCode.A)&&isColding==false)
        {
            //1.释放技能 创建粒子系统 显示技能特效
            //2.UI显示技能冷却效果
            sprite.fillAmount = 1;
            isColding = true;
        }
        if (isColding)
        {
            //当开始可以冷却时候 计算fillAmount递减值;
            sprite.fillAmount -= (1f / coldTime) * Time.deltaTime;
            //当值显示小于0.05的时候 表示不可以冷却
            if (sprite.fillAmount<=0.05f)
            {
                isColding= false;
                //显示黑色遮罩不可以用
                sprite.fillAmount = 0;
            }
        }
    }
}

TestHudText

using UnityEngine;
using System.Collections;

public class TestHudText : MonoBehaviour {


    //声明一个持有引用
    //private HUDText text;
    public HUDText text;

    //void Start () {
 //       //获取组件
 //       text = this.GetComponent<HUDText>();
    //}
    
    
    void Update () {
        //受伤害
        if (Input.GetMouseButtonDown(0))
        {
            
            text.Add(-10,Color.red,1f);//参数1:伤害值 累加 参数2:颜色 参数3:停留时间
        }

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

TestTextList

using UnityEngine;
using System.Collections;

public class TestTextList : MonoBehaviour {
    
    //声明一个text持有的引用
    private UITextList textList;

    //声明一个行号
    private int lineNumber=0;
    void Start () {

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

        //模拟鼠标按键添加字段
        if (Input.GetMouseButtonDown(0))
          {
            textList.Add("TonyWan"+lineNumber++);
          }
    

    }
}

上一篇下一篇

猜你喜欢

热点阅读