教练 发表于 2016-12-30 11:52:53

Unity3D 像素风格《猫捕鱼》小游戏(一)

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


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

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




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

    相关用到的动画类文件:
    ①CatAnimationController.cs 代码如下:
using UnityEngine;
using System.Collections;

public class CatAnimationController : MonoBehaviour
{
         
         
      public SpriteAnimationController leftWalk = null;
      public SpriteAnimationController rightWalk = null;
      public SpriteAnimationController upWalk = null;
      public SpriteAnimationController failed = null;
      public Flashing flash;
      
      public Vector3 direction;
         
      public void MoveRight ()
      {
                rightWalk.enabled = true;
                direction = Vector3.right;
      }
         
      public void MoveLeft ()
      {
                leftWalk.enabled = true;
                direction = Vector3.left;
      }
         
      public void LookUp ()
      {
                upWalk.enabled = true;
                direction = Vector3.up;
      }
         
      public IEnumerator Flashing ()
      {
                flash.enabled = true;

                yield return new WaitForSeconds(1.5f);

                flash.enabled = false;
                  
                transform.parent.GetComponent<Collider>().enabled = true;
      }

         

}

    ②SpriteAnimationController.cs?代码如下:
using UnityEngine;
using System.Collections;

public class SpriteAnimationController : MonoBehaviour
{
         
         
      
      public float time = 0.3f;
      
      string format = string.Empty;
      private int spriteNo = 0;
      private Texture[] sprite = null;
         
      void Awake ()
      {
                sprite = new Texture;
                for (int i=0; i<2; i++) {
                        string spriteName = string.Format ("Texture/" + format, i + 1);
                        sprite= Resources.Load (spriteName, typeof(Texture)) as Texture;
                }
      }

      void OnEnable ()
      {
                SpriteAnimationController[] animations = GetComponents<SpriteAnimationController> ();
                foreach (SpriteAnimationController anim in animations) {
                        if (anim != this)
                              anim.enabled = false;
                }
                  
                StartCoroutine (UpdateSprite ());
      }
         
      void OnDisable ()
      {
                StopAllCoroutines ();
      }
         
      IEnumerator UpdateSprite ()
      {
                while (true) {
            GetComponent<Renderer>().material.mainTexture = sprite ;
                        spriteNo = (spriteNo == 0) ? 1 : 0;
                        
                        yield return new WaitForSeconds(time);
                }
      }
}


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

public class TitleManager : MonoSingleton<TitleManager>
{
      
      
      Flashing flash;

      // Update is called once per frame
      void Update ()
      {
               
                if (Input.GetButtonDown ("Fire1")) {
                        StartCoroutine (GoPrologue ());
                        enabled = false;
                }
      }
      
      IEnumerator GoPrologue ()
      {
                GetComponent<PlaySound> ().PlayOneShot ();
               
                flash.enabled = false;
                flash.offTime = 0.1f;
                flash.onTime = 0.1f;

                flash.enabled = true;
               
                yield return new WaitForSeconds(1f);
               
                Application.LoadLevel ("Prologue");
      }
}


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

public class Flashing : MonoBehaviour
{
      
      
      public float onTime = 0.7f;
      
      public float offTime = 0.3f;
      
      void OnEnable ()
      {
                StartCoroutine (Flash ());
      }
      
      void OnDisable ()
      {
                StopAllCoroutines ();
      GetComponent<Renderer>().enabled = true;
      }
      
      // Use this for initialization
      IEnumerator Flash ()
      {
      
                while (true) {
            GetComponent<Renderer>().enabled = true;
                        yield return new WaitForSeconds(onTime);

            GetComponent<Renderer>().enabled = false;
                        yield return new WaitForSeconds(offTime);
                }
      }
      
}

      

    4.Prolgue.unity 过渡场景,并且场景中附加PrologueManager.cs 组件;
       PrologueManager.cs 代码如下:
using UnityEngine;
using System.Collections;

public class PrologueManager : MonoSingleton<PrologueManager>
{
      IEnumerator Start ()
      {
                yield return new WaitForSeconds(4.5f);
                Application.LoadLevel ("Game");
      }
}   

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

   

    好吧!因为项目信息有些多,所以我分为两篇描述,也许讲解不是很清楚,希望大家可以多多包涵。需要资源的话,直接加群:575561285

包子包子 发表于 2016-12-30 12:04:46

元素辣么大~我只想来看看~

苦行者 发表于 2016-12-30 15:50:37

资源哪里好,肯定元素找!

狼之独步 发表于 2016-12-30 15:57:24

{:1_144:}赞@ 加油!

mnsdzpz 发表于 2018-10-22 11:25:38

资源甚好,发帖艰辛,且阅且珍惜!

亊鈀菈 发表于 2018-10-22 11:37:34

感谢分享,有你真精彩

亊鈀菈 发表于 2018-10-22 11:37:38

感谢分享,有你真精彩

张前进 发表于 2018-10-23 09:38:55

好资源~!点赞

madmonkey 发表于 2018-10-23 09:50:21

不错的资源,谢谢分享

experiencc 发表于 2018-10-23 09:53:58

元素帖子强,满满正能量

ゞYx。 发表于 2018-10-24 08:48:52

老铁666

behindjj 发表于 2018-10-24 09:01:53

十分感谢楼主分享!!!

Abstract 发表于 2018-10-24 09:12:28

谢谢分享

tim_chen 发表于 2018-10-30 11:09:15

每天一学习

路过了 发表于 2018-10-30 11:42:34

首发必发微元素,荣耀加身装备酷

ShiinaMashiro 发表于 2018-10-30 11:49:25


资源甚好,发帖艰辛,且阅且珍惜!

qq_木子李_H0H 发表于 2019-2-20 10:10:04

不错

qq_浮云_RsB 发表于 2019-2-27 15:00:38

很有学习价值感谢.

有点淡定 发表于 2019-5-27 21:09:03

路过看看 感谢分享

有点淡定 发表于 2019-5-27 21:09:04

路过看看 感谢分享
页: [1] 2
查看完整版本: Unity3D 像素风格《猫捕鱼》小游戏(一)