如何用最短的时间做一个最小的游戏?
总体步骤先介绍一下:一、安装Unity+Visual Studio,我花了两天时间,因为总是安装出错;二、按视频步骤,在Unity中增加“游戏角色game object”、给角色增加“组件component”、增加“代码script”。 游戏角色有4个:1、旋转中心,新建一个"圆knob“,它是一个半透明的圆。 2、player,新建一个“圆knob”角色,是一个白色的小圆,玩家控制它来躲避六边形
·

总体步骤先介绍一下:
一、安装Unity+Visual Studio,我花了两天时间,因为总是安装出错;
二、按视频步骤,在Unity中增加“游戏角色game object”、给角色增加“组件component”、增加“代码script”。 游戏角色有4个:
1、旋转中心,新建一个"圆knob“,它是一个半透明的圆。 2、player,新建一个“圆knob”角色,是一个白色的小圆,玩家控制它来躲避六边形。用script给它加上“玩家控制旋转”的功能。 3、hexagon六边形,新建一个“line”角色,给它加上“不断缩小并消失”的功能。游戏要不断生成“六边形克隆体”,所以它将被做成“prefab预制件”。 4、spawner生成者,新建一个game object,增加一个代码脚本,脚本控制,每隔1秒生成一个新的“六边形克隆体”。
三、不断的“运行play”查看效果,用键盘左右方向键控制。本教程不包含build和对外发布环节。
一步步操作
Unity Hub中,新建项目”2D“:

等待Unity被打开:

Unity打开后,点击菜单”File->Build Settings“:

选择Android,并点击右下角”Switch Platform“:

等待Unity运行调用Android平台:

好了以后,点击菜单”Edit->Preferences“:

选择”Visual Studio“:

进入主界面,将看到, 最上面标题栏,已经有Android字样, 最左侧是”Hierachy角色等级结构“栏, 最右侧是”Inspector检查器“栏:

在最左侧”Hierachy角色等级结构“栏选中”Main Camera主摄像机“, 并在最右侧”Inspector检查器“栏,修改Background颜色为红色:


运行一下,查看效果,确认背景为红色: 在最左侧”Hierachy角色等级结构“栏中会自动多出一个角色:"DontDestroyOnLoad". 这是Unity 2021的正常现象,我也不知道为什么要出现这么个难以理解的东西。

在最左侧”Hierachy角色等级结构“栏中,选中最上面的”SampleScene“, 并右击新建一个”2DObject->Sprites->Circle“:

会出现一个新角色”Circle“,要确认它与”Main Camera“是”同级别“的:

选中”Circle“,并在最右侧”Inspector检查器->Sprite Renderer->Sprite“中搜索并选择”Knob“:


在”Transform->右上角->Reset“

在”Background“中选择颜色为黑色,最下面的”A透明度“拉到靠左49:

好了,第1个角色”旋转中心“已经做好了。 现在开始做第2个角色”Player“。
新建一个Circle:

修改名称为”Player“:

点击”Inspector->transform->Reset“:

点击"Inspector->Sprite Renderer->Sprite"改为"knob":

修改坐标位置"Transform->Position->y=0.8":

点击增加组件按键”Add Component“:

输入关键词搜索”rigid“,选择”Rigidbody 2D“:

inspector中新增出现了”Rigidbody 2D“组件:

在”Rigidbody 2D->Body Type“中选择”Kinematic“:


再增加一下组件”Circle Collider 2D“:



勾选”Is Trigger“:

再增加一个脚本组件”Player“:



增加后,双击这个脚本”player“:

Unity将调用打开Visual Studio:




删除”Start()函数“,增加代码”public float moveSpeed = 600f“, 并在”Update()函数“中,增加玩家输入控制的代码“Input”:

当你输入"Inp"时,Visual Studio会自动提示补全“Input”, 如果没有提示, 说明你安装Unity与Visual Studio时,出了问题。 如何解决,我也不知道。 我的办法是重装,连带操作系统重装,这花了我两天时间。
下面的截屏图片,是我重装后的,背景色不与上面的不同。 Player的代码是这个样子的: using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class Player : MonoBehaviour { public float moveSpeed = 600f;
float movement = 0f;
// Update is called once per frame
void Update()
{
movement = Input.GetAxisRaw("Horizontal");
}
private void FixedUpdate()
{
transform.RotateAround(Vector3.zero, Vector3.forward, -movement * moveSpeed * Time.fixedDeltaTime);
}
}

好了,点击“play运行一下”,发现小白球会根据键盘左右键的控制,转动起来了:


下面开始做第3个角色“hexagon六边形”的本体。
新建“Effects->Line”,注意它的层级要与“player"在同一级:

