Unity2D游戏开发-Pak木鱼
在接下来文章里我会以Unity为主一起制作游戏
在unity 里如何制作一个简单的敲木鱼游戏?
创建一个2D场景(本人使用Unity2023)
(每个一段时间要申请一个个人许可证)
点击下方蓝色按钮创建
将以下素材拖动到Assets文件夹中
这张图随意命名我先叫他背景
单机左键,将该图的纹理改为Sprite(2D和UI)
在同层级中右键创建 C#脚本
右键重命名将名字改为 fishicommand
单击场景中的Main Camera (就是摄像机)将创建好的C#脚本添加(拖动到添加组件)
(注意:请检查你的摄像机又没有Audio Listener组件)
如果没有,点击添加组件并搜索Audio Listener添加
这样就行
双击在 Assets文件夹中 的C#脚本复制下面的代码并ctrl+S 保存
(别问我为啥不写协程)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;
public class fishicommand : MonoBehaviour
{
public GameObject e2;
public RectTransform uiElement;
public RectTransform uiElement1;
public Text text_;
public GameObject GameObject1;
public GameObject GameObject2;
public Vector3 vector3xyz = new Vector3(0, 0, 0);
public int fishgongde = 1;
public int biaoji = 1;
public bool textshangshen = false;
public float xyz_y = 750;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (e2 != null)
{
e2.transform.position = new Vector3(0, uiElement1.localPosition.y / 650, 0);
}
text_.text = fishgongde.ToString("000");
uiElement1.localPosition = new Vector3(255, xyz_y, 0);
if (Input.GetMouseButtonDown(0))
{
GameObject e1 = Instantiate(GameObject2.gameObject, vector3xyz, this.transform.rotation);
Destroy(e1, 1.25f);
if (!textshangshen) xyz_y = -1000;
fishgongde++; textshangshen = true;
uiElement.localPosition = new Vector3(0,-350, 0);
}
if (uiElement.localPosition.y < 0)
{
uiElement.localPosition = new Vector3(0, uiElement.localPosition.y + 400 * Time.deltaTime);
}
if (fishgongde % 15 == 0 && biaoji != fishgongde)
{
e2 = Instantiate(GameObject1.gameObject, vector3xyz, this.transform.rotation);
Destroy(e2, 5f);
biaoji = fishgongde;
}
if (textshangshen)
{
xyz_y += 1500 * Time.deltaTime;
if (xyz_y >= 300) textshangshen = false;
}
if (!textshangshen) xyz_y = 300;
}
}
接下来设置UI
创建一个UI图片随便命名这里命名为 Image 1
点击Canvas像我这样:设制
把屏幕缩放比例改成9:16(没有的画要自己添加)
改背景颜色(推荐改成黑色)
调整木鱼图标将缩放改为stretch 并选择保持长宽比并将数值清零
创建3个旧版text并这样设置(文本按名字设置并放在合适位置)
创建两个空对象将他们放入脚本
导入一个敲木鱼的声音并放咋空对项1中(要添加音频组件)
再制作一个特效为第二个添加(添加为子文件)随便做
制作成预制体塞进去(ps:塞反了改一下)
完工