Sunny Land / Unity 资源

游戏展示

Sunny Land

游戏开发

创建 2D 游戏

Import Assets / 导入资源

Sunny Land

Background / 背景

每单元像素改为 16以下所有资源做相同操作

  • Project

    • Sunnyland
      • artwork
        • Environment
          • back
  • Inspector

    • Sprite Mode
      • Pixels Per Unit: 16
  • back -> Hierarchy

Tilemap / 瓦片地图

  • Hierarchy
    • Create
      • 2D Object
        • Tilemap
  • Window
    • 2D
      • Tile palette
  • Tile palette
    • Create New Palette
  • Project
    • Sunnyland
      • artwork
        • Environment
          • tileset
  • Inspector
    • Sprite Mode: Multiple
      • Pixels Per Unit: 16
    • Apply
    • Sprite Editor
  • Sprite Editor
    • Slice
      • Type: Grid By Cell Size
      • Pixel Size
        • X: 16
        • Y: 16
    • Apply
  • Project
    • Sunnyland
      • artwork
        • Environment
          • tileset -> Tile Palette

Main Camera Size / 调整相机尺寸

  • Inspector
    • Camera
      • Size: 6

Sorting Layer / 层排序

Order In Layer 也可以排序,数字越大越前

  • Hierarchy
    • Back
  • Inspector
    • Sprite Renderer
      • Additional Settings
        • Sorting Layer
          • Add Sorting Layer…

新增

  1. Background
  2. Foreground
  • Hierarchy

    • Back
  • Inspector

    • Check: 显示 Back
    • Sprite Renderer
      • Additional Settings
        • Sorting Layer: Background
  • Hierarchy

    • Grid
      • Tielmap
  • Inspector

    • Sprite Renderer
      • Additional Settings
        • Sorting Layer: Fackground

Player

创建 Player Sprite, 并将 player-idle-1 拖入

  • Project
    • Assets
      • Sunnyland
        • artwork
          • Sprites
            • player
              • idle: 16 Pixels Per Unit
                • player-idle-1

->

  • Hierarchy
    • Create
      • 2D Object
        • Sprite: Player
  • Inspector
    • Sprite Renderer
      • Sprite: player-idle-1

Physics / 物理

Tilemap

  • Inspector
    • Tilemap Collider 2D

Player

  • Inspector
    • Rigidbody 2D
      • Constraints
        • Freeze Rotation
          • Z: true # 不让 Z 轴移动
    • Box Collider 2D
      • Edit Collider

PlayerController.cs

1
using System;
2
using UnityEngine;
3
4
public class PlayerController : MonoBehaviour
5
{
6
    public float moveSpeed;
7
    public float jumpForce;
8
    private Rigidbody2D rb;
9
10
    private void Start()
11
    {
12
        rb = GetComponent<Rigidbody2D>();
13
14
        if (moveSpeed <= 0)
15
        {
16
            moveSpeed = 400;
17
        }
18
19
        if (jumpForce <= 0)
20
        {
21
            jumpForce = 400;
22
        }
23
    }
24
25
    private void FixedUpdate()
26
    {
27
        Movement();
28
    }
29
30
    private void Movement()
31
    {
32
        // -1 ~ 1
33
        float horizontalMove = Input.GetAxis("Horizontal");
34
        // [-1, 0, 1]
35
        float faceDirection = Input.GetAxisRaw("Horizontal");
36
        float EPSILON = 0.1f;
37
38
        if (Math.Abs(horizontalMove) > EPSILON)
39
        {
40
            rb.velocity = new Vector2(horizontalMove * moveSpeed * Time.deltaTime, rb.velocity.y);
41
        }
42
43
        if ((Math.Abs(faceDirection) > EPSILON))
44
        {
45
            transform.localScale = new Vector3(faceDirection, 1, 1);
46
        }
47
48
        if (Input.GetButtonDown("Jump"))
49
        {
50
            rb.velocity = new Vector2(rb.velocity.x, jumpForce * Time.deltaTime);
51
        }
52
    }
53
}

Animation / 动画

先选中 Player
先选中 Player
先选中 Player

  • Porject
    • Assets
      • Animation
        • Player
          • Animator Controller: Player

