element3ds.com
原创Unity3D资源 Unity3D 像素风格《猫捕鱼》小游戏(一)
发布于
2016-12-30
1620
31

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 幺九 于 2019-5-24 10:56 编辑


     今天为大家分享一下,如何在unity3d 中写一个像素级的射击类游戏。
     
    好吧!废话不多讲,我直接上图操作!

    1.新建一个unity3d 项目,导入需要的图片、声音、动画等资源文件(PS:素材都是从网上DownLoad下来的);
1_0.png

1_1.png

    2.根据需要的图片,制作对应的动画文件,例如鱼的动画,猫的动画等;

    相关用到的动画类文件:
    ①CatAnimationController.cs 代码如下:
  1. using UnityEngine;
  2. using System.Collections;
  3.   
  4. public class CatAnimationController : MonoBehaviour
  5. {
  6.          
  7.          
  8.         public SpriteAnimationController leftWalk = null;
  9.         public SpriteAnimationController rightWalk = null;
  10.         public SpriteAnimationController upWalk = null;
  11.         public SpriteAnimationController failed = null;
  12.         public Flashing flash;
  13.         [HideInInspector]
  14.         public Vector3 direction;
  15.          
  16.         public void MoveRight ()
  17.         {
  18.                 rightWalk.enabled = true;
  19.                 direction = Vector3.right;
  20.         }
  21.          
  22.         public void MoveLeft ()
  23.         {
  24.                 leftWalk.enabled = true;
  25.                 direction = Vector3.left;
  26.         }
  27.          
  28.         public void LookUp ()
  29.         {
  30.                 upWalk.enabled = true;
  31.                 direction = Vector3.up;
  32.         }
  33.          
  34.         public IEnumerator Flashing ()
  35.         {
  36.                 flash.enabled = true;
  37.   
  38.                 yield return new WaitForSeconds(1.5f);
  39.   
  40.                 flash.enabled = false;
  41.                   
  42.                 transform.parent.GetComponent<Collider>().enabled = true;
  43.         }
  44.   
  45.          
  46.   
  47. }
点击此处复制文本


    ②SpriteAnimationController.cs?代码如下:
  1. using UnityEngine;
  2. using System.Collections;
  3.   
  4. public class SpriteAnimationController : MonoBehaviour
  5. {
  6.          
  7.          
  8.         [Range(0.1f, 1f)]
  9.         public float time = 0.3f;
  10.         [SerializeField]
  11.         string format = string.Empty;
  12.         private int spriteNo = 0;
  13.         private Texture[] sprite = null;
  14.          
  15.         void Awake ()
  16.         {
  17.                 sprite = new Texture[2];
  18.                 for (int i=0; i<2; i++) {
  19.                         string spriteName = string.Format ("Texture/" + format, i + 1);
  20.                         sprite  = Resources.Load (spriteName, typeof(Texture)) as Texture;
  21.                 }
  22.         }
  23.   
  24.         void OnEnable ()
  25.         {
  26.                 SpriteAnimationController[] animations = GetComponents<SpriteAnimationController> ();
  27.                 foreach (SpriteAnimationController anim in animations) {
  28.                         if (anim != this)
  29.                                 anim.enabled = false;
  30.                 }
  31.                   
  32.                 StartCoroutine (UpdateSprite ());
  33.         }
  34.          
  35.         void OnDisable ()
  36.         {
  37.                 StopAllCoroutines ();
  38.         }
  39.          
  40.         IEnumerator UpdateSprite ()
  41.         {
  42.                 while (true) {
  43.             GetComponent<Renderer>().material.mainTexture = sprite [spriteNo];
  44.                         spriteNo = (spriteNo == 0) ? 1 : 0;
  45.                           
  46.                         yield return new WaitForSeconds(time);
  47.                 }
  48.         }
  49. }
点击此处复制文本



    3.新建Title.unity 开始场景,场景中新建一个Manager对象,附加TitleManager.cs组件,并且标题的闪烁功能Flashing.cs;
      ①TitleManager.cs代码如下:
  1. using UnityEngine;
  2. using System.Collections;

  3. public class TitleManager : MonoSingleton<TitleManager>
  4. {
  5.         
  6.         [SerializeField]
  7.         Flashing flash;

  8.         // Update is called once per frame
  9.         void Update ()
  10.         {
  11.                
  12.                 if (Input.GetButtonDown ("Fire1")) {
  13.                         StartCoroutine (GoPrologue ());
  14.                         enabled = false;
  15.                 }
  16.         }
  17.         
  18.         IEnumerator GoPrologue ()
  19.         {
  20.                 GetComponent<PlaySound> ().PlayOneShot ();
  21.                
  22.                 flash.enabled = false;
  23.                 flash.offTime = 0.1f;
  24.                 flash.onTime = 0.1f;

  25.                 flash.enabled = true;
  26.                
  27.                 yield return new WaitForSeconds(1f);
  28.                
  29.                 Application.LoadLevel ("Prologue");
  30.         }
  31. }
