1.游戏分析
2.资源导入
3.背景调整
4.图片自适应
在platform上添加Adjust脚本,让它自适应相机
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Adjust : MonoBehaviour
{
private void Start()
{
Resize();
}
/// <summary>
/// 适应屏幕
/// </summary>
void Resize()
{
float width = GetComponent<SpriteRenderer>().bounds.size.x;
float targetWidth = Camera.main.orthographicSize * 2 / Screen.height * Screen.width;
Vector3 scale = transform.localScale;
scale.x = targetWidth / width;
transform.localScale = scale;
}
}
运行程序,结果如下
5.涂鸦弹跳
给Player添加Player脚本,让它能够跳
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "Platform")
{
Jump(1);
}
}
public void Jump(float x)
{
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 12 * x), ForceMode2D.Impulse);
}
}
运行程序,结果如下
6.控制移动
在Player中添加角色左右移动的代码
Vector3 acc = Vector3.zero;
Vector3 diff;
if (Input.GetKey(KeyCode.LeftArrow))
{
acc.x = -0.1f;
transform.localScale = new Vector3(-1, 1, 1);
}
if (Input.GetKey(KeyCode.RightArrow))
{
acc.x = 0.1f;
transform.localScale = new Vector3(1, 1, 1);
}
diff = Vector3.MoveTowards(transform.localPosition, transform.localPosition + acc, 0.5f * Time.deltaTime);
transform.localPosition = diff;
运行程序,结果如下
7.设置边界
让两个边界互通
/// <summary>
/// 左右边界
/// </summary>
float rightBorder;
float leftBorder;
//设置边界
if(diff.x < leftBorder - 0.2f)
{
diff.x = rightBorder;
}
if (diff.x > rightBorder + 0.2f)
{
diff.x = leftBorder;
}
运行程序,结果如下
8.Tile的种类及参数
9.设置普通的砖块
给砖块 添加Tile脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tile : MonoBehaviour
{
public int tileType;
public Sprite[] sprites;
SpriteRenderer sp;
void Init()
{
sp = GetComponent<SpriteRenderer>();
switch (tileType)
{
case 0:
sp.sprite = sprites[0];
break;
case 1:
sp.sprite = sprites[1];
break;
case 2:
sp.sprite = sprites[2];
break;
case 3:
sp.sprite = sprites[3];
break;
case 4:
sp.sprite = sprites[4];
break;
case 5:
sp.sprite = sprites[5];
break;
default:
break;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player" && collision.GetComponent<Rigidbody2D>().velocity.y < 0)
{
switch (tileType)
{
case 0:
collision.GetComponent<Player>().Jump(1);
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
default:
break;
}
}
}
}
运行程序结果如下:
10.设置其他砖块碰撞效果
完善其他砖块的碰撞效果
switch (tileType)
{
case 0:
collision.GetComponent<Player>().Jump(1);
break;
case 1:
GetComponent<Rigidbody2D>().gravityScale = 1.5f;
break;
case 2:
collision.GetComponent<Player>().Jump(1);
GetComponent<Rigidbody2D>().gravityScale = 1.5f;
break;
case 3:
collision.GetComponent<Player>().Jump(1.5f);
break;
case 4:
collision.GetComponent<Player>().Jump(1);
break;
case 5:
collision.GetComponent<Player>().Jump(1);
break;
default:
break;
}
运行程序,结果如下
11.相机跟随
在相机上添加一个FollowTarget脚本,用来跟随玩家
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowTarget : MonoBehaviour
{
public Transform target;
Vector3 velocity = Vector3.zero;
float dampTime = 0.5f;
private void Update()
{
if (target)
{
Vector3 pos = Camera.main.WorldToViewportPoint(target.position);
Vector3 delta = target.position - Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, pos.z));
Vector3 destination = transform.position + delta;
destination.x = 0;
if(destination.y > transform.position.y)
{
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
}
}
}
}
运行程序结果如下:
12.单例模板
建立一个单例类型的类,之后只需要集成该类即可,不需要每次都要声明单例
创建一个MonoSingleton的单例类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class MonoSingleton<T> : MonoBehaviour
where T:MonoBehaviour
{
static T m_instance;
public static T Instance
{
get
{
return m_instance;
}
}
protected virtual void Awake()
{
m_instance = this as T;
}
}
然后创建GameManager并集成MonoSingleton
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoSingleton<GameManager>
{
protected override void Awake()
{
base.Awake();
}
}
13.生成Tile的对象池
在GameManager中创建Tile对象池,并进行初始化
//tile池子
public Queue<GameObject> tilePool = new Queue<GameObject>();
//tile样本
public GameObject tilePrefab;
//生成父位置
public Transform parent;
int initialSize = 60;
protected override void Awake()
{
base.Awake();
}
private void Start()
{
GenerateTilePool();
}
/// <summary>
/// 生成tile池子
/// </summary>
void GenerateTilePool()
{
for (int i = 0; i < initialSize; i++)
{
GameObject go = Instantiate(tilePrefab, parent);
go.SetActive(false);
go.name = i.ToString();
tilePool.Enqueue(go);
}
}
运行程序结果如下
14.tile的随机高度
创建GameSetting类,在里边定义各个种类的title
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
/// <summary>
/// 游戏设置
/// </summary>
[Serializable]
public class GameSetting
{
[Serializable]
public class NormalTile
{
//随机最小高度
public float minHeight;
//随机最大高度
public float maxHeight;
}
[Serializable]
public class BrokenTile
{
public float minHeight;
public float maxHeight;
}
[Serializable]
public class OneTimeOnly
{
public float minHeight;
public float maxHeight;
}
[Serializable]
public class SpringTile
{
public float minHeight;
public float maxHeight;
}
[Serializable]
public class MovingHorizontally
{
public float minHeight;
public float maxHeight;
//移动距离
public float distance;
//速度
public float speed;
}
[Serializable]
public class MovingVertically
{
public float minHeight;
public float maxHeight;
public float distance;
public float speed;
}
public NormalTile normalTile;
public BrokenTile brokenTile;
public OneTimeOnly oneTimeOnly;
public SpringTile springTile;
public MovingHorizontally movingHorizontally;
public MovingVertically movingVertically;
}