2D游戏场景

创建2D游戏工程,和场景

  • 创建2D工程


  • 创建2D场景(场景会保存在Asset目录下面)


创建工作层

  • 为了使游戏对象在场景重的层次不会错乱,可以创建不同的工作层来管理游戏对象。从而使游戏游戏场景的层次更加分明,就类似PS里面的图层的概念。

  • 调出工作层面板


  • 这里我们创建两个工作层,一个是背景工作层,一个是前景工作层



添加静态的物体

  • 导入静态资源


    • 稍后会把资源传到网盘上面去

    资源地址 先占位

    • 这里啰嗦一下Unity支持的资源导入的方式主要有,直接将资源复制到文件的目录下面去,将资源拖动到Project视图中的Asset文件夹中,通过Assets -> Import New Asset进行导入
  • 创建游戏的天空背景(就是创建一个精灵)


  • 精灵各项属性的意思


    • Sprite 图片信息
    • Color 颜色信息
    • Material 材质信息
    • Sorting Layer 分类层,层级越靠前优先级越高,相同情况后被渲染
    • Order in Layer 层中的顺序,数值越大优先级越高相同情况后被渲染
  • 修改精灵的属性


  • 创建游戏的草地精灵(小技巧,拖动资源到Hierachy面板可以直接创建精灵)


    • 草地的层应该和天空的层一样,渲染顺序要比天空要大
  • 多复制(选中 Ctrl + D)几个草地,调整位置放到层的下面

  • 调整相机的Size位置


添加角色和控制

  • 创建Sprite动画(将一张多纹理图切成多个Sprite)

  • 选择一个多纹理图,切割~~


  • 切割完成


  • 制作动画(创建一个Sprite,并把动画的第一帧给这个Sprite)


  • 创建一个文件夹用来保存动画


  • 给刚才创建的那个Sprite创建一个动画




    用Sprite的变化作为动画帧

    不断的添加关键帧
  • 创建一个文件夹(Scripts)用来存放脚本(C#)


  • 创建一个脚本(SwanMove)


  • 双击脚本并写代码(VS 果然是宇宙最强ide)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwanMove : MonoBehaviour {

    private float moveSpeed = 4;//移动的速度

    // Use this for initialization
    void Start () {
        //设置初始位置
        transform.position = new Vector3(22,3,0);
    }
    
    // Update is called once per frame
    void Update () {
        if (transform.position.x > -22)
        {
            //移动
            transform.Translate(Vector3.right * -moveSpeed * Time.deltaTime);
        }
        else {
            transform.position = new Vector3(22, 3, 0);
        }
    }
}
  • 将脚本设置给Sprite(直接将脚本拖动到Sprite上面就行)



创建主要的游戏对象

  • 创建Sprite 都为前景层,只是Order in Layout不同


  • 调整位置


  • 创建保龄球Sprite

  • 为保龄球添加刚体


  • 添加一个圆形的碰撞体(2D碰撞体)


  • 将保龄球做成预设体(就是方便多次使用)

  • 创建一个Asset文件夹取名 Prefabs


    直接拖过来
  • 创建一个空对象来控制保龄球的水平高度


  • 创建一个脚本来控制保龄球的实例化 使保龄球从天上一个一个的落下


public class GameController : MonoBehaviour {

    public GameObject ball;//保龄球
    private float maxWidth;
    private float time = 2;
    private GameObject newball;

    // Use this for initialization
    void Start () {
        //将屏幕的宽度转换成世界坐标
        Vector3 screenPos = new Vector3(Screen.width, 0, 0);
        Vector3 moveWidth = Camera.main.ScreenToWorldPoint(screenPos);

        //获取保龄球自身的宽度
        float ballWidth = ball.GetComponent<Renderer>().bounds.extents.x;

        //计算保龄球实例化位置的宽度
        maxWidth = moveWidth.x - ballWidth;
    }
    
    // Update is called once per frame
    void Update () {
        
    }

    private void FixedUpdate()
    {
        time -= Time.deltaTime;
        if (time < 0) {
            time = Random.Range(1.5f, 2.0f);
            float posX = Random.Range(-maxWidth, maxWidth);
            Vector3 spawnPosition = new Vector3(posX,transform.position.y,0);
            newball = Instantiate(ball, spawnPosition, Quaternion.identity);
            Destroy(newball, 10);
        }
    }
}
  • 将这个脚本放置到创建的空对象上面去并设置属性的初始值


  • 创建一个脚本控制帽子的移动

public class HatController : MonoBehaviour {

    private Vector3 rawPosition;
    private Vector3 hatPosition;
    private float maxWidth;
    // Use this for initialization
    void Start () {
        Vector3 screenPos = new Vector3(Screen.width,0,0);
        Vector3 moveWidth = Camera.main.ScreenToWorldPoint(screenPos);

        float hatW = GetComponent<Renderer>().bounds.extents.x;
        hatPosition = transform.position;
        maxWidth = moveWidth.x - hatW;

    }
    
    // Update is called once per frame
    void Update () {
        
    }

    private void FixedUpdate()
    {
        rawPosition = Camera.main.ScreenToViewportPoint(Input.mousePosition);
        hatPosition = new Vector3(rawPosition.x, hatPosition.y, 0);
        hatPosition.x = Mathf.Clamp(hatPosition.x, -maxWidth, maxWidth);
        GetComponent<Rigidbody2D>().MovePosition(hatPosition);
    }
}

创建2D阻挡物

  • 主要是给地面添加一个box的刚体



添加2D效果

  • 导入资源包
  • 添加粒子效果

Nothing is certain in this life. The only thing i know for sure is that. I love you and my life. That is the only thing i know. have a good day

:)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容