12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 2450|回复: 14
打印 上一主题 下一主题

使用unity3d+免费扩展工具开发2D游戏_第四节

[复制链接]

3795

主题

2

听众

5万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
53202
精华
32

活跃会员 优秀版主 推广达人 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2011-11-21 13:52:00 |只看该作者 |倒序浏览

          This is part 4 of our tutorial series on making a 2D game in unity3d with freely available tools and plugins. Part 1introduced you to the tools we’re using, in Part 2 we built our first level and in Part 3 we hooked up the control scripts and setup the ladders and ropes so that the player can navigate levels. In this article we’re going to create a pickup item, hook up the scoring system and add the interface text which reports score, lives and level.
         
           Hopefully you already went through part 2 and part 3 of the series, if you haven’t already you might want to go back and do them now so everything in this part will make sense.
         

           If you’d rather skip ahead, you can download the project up to this point. You can alsoclick here to play the game as it will be at the end of Part 4.
         

           Adding The Scoring and Pickup Scripts
         

           The game is working great so far – the player can move, climb ladders and shimmy across ropes. Now we’re going to add a “gold chest” pickup item and an interface to track scoring, player lives and the current level.
         


            Download scoring and pickup scripts and unzip the file somewhere on your hard drive.
           

            Copy
            
             Scoring.cs
            
             and
            
             Pickup.cs
            
             from the extracted .zip file and paste them into your project’s
            
             Assets/Scripts
            
             folder.
           


           Modifications To Existing Scripts:
         

           You’re going to need to make a few changes to the scripts from part 3 so that these new scripts will work.
         

           Open xa.cs script and then uncomment the first line in the Class:
         

           public static Scoring sc;
         

           In the Start function, uncomment this line:
         

           sc = (Scoring)(this.gameObject.GetComponent("Scoring"));
         

           Open Player.cs, find the OnTriggerEnter function (line 256) and then uncomment the following code block:
         

           if (other.gameObject.CompareTag("ickup"))
         

           {
         

               if (other.GetComponent<ickup>())
         

               {
         

                   other.GetComponent<ickup>().PickMeUp();
         

                   xa.sc.Pickup();
         

               }
         

           }
         
           
         
           Making The Pickup Sprite
         

           You might remember from part 2, that you already added the
           
            pickup.png
           
            to the
           
            level
           
           sprite atlas and then created a
           
            level
           
            container object that points to the sprite atlas so we don’t need to do any additional Container setup to create the pickup.
         


            Drag & drop the Sprite object from
            
             Orthello –> Objects –> Sprites
            
             into the Hierarchy or Scene which will create a new object named something like “Sprite (id=-3700)“. Rename that object to “
            
             pickup
            
            “.
           

            Drag the level object that we created in part 2 from
            
             OT –> Containers
            
             and drop it on to the
            
             Sprite Container
            
             slot in the Inspector.
           


           Your sprite will appear but it looks like the brick sprite that we made before, that’s because the brick is the first texture on the Sprite Atlases index.
         


            Click and hold your mouse over the word “
            
             Frame Index
            
            ” in the inspector (with the pickup object selected) and then drag the mouse to the right to scroll through the textures on the sprite atlas. The pickup texture is at index
            
             16
            
             so set it to that. The sprite should now look like a a white rectangle with a red box in the center and a black background.
           


           Adjusting The Collision:
         

           The sprite is a square, but we only want the object to be picked up only when the player touches the white part of the pickup.
         


            Check the
            
             Collidable
            
             checkbox to add collision to the object.
           

            Open up the drop down list next to
            
             Physics
            
             and then select
            
             Custom
            
            .
           

            Under
            
             Box Collider
            
            , set the
            
             Center Y
            
             to
            
             -0.15
            
            ,
            
             Size X
            
             to
            
             0.8
            
             and
            
             Size Y
            
             to
            
             0.5
            
            . Leave
            
             Size Z
            
             set to
            
             0.4
            
            .
           


           Setting The Tag:
         

           We need to tag the pickup so that the player will trigger it when touched.
         


            Create a new tag by going to
            
             Edit –> Project Settings –> Tags
            


            Add a new
            
             Tag
            
             in the Tags list (probably in Element 3 if you’ve been following the series) named “
            
             Pickup
            
            ” (without the quotes).
            
             It’s important that the name is correct since Player.cs looks for this exact name.
            


            Select the pickup object in the Hierarchy, then click on the drop down list next to
            
             Tags
            
             at the top of the Inspector and then select
            
             Pickup
            
            .
           


           Add The Script and Make It A Prefab
         

           Now we just need to add the
           
            Pickup.cs
           
            script to the sprite so that it will get “picked up” when the player touches it and we also need to turn it into a prefab so that it’s easy to place in all your levels.
         


            Drag the
            
             Pickup.cs
            
             script from the Project Scripts folder and drop it on to the
            
             pickup
            
            sprite in the Hierarchy.
           

            Drag the
            
             pickup
            
             object from the Hierarchy into the
            
             Prefabs
            
             folder in the Project tab to create prefab from the object.
           


           The pickup is designed so that you can snap the bottom edge (the black part) of the sprite to the top edge of a brick sprite the white part of the powerup will be the correct height from the top of the brick. Duplicate your new pickup prefab a few times and place it around you level.
         

           If you followed all of the steps so far, your pickup should look like this:
           

            
         




           Add The Interface Text
         

           At this point if you place the pickup in your level so that the player can pick it up, the sprite will disappear correctly as if he picked it up, but you’ll get some errors in the console because the Scoring script isn’t in the scene yet and we don’t have any UI to report the score. So let’s fix that.
         


            First add the
            
             Scoring.cs
            
             script to the scene: Drag
            
             Scoring.cs
            
             from your Project’s Scripts folder and drop it on to the
            
             Scripts
            
             object in the Hierarchy. If you select the
            
             Scripts
            
             object, you’ll see that the
            
             Scoring
            
             script has a bunch of slots for text objects that we need to add – but first we need to create the text objects.
           

            Download the G7 Silkworm font and unzip the file somewhere on your computer. This font isn’t exactly the same as the one used in the original Lode Runner game, but it’s pretty close. If there’s another font you’d rather use, go for it. And if you find a font that’s closer to the Lode Runner font, please let me know in the comments.
           

            Create a new folder in your
            
             Assets
            
             folder named
            
             Fonts
            
            , locate the
            
             silkworm.TTF
            
             font on your hard drive and then copy it into the
            
             Assets/Fonts
            
             folder.
           

            In Unity, select the
            
             silkworm
            
             font in your Project’s Fonts folder and then in the Inspector set the
            
             Font Size
            
             to
            
             26
            
            .
           


           Adding The Score Text:
         


            Drag the
            
             silkworm
            
             font from the
            
             Fonts
            
             folder in the Project panel and drop it into the Hierarchy. This will automatically create a new
            
             GUI Text
            
             object in the Hierarchy using the
            
             silkworm
            
             font.
           

            Rename the object to “
            
             Score Label
            
            “
           

            In the
            
             Text
            
             field under GUIText in the Inspector, type “
            
             SCORE
            
            ” in all caps without the quotes. The Silkworm font only has uppercase letters so don’t try to use lowercase letters.
           

            When you add a font to the scene, the X and Y position are automatically set to X 0.5 and Y 0.5, but we’re going to set the position of our fonts using the Pixel Offset so reset the
            
             X
            
             and
            
             Y Position
            
             to
            
             0
            
            .
           

            Change
            
             Pixel Offset X
            
             to
            
             16
            
             and
            
             Pixel Offset Y
            
             to
            
             592
            
            . This should position the
            
             SCORE
            
             text to the upper left hand corner of the Game view (note you won’t see the text in the Scene view so look at the Game view to help position the text).
           


           We also want to keep the scene nicely organized and make it a little easier to hook up the interface objects to the Scoring script later on. You’re going to make the Score Label object a child of the Scripts object and then you’ll duplicate each of the other text objects from this one in order to save a few of the setup steps.
         


            Select the
            
             Score Label
            
             object in the Hierarchy and then drag and drop it onto the
            
             Scripts
            
             object in the Hierarchy so that it becomes a child of the Scripts object.
           

            Expand the little arrow next to the
            
             Scripts
            
             object, select
            
             Score Label
            
             object and then duplicate it.
           

            Rename the new text object to “
            
             Score Value
            
            “.
           

            Change the
            
             Text
            
             to
            
             0000000
            
             (7 zeroes).
           

            Change the
            
             Pixel Offset X
            
             to
            
             150
            
             and leave the
            
             Y
            
             set to
            
             592
            
            .
           


           Adding The Lives Text:
         


            Select either the Score Label or the Score Value text object and duplicate it.
           

            Rename the new text object to “
            
             Lives Label
            
            “
           

            Change the Text to
            
             LIVES
            
            .
           

            Change the
            
             Pixel Offset X
            
             to
            
             348
            
             and leave
            
             Y
            
             set to
            
             592
            
            .
           

            Duplicate
            
             Lives Label
            
             object and rename it to “
            
             Lives Value
            
            “.
           

            Change the
            
             Text
            
             to
            
             000
            
             (3 zeroes).
           

            Change the
            
             Pixel Offset X
            
             to
            
             480
            
             and leave
            
             Y
            
             set to
            
             592
            
            .
           


           Adding The Level Text:
           

            
         


            Select one of the other text objects you already created and duplicate it.
           

            Rename the new text object to “
            
             Level Label
            
            “
           

            Change the
            
             Text
            
             to
            
             LEVEL
            
            .
           

            Change the
            
             Pixel Offset X
            
             to
            
             576
            
             and leave
            
             Y
            
             set to
            
             592
            
            .
           

            Duplicate
            
             Level Label
            
             and rename the new object to “
            
             Level Value
            
            “
           

            Change the
            
             Text
            
             to
            
             000
            
             (3 zeroes).
           

            Change the
            
             Pixel Offset X
            
             to
            
             710
            
             and leave
            
             Y
            
             set to
            
             592
            
            .
           


           Hooking Up The Scripts:
         



           Now we need to connect the interface text objects to the Scoring script.
         


            Select the
            
             Scripts
            
             object in the Hierarchy. Under the Scoring script in the Inspector, you’ll see slots for each of the GUIText objects we just created.
           

            Select each of the text objects from the Hierarchy and drag and drop them into the corresponding slot on the Scoring script. For example, drag
            
             Level Label
            
             and drop it on to the
            
             Level Label Text
            
             field.
           


           Once all of the text objects are connected to the
           
            Scoring
           
            script, press
           
            Play
           
            in Unity. You should see the SCORE, LIVES and LEVEL text turn to a red color that matches the border color and the number of lives should change to 005 and the level number should change to 001.
           

            
         

           Now if you run the character over a pickup object in the game, the Score should increase by 250.
           

            
         

           Make The Top Border
         



           To help keep the interface text separate from the gameplay area, we’re going to add a thin border just under the interface text.
           

            
         


            Create a new cube by going to
            
             Game Object –> Create Other –> Cube
            


            Rename the object to “
            
             border top
            
            “.
           

            Set the
            
             Transform Position X
            
             to
            
             0
            
            ,
            
             Y
            
            to
            
             8.7
            
             and
            
             Z
            
             to
            
             1
            
            .
           

            Set the
            
             Scale X
            
             to
            
             26
            
            ,
            
             Y
            
             to
            
             0.2
            
             and
            
             Z
            
             to
            
             1
            
            .
           

            Find the
            
             border
            
             material in the Materials folder and then drag and drop it on to the
            
             border top
            
             object. We created the border material in a previous article.
            

            
           


           If you followed all of the steps above, then your Hierarchy and Inspector (with the Scripts object selected) should look the following:
           

            
         




            
         

           Creating The GLOBAL Prefab
         



           Next we want to do some things to keep the scene nicely organized and also to make it easy to add the key components to any new levels that you create. We’re going to create a GLOBAL prefab that contains the Main Camera, the Scripts (which includes the interface text objects) and the top and bottom borders.
         


            Create an empty game object by boing to
            
             Object –> Create Empty
            
            , zero out the transforms so that it’s at 0,0,0 on the X,Y,Z and then rename the object to “
            
             GLOBAL
            
            “.
           

            Select the
            
             Main Camera
            
             in the Hierarchy and then drag and drop it onto the
            
             GLOBAL
            
            object so that the Main Camera becomes a child of GLOBAL.
           

            Select the
            
             Scripts
            
             object in the Hierarchy and drag and drop it onto the
            
             GLOBAL
            
            object.
           

            Select the
            
             border bottom
            
             and
            
             border top
            
             objects (make sure they’re not already a child of some other game object first) and drag and drop them onto the
            
             GLOBAL
            
            object.
           


           That’s everything we need for the GLOBAL object, so now let’s turn it into a prefab that can be easily added to other levels and updated later if we need to make changes.
           

            
         


            Select the
            
             GLOBAL
            
             object in the Hierarchy and then drag and drop it onto the
            
             Prefabs
            
            folder in the Project panel.
            

            
           

            Creating The Player Prefab
           



            In Part 3 of the series, I forgot to create a prefab from our player character, so let’s do that now if you haven’t already.
            

            
           


             Select the
            
              player
            
              object in the Hierarchy and zero his Transform position X to 0, Y to 0 and leave the Z position set to -1. This step is optional, I just like to have my game objects centered when I initially create a prefab.
            

              
            


            If you’ve followed all of the steps so far, then your Project and scene Hierarchy should look like the following image:
            






            
           

            Conclusion
           



            We’re getting a lot closer to having a complete game. Over the course of the next few tutorials I hope to add enemy AI (still working on a good solution for them), breakable bricks, a front end interface, an exit ladder (for exiting levels), some kind of manager for keeping track of your current level and a few other things like a game over screen. Let me know in the comments if there are other things you’d like to see covered.
            

            
           

            If you followed all of the steps so far, then your game should look something like this image (click to see a larger version):
            

            
           

            
           






            You can download the project up to this point and you can play the web version of the project here.
            



            If you like this post, please be sure to say hi in the comments and follow me on Twitterand Facebook. Your support helps to keep these tutorial coming. This blog post is part of iDevBlogADay, a collection of indie developers writing about their development experiences.
            



            转自:http://www.rocket5studios.com
            

            
           

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-1-23 23:31:16 |只看该作者
新年快乐,龙马精神,万事如意,百尺竿头,恭喜发财,年年有余。
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2012-1-26 23:20:01 |只看该作者
新年的风,吹散你的阴霾;新年的雨,冲刷你的无奈;新年的雾,锁定你的失败;新年的日,照耀你的期待。新年到来,幸福怀揣,新年快乐,响彻天外!
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-2-10 23:24:22 |只看该作者
百度的叫度娘,网易的叫易娘,新浪内部还在为是叫新娘还是浪娘而争论不休!……不管你们是企鹅的额娘,豆瓣的伴娘,还是华为的伪娘,都要记得,淘宝才是你们的亲娘啊!亲!!
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-3-2 23:21:49 |只看该作者
我是老实人,我来也!
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

6#
发表于 2012-3-24 23:21:49 |只看该作者
真是不错啊
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-6-4 23:25:04 |只看该作者
心中有爱,爱咋咋地
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2012-8-12 00:29:18 |只看该作者
好`我顶``顶顶
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

9#
发表于 2012-8-22 00:22:23 |只看该作者
无聊时可以刷屏幕 灌水 也可以试试 帖子的标题究竟可以写多长
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

10#
发表于 2012-8-22 23:50:22 |只看该作者
谢谢楼主,真是太实用了
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-6-1 14:13 , Processed in 0.107119 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部