昨天我們一起學習了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; } }
運行效果:
或者效果為連續的。
希望大家能共同學習!