修改它的名称为”Hexagon“,并点击”inspector->Line->Positions“,将打开”节点编辑矩阵“:


输入”Size=6“,则矩阵变为6行了,修改6个节点的坐标如下:

为”Hexagon“角色新建一个下属角色,选择”Create Empty“:

修改这个下属角色的名称为”Collider“:

”Collider“角色的层级效果如下:

为"Collider"角色新建一个”Edge Collider 2D“组件:


Collider默认是2个节点,输入6个,并编辑节点坐标:


鼠标滚轮放大”Scene“栏窗口,会发现细细的、绿色的”Collider六边形“, 它藏在白色的”Hexagon“里面:

回去Hierarchy,选择”Hexagon“,为它新建一个”Rigidbody 2D“组件:


修改选择”Inspector->Rigidbody 2D->Body Type“为"Kinematic":

为”Hexagon“角色新建一个”Hexagon“脚本:



双击”Hexagon“,进入Visual Studio:


修改代码如下,增加了对六边形角色的”放大、缩小、删除“功能: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Hexagon : MonoBehaviour { public Rigidbody2D rb; public float shrinkSpeed = 3f; // Start is called before the first frame update void Start() { rb.rotation = Random.Range(0f, 360f); transform.localScale = Vector3.one * 10f;
}
// Update is called once per frame
void Update()
{
transform.localScale -= Vector3.one * shrinkSpeed * Time.deltaTime;
if (transform.localScale.x <= 0.05f)
{
Destroy(gameObject);
}
}
}

回到"Inspector->Hexagon (Script)->Rb",里面显示"None (Rigidbody 2D): 将上面的另一个组件”Rigidbody 2D“拖拽放进去:

于是,我们的代码就获取到了对”Rigidbody 2D“组件对象的控制权了,即Rb:

点击”play运行“一下,查看效果, 是的,六边形开始有从大变小的动画效果了:

将"Hierarchy"栏中的"Hexagon"角色,拖拽到”Project“栏中, 把"Hexagon"角色本体变为可克隆的"prefab资源":

查看一下”Project->Assets“中,确实多了一个资源”Hexagon“:

在"Hierarchy"栏中删除"Hexagon"角色本体。
好了,第3个角色也做完了。 现在要做第4个角色,即"spawer",用来生成"Hexagon"克隆体。 这是一个”Hierarchy”中以文字形式存在,但是"Scene“中看不见的角色。
新建一个空角色“Create Empty”:

修改名称为“Spawner”:

为它新建一个“Spawner”脚本:

双击打开脚本:

增加代码如下,为"Spawner"角色增加每隔1秒,实例化一个“hexagonPrefab”克隆体的功能: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spawner : MonoBehaviour { public GameObject hexagonPrefab; public float spawnRate = 1f; private float nextTimeToSpawn = 0f; // Update is called once per frame void Update() { if (Time.time >= nextTimeToSpawn) { Instantiate(hexagonPrefab, Vector3.zero, Quaternion.identity); nextTimeToSpawn = Time.time + 1f / spawnRate; }
}
}

回到Unity,"Hierarchy"中点击“Spawner”, 并找到"Inspector->Spawner (Script)->Hexagon Prefab", 里面是空的“None (Game Object)”:

在“Project->Assets”中找到“Hexagon”, 并拖拽到上面的"Inspector->Spawner (Script)->Hexagon Prefab"里:

点击“play运行”,查看效果, 是的,有无数个六边形不断地出现了:

好了,主体功能已经完成, 最后一步是要实现“碰撞效果”, 即,六边形碰撞到小圆时,游戏结束并重新开始。
双击“Project->Assets->Player”脚本,打开Visual Studio:


最上面,增加“场景命名空间”代码: using UnityEngine.SceneManagement;
增加新建事件函数代码: private void OnTriggerEnter2D(Collider2D collision) { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); }
完整代码如下:

点击“play运行"一下,有游戏重启的效果就行了。 游戏功能已经完成。
最后,为了锦上添花, 我们还要增加难度,让整个场景旋转起来, 这样,更酷了。
实现方法是对”Main Camera“增加功能。 在"Hierarchy"栏中,选中”Main Camera“, 并在”Inspector“栏中增加”Ratator“脚本:

双击”Rotator“,打开Visual Studio:

增加代码如下,增加了旋转功能: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Ratator : MonoBehaviour { // Start is called before the first frame update void Start() {
}
// Update is called once per frame
void Update()
{
transform.RotateAround(Vector3.forward, Time.deltaTime * 1f);
}
}


ok,全部结束了。 运行一下吧。 累死了......不过你如果看到这了,就给个赞吧, 谢谢。
更多推荐
所有评论(0)