->

  • Inpsector
    • Animator
      • Controller: Player
  • Hierarchy
    • Player
  • Window
    • Animation
      • Animation
  • Animation
    • Create: Idle
    • Setting
      • Show Sample Rate: true
    • Samples: 10
  • Project
    • Assets
      • Sunnyland
        • artwork
          • Sprites
            • player
              • idle
                • player-idle-[1-4] ->Idle

Animator / 动画师(动画转变)

  • Window
    • Animation
      • Animator
  • Animator
    • Idle
      • Make Transition
        • Run
        • Jump
    • Run
      • Make Transition
        • Idle
        • Jump
    • Jump
      • Make Transition
        • Fall
    • Fall
      • Make Transition
        • Idle
    • Parameters + float
      • running: 0.0
    • Parameters + bool
      • jumping: false
      • falling: false
      • idle: false

Idle -> Run

  • Inspector
    • Has Exit Time: false
    • Settings
      • Transition Duration: 0
    • Conditions
      • running > 0.1

Run -> Idle

  • Inspector
    • Has Exit Time: false
    • Settings
      • Transition Duration: 0
    • Conditions
      • running < 0.1

Idle -> Jump & Run -> Jump

  • Inspector
    • Has Exit Time: false
    • Settings
      • Transition Duration: 0
    • Conditions
      • jumping: true

Jump -> Fall

  • Inspector
    • Has Exit Time: false
    • Settings
      • Transition Duration: 0
    • Conditions
      • falling: true
      • jumping: false

Fall -> Idle

  • Inspector
    • Has Exit Time: false
    • Settings
      • Transition Duration: 0
    • Conditions
      • falling: false
      • idle: true

Bug Fix:

  • Bug 现象
    • Player 会卡住
  • 原因
    • Box Collider 2DTilemap 多点碰撞检测
  • 解决方案
    • 添加 Circle Collider 2D,单点碰撞检测

Cinemachine / 电影镜头

Install Package / 安装包

  • Menu
    • Window
      • Package Manager
  • Packages
    • Cinemachine: Install

Create 2D Camera / 创建 2D Camera

  • Menu
    • Cinemachine
      • Create 2D Camera

CM vcam1

  • Inspector
    • Cinemachine Virtual Camera(Script)
      • Follow: Player
      • Lens
        • Orthographic Size: 7.43
      • Body
        • Dead Zone Width: 0.2
        • Dead Zone Height: 0.24
调整背景
  • Hierarchy
    • Create
      • Create Empty: Background
  • Inspector
    • Polygon Collider 2D
      • IsTrigger: True
CinemaConfiner / 界限,边界
  • Inspector
    • Cinemachine Virtual Camera(Script)
      • Add Extension
        • CinemaConfiner
    • Cinemachine Confiner(Script)
      • Bounding Shape 2D: Background(Polygon Collider 2D)

Collections / 收集

cherry & gem: 16 px

  • Hierarchy
    • Create
      • 2D Object
        • Sprite: Cherry
  • Inspector
    • Tag: Collection
    • Sprite Renderer
      • Sprite: cherry-1
      • Sorting Layer: Foreground
    • Box Collider
      • IsTrigger: true
    • Animator
      • Controller: Cherry
        <-
  • Project
    • Assets
      • Animation
        • Collections
          • Animator Controller: Cherry

Bug Fix:

  • Bug 现象
    • Player 方向键,粘墙(Tilemap)
  • 原因
    • PlayerTilemap 之间有磨擦力,不会自动滑落
  • 解决方案
    • Assets 添加 Physics Material 2D,磨擦力(Friction) 置为 0,添加到 Player > Box Collider 2D

UI

  • Hierarchy
    • Create
      • UI
        • Canvas
  • Canvas
    • Create
      • UI
        • Text: Cherry
        • Text: Numbers

Enemies / 敌人

