unityUnityUI

Unity3D之UGUI 应用案例详解(目录界面、游戏场景及CD

2017-04-14  本文已影响2510人  TonyWan_AR

目录界面

游戏场景及CD技能


开始界面

菜单开发

角色面板及背包系统

关卡界面

任务列表

设置界面

登录界面

关键代码

GameManager

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {


    public void OnStratGame(int sceneIndex) {
        Application.LoadLevel(sceneIndex);
    }
}

LevelButtonScrollList



using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class LevelButtonScrollList : MonoBehaviour, IBeginDragHandler,IDragHandler,IEndDragHandler{
    //持有引用
    private ScrollRect scrollRect;

    //定义页面的数组
    private float[] pageArray = new float[] {0, 0.333f,0.666f,1};

    //差值运算 滑动页面时有缓慢效果;
    private float targetHorizontalPosition=0;

    //滑行速度
    public float smoothSpeed = 4;

    //定义拖拽标志位
    private bool isDraging=false;


    //定义选中的数组;
    public Toggle[] toggle;
    void Start () {
    //获取组件
        scrollRect=this.GetComponent<ScrollRect>();
    }
    
    
    void Update () {
        //当拖拽结束后才执行吃函数
        if (!isDraging)
        {
            //差值运算
            scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition, targetHorizontalPosition, Time.deltaTime * smoothSpeed);//参数1:起始位置 参数2:目标位置 参数3:时间变化

        }

    
    }

    //滑动开始
    public void OnBeginDrag(PointerEventData eventData)
    {
        //开始拖拽
        isDraging = true;
    }

    /// <summary>
    /// 控制滑动列表按照页数滚动
    /// </summary>
    /// <param name="eventData"></param>
    public void OnEndDrag(PointerEventData eventData)//滑动结束
    {
        //拖拽结束
        isDraging = false;

       //滑动结束后获取相应的位置;从而来通过滑动条控制页数
       Vector2 temp = scrollRect.normalizedPosition;//二维向量;
        Debug.Log(temp);
        //换可以通过水平位置获取活动值;
        float  posx = scrollRect.horizontalNormalizedPosition;

        //定义下标
        int index = 0;
        //比较位移值;
        float offset = Mathf.Abs(pageArray[index] - posx );//取 区间值减移动位置值
        
        //遍历数组
        for (int i = 1; i < pageArray.Length; i++)
        {
            float offsetTemp = Mathf.Abs(pageArray[i] - posx);
            //如果当前值小于滑动值;
            if (offsetTemp<offset)
            {
                //记录下标
                index = i;

                offset = offsetTemp;
            }
           
        }

        //离最近的值进行页面设置
       // scrollRect.horizontalNormalizedPosition = pageArray[index];

        targetHorizontalPosition = pageArray[index];

        //滑动到当前页数把toggle按钮设置true;
        toggle[index].isOn = true;
    }


    //注册4个公开的方法;
    public void MoveToPage1(bool isOn) {

        if (isOn)
        {
            targetHorizontalPosition = pageArray[0];
        }
    }

    public void MoveToPage2(bool isOn)
    {

        if (isOn)
        {
            targetHorizontalPosition = pageArray[1];
        }
    }

    public void MoveToPage3(bool isOn)
    {

        if (isOn)
        {
            targetHorizontalPosition = pageArray[2];
        }
    }

    public void MoveToPage4(bool isOn)
    {

        if (isOn)
        {
            targetHorizontalPosition = pageArray[3];
        }
    }



    //滑动进行时;
    public void OnDrag(PointerEventData eventData)
    {
      

    }
}

MyToggle

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class MyToggle : MonoBehaviour {
    //定义两个公开的持有引用
    public GameObject isOnGameObject;
    public GameObject isOffGameObject;
    
    //持有yinyong
    private Toggle toggle;

    void Start () {
        //获取组件
        toggle=this.GetComponent<Toggle>();
        //传递参数
        ToggleOnChange(toggle.isOn);
    }
    
    
    void Update () {
    
    }

    //注册事件
    public void ToggleOnChange(bool isOn){
        //通过传递参数判断是否隐藏;
        isOnGameObject.SetActive(isOn);
        isOffGameObject.SetActive(!isOn);

}
}

Player

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    public float speed = 100;

    void Start () {
    
    }
    

    void Update () {

        this.transform.Rotate(Vector3.up * Time.deltaTime * speed);
    }

    public void ChangeSpeed(float speedNew) {
        this.speed = speedNew;
    }
}

SkillItem

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class SkillItem : MonoBehaviour {

    //定义冷却时间
    public float coldTime = 2f;
    
    //定义计时器;
    private float timer = 0;//从零开始计时
    
    //定义持有的引用
    private Image filledImage;

    //计时标志位
    private bool isStartTimer = false;

    //设置快捷键
    public KeyCode keyCode;
    void Start () {
    
        //通过查找方法获取相应的组件
        filledImage=this.transform.Find("FilledImage").GetComponent<Image>();
    }
    

    void Update () {

        //设置快捷键
        if (Input.GetKeyDown(keyCode))
        {
            //按下快捷键 表示可以开始计时;
            isStartTimer = true;
        }

    
        //判断标志位 时候可以开始计时
        if (isStartTimer)
        {
            //计时器累加
            timer += Time.deltaTime;
            //控制关键组件的比例值
            filledImage.fillAmount=(coldTime-timer)/coldTime;
            //判断计时器大于等于冷却时间 归零
            if (timer>=coldTime)
            {
                //计时器归零;
                timer = 0;
                //标志位设置
                isStartTimer = false;
                //恢复到初始值;
                filledImage.fillAmount = 0;
            }
        }
        
    }

    public void OnClick() {
     //触发点击事件。设置标志位
        isStartTimer = true;
    }
}

上一篇 下一篇

猜你喜欢

热点阅读