有梦想的小鸟

                                                              有梦想的小鸟

  本系列讲解愤怒的小鸟,也详细的讲解一些基础的东西!

  我是在unity3d 3.5.6f4版本下操作的,以后的时间里,将会和大家一起慢慢的分享这个示例demo。如果有讲解的不当之处,请多多指教,并留言,共同学习。。。

  在愤怒的小鸟中,主要分为了界面与关卡,以及整个逻辑的实现。下面来分享一下加载界面UI与游戏的开始界面UI。整个的UI,都是通过unity3d中的gui实现,简单的去模仿愤怒小鸟中的ui格式。那先做一个StartingLevel场景,场景中建立一个空的gameobject,并命名为LevelManager,下面就开始分享第一个UI管理脚本,LevelManagerScript脚本。这个脚本时专门的逻辑性的管理每一个场景的加载。我们先做开始界面。开始界面时,总会有一个加载的界面来通知玩家处于加载,当加载完成后,才是开始界面。。。

先把代码方法写一下:

using UnityEngine;
using System.Collections;

public class LevelManagerScript : MonoBehaviour
{
    public float splashTime;
    public int currentLevel;
    public GUISkin gnomeGUISkin;
    private float startTime;
    public Texture loadingImage;
    public Texture playImage;

    // Use this for initialization
    void Start()
    {
        startTime = Time.time;
    }
    void Awake()
    {
        DontDestroyOnLoad(transform.gameObject);
    }

    // Update is called once per frame
    void Update()
    {
        if ((Application.loadedLevel == 0) && ((Time.time) - startTime > splashTime))
        {
            Application.LoadLevel(1);
            currentLevel = 1;
        }
    }

    void OnGUI()
    {
        int w = Screen.width;
        int h = Screen.height;

        if (null != gnomeGUISkin)
        {
            GUI.skin = gnomeGUISkin;
        }
        if (Application.loadedLevel == 0)
        {
            // Draw the loading screen splash
            GUI.DrawTexture(new Rect(0, 0, w + 1, h + 1), loadingImage, ScaleMode.ScaleAndCrop);
        }
     else if (Application.loadedLevel == 1)
      {
         // Draw the play screen splash
         GUI.DrawTexture(new Rect(0,0,w+1,h+1), playImage, ScaleMode.ScaleAndCrop);
      }

} }

DontDestroyOnLoad();当加载一个新关卡时,物体不被销毁,然后新关卡中的物体被加载进来。如果物体是一个组件或游戏物体,它的整个transform层次将不会被销毁,全部保留下来。

Application.LoadedLevel();加载关卡,参数可以是索引,可以是关卡的名字。所加载的关卡也是一个独立的场景。

下面是所加载的PlayScreen关卡场景的代码,脚本名称为PlayButtonScript。

using UnityEngine;
using System.Collections;

public class PlayButtonScript : MonoBehaviour {

    public Texture play;
    public GUISkin myGUISkin;

    void OnGUI()
    {
        GUI.skin = myGUISkin;

        int w = Screen.width;
        int h = Screen.height;
        if (GUI.Button(new Rect(w/2 - 100, h/2 + h/4, 200,100), play))
        {
            //加载下paly后的场景
        }
    }
}

整个的实现思路是,用了三张png格式的图片,一张是加载Loading的图片,一张是开始游戏GenericGnomeToss的背景图片,一张是按钮ButtonPlay的图片,(Time.time) - startTime > splashTime计算,让背景图片设置了显示的3秒的时间去当做加载的过程,随后就用Application.LoadedLevel();方法去加载出PlayScreen场景。

好了,对于愤怒的小鸟的开始讲解中的加载与开始就到这了。。。希望多多提出意见,敬请留言,共同学习。。。。

如果有问题也请QQ留言:1029633680

 

 

 

 

 

 

 

posted @ 2013-04-18 01:33  广州阿龙  阅读(497)  评论(0编辑  收藏  举报