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
元素辣么大~我只想来看看~ 资源哪里好,肯定元素找! {:1_144:}赞@ 加油! 资源甚好,发帖艰辛,且阅且珍惜! 感谢分享,有你真精彩 感谢分享,有你真精彩 好资源~!点赞 不错的资源,谢谢分享 元素帖子强,满满正能量 老铁666 十分感谢楼主分享!!! 谢谢分享 每天一学习 首发必发微元素,荣耀加身装备酷
资源甚好,发帖艰辛,且阅且珍惜! 不错 很有学习价值感谢. 路过看看 感谢分享 路过看看 感谢分享
页:
[1]
2