代码改变世界

Unity 进度条3D制作(3D版)

2014-05-18 14:05  wuzhang  阅读(2573)  评论(3编辑  收藏  举报

昨天我们一起学习了2D进度跳的制作,那么趁着我们脑海中还残存昨日的记忆,今天继续学习另一种方法:

实现思路:当鼠标悬浮Start按钮->实例化物体并显示进度->100/100->进入游戏场景;

       鼠标离开按钮进度还原为 0/100

       直接点击Start按钮也可完成。

1,首先我们导入NGUI资源包,在当前工程的场景下创建UI。

    

2,在Herarchy下添加平行光,在Panel下添Button。

   

 此处Background没有选择背景图片,即背景色为默认值,此处label设置字体显示Start,颜色红色。

   

3,另外还需在Panel下添加一个Label用于显示进度的百分比,我命名为process_lab.

  

4, 万事俱备,只欠脚本了。

   OverButton.cs 是用于检测鼠标是否悬浮于Start上.

   如果真的悬浮于Start上那么我们可以定义一bool值IsOnButton并且置为true,否则IsOnButton=false;

  

using UnityEngine;
using System.Collections;



public class OverButton : MonoBehaviour 
{

    public static bool IsOnButton;

    // Use this for initialization
    void Start () 
    {
        IsOnButton=false;
    }


    void OnHover(bool isOver)
    {
        if(isOver)
        {
            IsOnButton=true;
            Debug.Log (IsOnButton);
        }
        else
        {
            IsOnButton=false;
            Debug.Log (IsOnButton);
        }
    }


    // Update is called once per frame
    void Update () 
    {
    
    }

}

5,检测悬浮我们已搞定,接下来就是绘制进度条形状了。

 DrowCircle.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class DrowCircle : MonoBehaviour 
{

    public UIButton button;
    public UILabel pro_label;
    public GameObject prb_ball;
    public List<GameObject>ball;
    public Vector2 vect2;
    string str="加载游戏:";
    private static int  _proNum;

    // Use this for initialization
    void Start ()
    {
        _proNum=0;
        ball=new List<GameObject>();
        InvokeRepeating("Circle",0f,0.167f);
    }


    public void Circle()
    {
        if(OverButton.IsOnButton)
        {
            if(ball.Count<=20)
            {
                _proNum=ball.Count;
                GameObject tmp_ball=Instantiate(prb_ball) as GameObject;
                tmp_ball.transform.localPosition=new Vector3(button.transform.localPosition.x+0.5f,button.transform.localPosition.y,0);
                tmp_ball.transform.RotateAround(button.transform.localPosition,Vector3.forward,ball.Count*(-18));
                ball.Add(tmp_ball);
                ;
            }
        }
        else
        {
            foreach(var obj in ball)
            {
                Destroy(obj);
            }
            ball.Clear();
            _proNum=0;
        }
    }


    // Update is called once per frame
    void Update () 
    {
    
    }

    /// <summary>
    /// 开始按钮点击
    /// </summary>
    void OnClick()
    {
        //Application.LoadLevel("GameSence");
    }
    

    void OnGUI()
    {
        pro_label.text=str+(_proNum*5).ToString()+"/100";
    }

}

6,脚本写完了,如何绑定呢?鼠标悬浮按钮上那么肯定是将OverButton.cs 添加到Start上了。

    我把另一脚本DrowCircle.cs也添加到了Start按钮上了,绑定如下:

    

   因为我想绘制一个以Start按钮为中心的球形3D图案,还需以Sphere预设,自行创建即可。

7,最后实现点击Button事件需要添加脚本Buttonmessage.cs,上截图最下端可以看到Target 设为Start按钮Funcation Name 为OnClick.

    最终我们该看到自己的劳动成果了,实现效果:

   

  

  

  
鼠标悬浮按钮前和离开效果:

  

第二种效果:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class DrawLine : MonoBehaviour
{
    private List<GameObject>line;
    public GameObject prbCube;
    public Vector2 vect2;
    public static int tmp_x;
    public UILabel label1;
    private string str;
    private int tmp_num;
    
    // Use this for initialization
    void Start () 
    {
        tmp_x=0;
        tmp_num=0;
        str="加载游戏:";
        line=new List<GameObject>();
        InvokeRepeating("CreateLine",0,0.167f);
    }
    
    
    // Update is called once per frame
    void Update () 
    {
        
    }


    /// <summary>
    /// 进度条
    /// </summary>
    void CreateLine()
    {
        if(OverButton.IsOnButton)
        {
            if(line.Count<=20)
            {
                tmp_num=line.Count;
                GameObject tmp=Instantiate(prbCube)as GameObject;
                tmp.transform.localPosition=new Vector3(((float)line.Count/10-2f)+1.2f,0.8f,0);
                line.Add(tmp);
            }
        }
        else
        {
            foreach(GameObject i in line)
            {
                Destroy(i);
            }
            tmp_num=0;
            line.Clear();
        }
    }
    
    
    /// <summary>
    /// 显示进度
    /// </summary>
    void OnGUI()
    {
        label1.text=str+(tmp_num*5).ToString()+"/100";
        label1.color=Color.yellow;
    }
}

运行效果:

  

  

  或者效果为连续的。

  

  

希望大家能共同学习!