点击此处复制文本


      
      ②Flashing.cs代码如下:
  1. using UnityEngine;
  2. using System.Collections;

  3. public class Flashing : MonoBehaviour
  4. {
  5.         
  6.         [Range(0.01f, 1f)]
  7.         public float onTime = 0.7f;
  8.         [Range(0.01f, 1f)]
  9.         public float offTime = 0.3f;
  10.         
  11.         void OnEnable ()
  12.         {
  13.                 StartCoroutine (Flash ());
  14.         }
  15.         
  16.         void OnDisable ()
  17.         {
  18.                 StopAllCoroutines ();
  19.         GetComponent<Renderer>().enabled = true;
  20.         }
  21.         
  22.         // Use this for initialization
  23.         IEnumerator Flash ()
  24.         {
  25.         
  26.                 while (true) {
  27.             GetComponent<Renderer>().enabled = true;
  28.                         yield return new WaitForSeconds(onTime);

  29.             GetComponent<Renderer>().enabled = false;
  30.                         yield return new WaitForSeconds(offTime);
  31.                 }
  32.         }
  33.         
  34. }

点击此处复制文本
     

    4.Prolgue.unity 过渡场景,并且场景中附加PrologueManager.cs 组件;
       PrologueManager.cs 代码如下:
  1. using UnityEngine;
  2. using System.Collections;
  3.   
  4. public class PrologueManager : MonoSingleton<PrologueManager>
  5. {
  6.         IEnumerator Start ()
  7.         {
  8.                 yield return new WaitForSeconds(4.5f);
  9.                 Application.LoadLevel ("Game");
  10.         }
  11. }
点击此处复制文本
   

    5.接下来,我们需要做一些准备工作,制作需要的Prefab,例如鱼、子弹、猫等;

   

    好吧!因为项目信息有些多,所以我分为两篇描述,也许讲解不是很清楚,希望大家可以多多包涵。需要资源的话,直接加群:575561285
参与人数 3 元素币 +50 活跃度 +41 贡献值 +1
还没有设置签名!您可以在此展示你的链接,或者个人主页!

使用道具 举报 登录

回复 <
包子包子  发表于 2016-12-30 12:04:46  
2#
元素辣么大~我只想来看看~
回复 收起回复
使用道具
苦行者  发表于 2016-12-30 15:50:37  
3#
资源哪里好,肯定元素找!
回复 收起回复
使用道具
狼之独步  发表于 2016-12-30 15:57:24  
4#
{:1_144:}赞@ 加油!
回复 收起回复
使用道具
mnsdzpz  发表于 2018-10-22 11:25:38  
5#
资源甚好,发帖艰辛,且阅且珍惜!
回复 收起回复
使用道具
亊鈀菈  发表于 2018-10-22 11:37:34  
6#
感谢分享,有你真精彩
回复 收起回复
使用道具
亊鈀菈  发表于 2018-10-22 11:37:38  
7#
感谢分享,有你真精彩
回复 收起回复
使用道具
张前进  发表于 2018-10-23 09:38:55  
8#
好资源~!点赞
回复 收起回复
使用道具
madmonkey  发表于 2018-10-23 09:50:21  
9#
不错的资源,谢谢分享
回复 收起回复
使用道具
experiencc  发表于 2018-10-23 09:53:58  
10#
元素帖子强,满满正能量
回复 收起回复
使用道具
ゞYx。  发表于 2018-10-24 08:48:52  
11#
老铁666
回复 收起回复
使用道具
behindjj  发表于 2018-10-24 09:01:53  
12#
十分感谢楼主分享!!!
回复 收起回复
使用道具
Abstract  发表于 2018-10-24 09:12:28  
13#
谢谢分享
回复 收起回复
使用道具
tim_chen  发表于 2018-10-30 11:09:15  
14#
每天一学习
回复 收起回复
使用道具
路过了  发表于 2018-10-30 11:42:34  
15#
首发必发微元素,荣耀加身装备酷
回复 收起回复
使用道具
ShiinaMashiro  发表于 2018-10-30 11:49:25  
16#

资源甚好,发帖艰辛,且阅且珍惜!
回复 收起回复
使用道具
qq_木子李_H0H  发表于 2019-2-20 10:10:04  
17#
不错
回复 收起回复
使用道具
qq_浮云_RsB  发表于 2019-2-27 15:00:38  
18#
很有学习价值感谢.
回复 收起回复
使用道具
有点淡定  发表于 2019-5-27 21:09:03  
19#
路过看看 感谢分享
回复 收起回复
使用道具
有点淡定  发表于 2019-5-27 21:09:04  
20#
路过看看 感谢分享
回复 收起回复
使用道具
12下一页

快来发表你宝贵的意见吧!

教练 实名

通过了实名认证的内容创造者

  

主题
18
精华
0
超神
0
扩散
0
微金
153
智慧
57
余额
1
在线时间
30 小时

长枪

快速回复 返回顶部 返回列表