Frog

  • Hierarchy
    • Create
      • Create Empty: Enemies
    • Enemies
      • Create
        • 2D Object
          • Sprite: Frog
      • Frog
        • Left: Empty Object
        • Right: Empty Object
  • Inspector
    • Tag: Enemy
    • Sprite Renderer
      • Sprite: frog-idle-1
    • Rigidbody 2D
      • Constraints
        • Freeze Rotation
          • Z: true
    • Box Collider 2D
      • Edit Collider

创建 Animator Controller: Frog, 拖动到 Frog

创建三种状态

  1. FrogIdle
  2. FrogJump
  3. FrogFall
  • Prefabs
    • Player

Eagle

Assets 中拖动 eagle-attack-1Hierarchy,重命名为 Eagle

  • Hierarchy
    • Eagle
      • Top: Empty Object
      • Bottom: Empty Object
  • Inspector
    • Tag: Enemy
    • Sprite Renderer
      • Sprite: eagle-attack-1
    • Rigidbody 2D
      • Gravity Scale: 0 # 重力
      • Constraints
        • Freeze Position
          • X: true
        • Freeze Rotation
          • Z: true
    • Box Collider 2D
      • Edit Collider
      • Offset
        • X: -0.4
        • Y: -0.4
      • Radius: 0.5

创建 Animator Controller: Eagle, 拖动到 Eagle

创建一种状态 Eagle

  • Prefabs
    • Player

Audio / 音效

  • Audio Listener: 耳朵
  • Audio Source: 扬声器
  • Audio Clips: 音乐/声音

Assets

  • Sound FX - Retro Pack
  • Casual Game BGM

Player BGM / 背景音

  • Inspector
    • Prefab
      • Overrides
        • Apply All # 每个 Player Prefab 都包含声音
    • Audio Source
      • Loop: true
      • AudioClip: BGM_01
        <-
  • Project
    • CasualGame
      • BGM_01

Enemy 消灭声音

  • Hierarchy
    • Enemy
      • Frog >
  • Inspector
    • Audio Source Effects
      • Play On Awake: false
      • AudioClip: hit_04
        <-
  • Project
    • Zero Rare
      • Retro Source
        • Hit
          • hit_04

其它声音

  • 收集
  • 。。。

Dialog / 对话框

Assets / 字体

Font: Free Pixel Font - Thaleah

  • Hierarchy
    • Canves
      • Panel: Dialog
        • Text

House

  • Inspector
    • Box Collider 2D
      • Edit Collider
      • Is Trigger: true
    • Enter Dialog(Script)
      • Enter Dialog: Dialog

Dialog Animation

  • Project
    • Animation
      • Dialog
        • Animation: Dialog

拖动到 UI Dialog 上,自动生成 EnterDialog Animator

Crouch / 下蹲

  • Edit
    • Project Settings…
      • Input
        • Jump
          • Duplicate Array Element: Crouch
  • Crouch
    • Positive Button: s
    • Alt Positive Button: down
  • Project
    • Assets
      • Animation
        • Player
          • Animation: Crouch

DeadLine / 死亡

  • Hierarchy
    • Empty Object: DeadLine
  • Inspector
    • Tag: DeadLine
    • Transform
      • Position
        • Y: -8.3
    • Box Collider 2D
      • Is Trigger: True

光效

  • 素材的 Diffuse 材质
  • 法线贴图 Normal map
  • 点光源,方向光源

安装 Package

Lightweight RP

  • Level_2
    • Grid
      • Ground
  • Inspector
    • Tilemap Renderer
      • Material: Default-Diffuse

变暗

导出游戏

  • File
    • Build Settings (Shift + Cmd + B)

Build Settings

  • Player Settings

小结

UI 与 Game 用的是两套不同的坐标。

参考:

  1. Unity教程 入门第一款游戏制作
  2. 2D Movement in Unity (Tutorial)

下载

游戏

Release: https://github.com/GameDevLog/GameDevLogTemplete/releases

源码

GitHub: https://github.com/GameDevLog/GameDevLogTemplete

本文标题:Sunny Land / Unity 资源

文章作者:iOSDevLog

发布时间:2019年12月11日 - 19:04:52

最后更新:2019年12月19日 - 15:40:59

原始链接:https://game.iosdevlog.com/2019/12/11/Sunny-Land/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

iOSDevLog wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!