笔记要点
edit--->project settings--->editor
导入资源
单击右键--->Show in Explorer--->Assets复制粘贴
图层问题
springjoint组件
右侧小蓝点表示固定位置
Static 保持静止 不受重力
Scripts用来存放脚本
public class Bird : MonoBehaviour {
private bool isClick = false;
private void OnMouseDown()//鼠标按下
{
isClick = true;
}
private void OnMouseUp()//鼠标抬起
{
isClick = false;
}
private void Update()
{
if (isClick) {//鼠标一直按下,进行位置的跟随
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);//坐标系不同,小鸟坐标是屏幕中间是(0,0),而鼠标则是左下角是(0,0),将屏幕坐标转换为世界坐标
transform.position += new Vector3(0, 0, -Camera.main.transform.position.z);//将3维坐标投射到2维平面上
}
}
}
最大距离限制(05)
选择图片,并拖到至
ctrl+6打开动画窗口
取消循环播放
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Pig : MonoBehaviour {
public float maxSpeed = 10;
public float minSpeed = 2;
private SpriteRenderer render;
public Sprite hurt;
public GameObject boom;
private void Awake()
{
render = GetComponent<SpriteRenderer>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.relativeVelocity.magnitude > maxSpeed)
{
Dead();
}
else if(collision.relativeVelocity.magnitude > minSpeed && collision.relativeVelocity.magnitude<maxSpeed) {
render.sprite = hurt;
}
}
void Dead() {
Destroy(gameObject);
Instantiate(boom, transform.position, Quaternion.identity);
}
}
预设体
拖动预设体到pig脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Pig : MonoBehaviour {
public float maxSpeed = 10;
public float minSpeed = 2;
private SpriteRenderer render;
public Sprite hurt;
public GameObject boom;
public GameObject score;
private void Awake()
{
render = GetComponent<SpriteRenderer>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.relativeVelocity.magnitude > maxSpeed)
{
Dead();
}
else if(collision.relativeVelocity.magnitude > minSpeed && collision.relativeVelocity.magnitude<maxSpeed) {
render.sprite = hurt;
}
}
void Dead() {
Destroy(gameObject);
Instantiate(boom, transform.position, Quaternion.identity);
GameObject go = Instantiate(score, transform.position+new Vector3(0,0.5f,0), Quaternion.identity);
Destroy(go, 1.5f);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class GameManager : MonoBehaviour {
public List<Bird> birds;
public List<Pig> pig;
public static GameManager _instance;private void Start()
{
Initialized();
}private void Awake()
{
_instance = this;
}
private void Initialized()
{
{
for (int i = 0; i < birds.Count; i++) {
if (i == 0)
{
birds[i].enabled = true;
birds[i].sp.enabled = true;
}
else {
birds[i].enabled = false;
birds[i].sp.enabled = false;
}
}
}
}
public void NextBird() {
if (pig.Count > 0)
{
if (birds.Count > 0)
{
//下一只飞吧
Initialized();
}
else {
//lose
}
}
else {
//win
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Bird : MonoBehaviour {
private bool isClick = false;
public Transform rightPos;
public float maxDis = 1;
[HideInInspector]
public SpringJoint2D sp;
private Rigidbody2D rg;
public LineRenderer right;
public LineRenderer left;
public Transform leftPos;
public GameObject boom;
private void Awake()
{
sp = GetComponent<SpringJoint2D>();
rg = GetComponent<Rigidbody2D>();
}
private void OnMouseDown()//鼠标按下
{
isClick = true;
rg.isKinematic = true;//物理学计算
}
private void OnMouseUp()//鼠标抬起
{
isClick = false;
rg.isKinematic = false;
Invoke("Fly", 0.1f);
}
private void Update()
{
if (isClick) {//鼠标一直按下,进行位置的跟随
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);//坐标系不同,小鸟坐标是屏幕中间是(0,0),而鼠标则是左下角是(0,0),将屏幕坐标转换为世界坐标
transform.position += new Vector3(0, 0, -Camera.main.transform.position.z);//将3维坐标投射到2维平面上
if (Vector3.Distance(transform.position,rightPos.position)>maxDis) {//进行位置限定
Vector3 pos = (transform.position - rightPos.position).normalized;//单位化向量
pos *= maxDis;
transform.position = pos + rightPos.position;
}
Line();
}
}
void Fly() {
sp.enabled = false;
Invoke("Next", 5);
}
/// <summary>
/// 画线操作
/// </summary>
void Line() {
right.SetPosition(0, rightPos.position);
right.SetPosition(1, transform.position);
left.SetPosition(0, leftPos.position);
left.SetPosition(1, transform.position);}
/// <summary>
/// 下一只小鸟飞出
/// </summary>
void Next() {
GameManager._instance.birds.Remove(this);
Destroy(gameObject);
Instantiate(boom, transform.position, Quaternion.identity);
GameManager._instance.NextBird();
}
}
设置拖尾效果
添加show函数
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Win : MonoBehaviour {
/// <summary>
/// 动画播放完,显示星星
/// </summary>
public void Show() {
GameManager._instance.ShowStars();
}
}
补充:
渲染层级
1.Camera
depth先绘制depth低的物体
2.同一个camera
sorting layer
3.同一个sorting layer
order in layer
大的遮挡小的
按照layer有选择性的渲染场景中的物体
主摄像机和UI摄像机图像的融合
让星星显示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class GameManager : MonoBehaviour {
public List<Bird> birds;
public List<Pig> pig;
public static GameManager _instance;
private Vector3 origionPos;//初始位置
public GameObject win;
public GameObject lose;
public GameObject[] stars;private void Start()
{
Initialized();
}private void Awake()
{
_instance = this;
if (birds.Count > 0) {
origionPos = birds[0].transform.position;
}
}
private void Initialized()
{
{
for (int i = 0; i < birds.Count; i++) {
if (i == 0)
{
birds[0].transform.position= origionPos;
birds[i].enabled = true;
birds[i].sp.enabled = true;
}
else {
birds[i].enabled = false;
birds[i].sp.enabled = false;
}
}
}
}
public void NextBird() {
if (pig.Count > 0)
{
if (birds.Count > 0)
{
//下一只飞吧
Initialized();
}
else {
//lose
lose.SetActive(true);
}
}
else {
//win
win.SetActive(true);
}
}
public void ShowStars() {
StartCoroutine("show");
}
IEnumerator show() {
for (int i = 0; i < birds.Count+1; i++) {
yield return new WaitForSeconds(0.2f);
stars[i].SetActive(true);
}
}
}
携程的用法
锚点的修改
滑窗动画的建立
public void Replay() {
SceneManager.LoadScene(2);
}
File--->build settings
取消勾选快速转换动画
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class PausePanel : MonoBehaviour {
private Animator anim;
public GameObject button;
private void Awake()
{
anim = GetComponent<Animator>();
}
public void Retry() {
Time.timeScale = 1;
SceneManager.LoadScene(2);
}
/// <summary>
/// 点击了Pause按钮
/// </summary>
public void Pause() {
//播放pause动画
anim.SetBool("isPause", true);
button.SetActive(false);
}
public void Home() {
Time.timeScale = 1;
SceneManager.LoadScene(1);
}
/// <summary>
/// 点击了继续按钮
/// </summary>
public void Resume()
{
//播放resume动画
Time.timeScale = 1;
anim.SetBool("isPause", false);}
/// <summary>
/// pause动画播放完调用
/// </summary>
public void pauseAnimEnd() {
Time.timeScale = 0;
}
/// <summary>
/// resume动画播放完调用
/// </summary>
public void ResumeAniEnd() {
button.SetActive(true);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Bird : MonoBehaviour {
private bool isClick = false;
public Transform rightPos;
public float maxDis = 1;
[HideInInspector]
public SpringJoint2D sp;
private Rigidbody2D rg;
public LineRenderer right;
public LineRenderer left;
public Transform leftPos;
public GameObject boom;
private bool canMove=true;
public float smooth = 3;
public AudioClip select;
public AudioClip fly;private void Awake()
{
sp = GetComponent<SpringJoint2D>();
rg = GetComponent<Rigidbody2D>();
}
private void OnMouseDown()//鼠标按下
{
if (canMove) {
isClick = true;
rg.isKinematic = true;//物理学计算
}
}
private void OnMouseUp()//鼠标抬起
{
if (canMove)
{
Audioplay(select);
isClick = false;
rg.isKinematic = false;
Invoke("Fly", 0.1f);
//禁用划线组件
right.enabled = false;
left.enabled = false;
canMove = false;
}
}
private void Update()
{
if (isClick) {//鼠标一直按下,进行位置的跟随
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);//坐标系不同,小鸟坐标是屏幕中间是(0,0),而鼠标则是左下角是(0,0),将屏幕坐标转换为世界坐标
transform.position += new Vector3(0, 0, -Camera.main.transform.position.z);//将3维坐标投射到2维平面上
if (Vector3.Distance(transform.position,rightPos.position)>maxDis) {//进行位置限定
Vector3 pos = (transform.position - rightPos.position).normalized;//单位化向量
pos *= maxDis;
transform.position = pos + rightPos.position;
}
Line();
}
//相机跟随
float posX = transform.position.x;
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position,new Vector3(Mathf.Clamp(posX,0,20),Camera.main.transform.position.y, Camera.main.transform.position.z),smooth*Time.deltaTime);
}
void Fly() {
sp.enabled = false;
Invoke("Next", 5);
Audioplay(fly);
}
/// <summary>
/// 画线操作
/// </summary>
void Line() {
right.enabled = true;
left.enabled = true;
right.SetPosition(0, rightPos.position);
right.SetPosition(1, transform.position);
left.SetPosition(0, leftPos.position);
left.SetPosition(1, transform.position);}
/// <summary>
/// 下一只小鸟飞出
/// </summary>
void Next() {
GameManager._instance.birds.Remove(this);
Destroy(gameObject);
Instantiate(boom, transform.position, Quaternion.identity);
GameManager._instance.NextBird();
}
public void Audioplay(AudioClip clip) {
AudioSource.PlayClipAtPoint(clip,transform.position);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Pig : MonoBehaviour {
public float maxSpeed = 10;
public float minSpeed = 2;
private SpriteRenderer render;
public Sprite hurt;
public GameObject boom;
public GameObject score;
public bool isPig=false;
public AudioClip hurtClip;
public AudioClip dead;
public AudioClip birdCollision;private void Awake()
{
render = GetComponent<SpriteRenderer>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag=="Player") { Audioplay(birdCollision); }
if (collision.relativeVelocity.magnitude > maxSpeed)
{
Dead();
}
else if(collision.relativeVelocity.magnitude > minSpeed && collision.relativeVelocity.magnitude<maxSpeed) {
render.sprite = hurt;
Audioplay(hurtClip);
}
}
void Dead() {
if (isPig) {
GameManager._instance.pig.Remove(this);
}
Destroy(gameObject);
Instantiate(boom, transform.position, Quaternion.identity);
GameObject go = Instantiate(score, transform.position+new Vector3(0,0.5f,0), Quaternion.identity);
Destroy(go, 1.5f);
//播放音乐
Audioplay(dead);
}
public void Audioplay(AudioClip clip)
{
AudioSource.PlayClipAtPoint(clip, transform.position);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Bird : MonoBehaviour {
private bool isClick = false;
public Transform rightPos;
public float maxDis = 1;
[HideInInspector]
public SpringJoint2D sp;
protected Rigidbody2D rg;
public LineRenderer right;
public LineRenderer left;
public Transform leftPos;
public GameObject boom;
private bool canMove=true;
public float smooth = 3;
public AudioClip select;
public AudioClip fly;
private bool isFly = false;private void Awake()
{
sp = GetComponent<SpringJoint2D>();
rg = GetComponent<Rigidbody2D>();
}
private void OnMouseDown()//鼠标按下
{
if (canMove) {
isClick = true;
rg.isKinematic = true;//物理学计算
}
}
private void OnMouseUp()//鼠标抬起
{
if (canMove)
{
Audioplay(select);
isClick = false;
rg.isKinematic = false;
Invoke("Fly", 0.1f);
//禁用划线组件
right.enabled = false;
left.enabled = false;
canMove = false;
}
}
private void Update()
{
if (isClick) {//鼠标一直按下,进行位置的跟随
transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);//坐标系不同,小鸟坐标是屏幕中间是(0,0),而鼠标则是左下角是(0,0),将屏幕坐标转换为世界坐标
transform.position += new Vector3(0, 0, -Camera.main.transform.position.z);//将3维坐标投射到2维平面上
if (Vector3.Distance(transform.position,rightPos.position)>maxDis) {//进行位置限定
Vector3 pos = (transform.position - rightPos.position).normalized;//单位化向量
pos *= maxDis;
transform.position = pos + rightPos.position;
}
Line();
}
//相机跟随
float posX = transform.position.x;
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position,new Vector3(Mathf.Clamp(posX,0,20),Camera.main.transform.position.y, Camera.main.transform.position.z),smooth*Time.deltaTime);
if (isFly) {
if (Input.GetMouseButtonDown(0)) {
ShowSkill();
}
}
}
void Fly() {
isFly = true;
sp.enabled = false;
Invoke("Next", 5);
Audioplay(fly);
}
/// <summary>
/// 画线操作
/// </summary>
void Line() {
right.enabled = true;
left.enabled = true;
right.SetPosition(0, rightPos.position);
right.SetPosition(1, transform.position);
left.SetPosition(0, leftPos.position);
left.SetPosition(1, transform.position);}
/// <summary>
/// 下一只小鸟飞出
/// </summary>
void Next() {
GameManager._instance.birds.Remove(this);
Destroy(gameObject);
Instantiate(boom, transform.position, Quaternion.identity);
GameManager._instance.NextBird();
}
public void Audioplay(AudioClip clip) {
AudioSource.PlayClipAtPoint(clip,transform.position);
}
private void OnCollisionEnter2D(Collision2D collision)
{
isFly = false;
}
/// <summary>
/// 技能操作
/// </summary>
public virtual void ShowSkill() {
isFly = false;
}
}
类继承 绿色小鸟
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class GreenBird : Bird {
public override void ShowSkill()
{
base.ShowSkill();
Vector3 speed = rg.velocity;
speed.x *= -1;
rg.velocity = speed;
}}
处理星星越界
IEnumerator show() {
for (int i = 0; i < birds.Count+1; i++) {
if (i >= stars.Length) {
break;
}
yield return new WaitForSeconds(0.2f);
stars[i].SetActive(true);
}
}