siki学院愤怒的小鸟脚本

GameManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
    public List birds;//场景中的小鸟作为元素的List,实际上是小鸟物体的Bird组件作为元素
    public List pigs;//场景中的猪作为元素的List,实际上是猪的Break组件作为元素
    private bool Win = false,Lose = false;//赢和输状态波尔值
    public Vector3 BirdsBornPos;//下一只小鸟出生传送点
    private GameObject LoseResult;//输了之后要显示的界面
    private GameObject WinResult;//赢了之后要显示的界面
    private GameObject BackAppear;//输或赢都显示的背景暗淡,附带有一段Color动画
    public GameObject[] Stars;//评分用的三个星星的List
    private GameObject pausePanel;//暂停界面
    private GameObject pauseButton;//暂停按钮
    public static GameManager _instance;//对GameManager的单例化
    private int CurrentStars;//临时存储当前关卡过关后获得的星星数
    private int sum;//临时使用的整型统计变量
    private GameObject LevelResult;//一个Canvas物体
    private Camera UIcamera;//专用以渲染UI类物体的摄像机
    private void Awake()
    {

        _instance = this;//单例语句
        if (birds.Count > 0) BirdsBornPos = birds[0].gameObject.transform.position;//给小鸟出生瞬移点一个初始值
        LevelResult = GameObject.Find("LevelResult");
        UIcamera = GameObject.Find("UICamera").GetComponent();//一些赋值
        BackAppear = LevelResult.transform.Find("BackAppear").gameObject;
        //查找active gameobject的hidden child gameobject
        WinResult = LevelResult.transform.Find("Win").gameObject;
        LoseResult = LevelResult.transform.Find("Lose").gameObject;
        BackAppear.transform.SetSiblingIndex(0);
        //设置backappear为子物体列表里第一个渲染的(最后面)/通过调整Hierarchy的层级排序也可以确定渲染的顺序,在上的先渲染

        pauseButton = GameObject.Find("Pause").transform.Find("pauseButton").gameObject;
        pausePanel = GameObject.Find("Pause").transform.Find("pausePanel").gameObject;
        //指定暂停按钮和面板物体

        Stars[0] = GameObject.Find("LevelResult").transform.Find("Win").transform.Find("Stars").transform.Find("left").gameObject;
        Stars[1] = GameObject.Find("LevelResult").transform.Find("Win").transform.Find("Stars").transform.Find("middle").gameObject;
        Stars[2] = GameObject.Find("LevelResult").transform.Find("Win").transform.Find("Stars").transform.Find("right").gameObject;
        //指定左中右三个胜利星星

        Canvas canvas = LevelResult.GetComponent();//先设定rendermode再设定相机
         canvas.renderMode= RenderMode.ScreenSpaceCamera;
        canvas.worldCamera = UIcamera;
        //设置canvas的渲染相机
    }
    private void Start()
    {
        Initialize();
        //if (BackAppear) Debug.Log("Found");
        //查找失败会返回一个空指针,否则是有值的
        //else Debug.Log("Missing");
    }
    public void Initialize()//重新对小鸟们的属性控制
    {
        for(int i = 0; i < birds.Count; i++)
        {
            if (i == 0)
            {
                birds[i].gameObject.transform.position = BirdsBornPos;
                birds[i].enabled = true;//对于component的可用性使用enabled
                birds[i].sp.enabled = true;//启用弹簧
            }
            else
            {
                birds[i].enabled = false;
                birds[i].sp.enabled = false;
            }
        }
    }
    public void JudgeLevelResult()
    {
        if (Win == false&&pigs.Count == 0)
        {
            Win = true;

            WinResult.SetActive(true);//对于gameobject的可用性使用setactive
            BackAppear.SetActive(true);
            StartCoroutine("ShowStars");//协程显示星星
        }
        if (Lose == false&&pigs.Count > 0 && birds.Count == 0)
        {
            Lose = true;
            LoseResult.SetActive(true);
            BackAppear.SetActive(true);//背景逐渐变黑,实质上是BackAppear物体的激活,关于它的动画是自动播放的

        }
    }

    IEnumerator  ShowStars()
    {

        for(CurrentStars = 0; CurrentStars < birds.Count&&CurrentStars<3; CurrentStars++)//显示星星不超过3个并且显示星星数是剩余小鸟的数量加一
        {
            yield return new WaitForSeconds(0.5f);//每个星星显示的间隔

            Stars[CurrentStars].SetActive(true);
        }

        SaveData();//显示完星星存储一下关卡星星的数据
    }

    private void SaveData()
    {

        if (CurrentStars > PlayerPrefs.GetInt(PlayerPrefs.GetString("CurrentTheme")+ PlayerPrefs.GetString("CurrentLevel")))//只有刷新过往记录才执行存储
        {
            PlayerPrefs.SetInt(PlayerPrefs.GetString("CurrentTheme")+PlayerPrefs.GetString("CurrentLevel"), CurrentStars);//向对应的“目前地图+目前关卡”键值存整型值
        }
        switch (PlayerPrefs.GetString("CurrentTheme"))//分类讨论
        {
            case "T1":
                sum = 0;
                for(int i = 1; i <= 12; i++)
                {

                    sum += PlayerPrefs.GetInt("T1"+"level"+i);
                }
                break;
            case "T2":
                sum = 0;
                for (int i = 1; i <= 12; i++)
                {

                    sum += PlayerPrefs.GetInt("T1" + "level" + i);
                }
                for (int i = 1; i <= 8; i++)
                {
                    sum += PlayerPrefs.GetInt("T2"+"level" + i);
                }
                break;
            case "T3":
                sum = 0;
                for (int i = 1; i <= 12; i++)
                {

                    sum += PlayerPrefs.GetInt("T1" + "level" + i);
                }
                for (int i = 1; i <= 8; i++)
                {
                    sum += PlayerPrefs.GetInt("T2" + "level" + i);
                }
                for (int i = 1; i <= 1; i++)
                {
                    sum += PlayerPrefs.GetInt("T3"+"level" + i);
                }
                break;//遍历已解锁的地图的所有关卡的星星数
        }
        PlayerPrefs.SetInt("CollectedNum", sum);//保存已收集星星总数,赋值计算好的sum

    }
    public void Pause()
    {
        Time.timeScale = 0;//控制时间速度为零
        pauseButton.SetActive(false);//隐藏暂停按钮
        pausePanel.SetActive(true);//显示暂停面板
    }
    public void Resume()
    {
        Time.timeScale = 1;//恢复时间速度
        pausePanel.SetActive(false);//逆着操作上两步
        pauseButton.SetActive(true);
    }
    public void BacktoLevelMenu()
    {
        //LoadSceneAsync和LoadScene延迟那么一小点时间,用来避免场景装载的速度过慢影响玩家体验
        SceneManager.LoadSceneAsync("002-levels");//装载levels场景
        if (PlayerPrefs.GetString("CurrentTheme") == "T1")
        {

            GameObject.Find("Canvas").transform.Find("Map").gameObject.SetActive(false);
            GameObject.Find("Canvas").transform.Find("LevelsPanel-1").gameObject.SetActive(true);
        }
        else if(PlayerPrefs.GetString("CurrentTheme") == "T2")
        {
            GameObject.Find("Canvas").transform.Find("Map").gameObject.SetActive(false);
            GameObject.Find("Canvas").transform.Find("LevelsPanel-2").gameObject.SetActive(true);
        }
        else if (PlayerPrefs.GetString("CurrentTheme") == "T3")
        {
            GameObject.Find("Canvas").transform.Find("Map").gameObject.SetActive(false);
            GameObject.Find("Canvas").transform.Find("LevelsPanel-3").gameObject.SetActive(true);
        }
        else
        {

        }
    }
    public void RestartThisLevel()
    {
        SceneManager.LoadSceneAsync("003-game");//重新装载当前场景
        Time.timeScale = 1;
    }

    public void EnterGame()
    {
        GameObject.Find("Canvas").transform.Find("Start").gameObject.SetActive(false);//开始游戏按钮
        GameObject.Find("Canvas").transform.Find("hint").gameObject.SetActive(true);//加载游戏的文字提示
        Invoke("LoadMapsMenu",2.2f);//有一个假装loading的等待时间哈哈
    }
    private void LoadMapsMenu()
    {
        SceneManager.LoadSceneAsync("002-levels");
    }
    public void ShowReadme()
    {
        GameObject.Find("Canvas").transform.Find("ReadMe").gameObject.SetActive(true);//一张说明图片
        GameObject.Find("Canvas").transform.Find("ReadButton").gameObject.SetActive(false);//“如何玩”按钮
        GameObject.Find("Canvas").transform.Find("Iknew").gameObject.SetActive(true);//“嗯嗯”按钮
    }
    public void HideReadme()//和ShowReadme功能相反
    {
        GameObject.Find("Canvas").transform.Find("ReadMe").gameObject.SetActive(false);
        GameObject.Find("Canvas").transform.Find("ReadButton").gameObject.SetActive(true);
        GameObject.Find("Canvas").transform.Find("Iknew").gameObject.SetActive(false);
    }
    public void Quit()//退出游戏程序
    {
        Application.Quit();
    }
}

Bird.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bird : MonoBehaviour {
    private bool isClick = false;//正在点击
    protected bool dead = false;
    private bool CanControlCamera = true;//当前是否可以通过方向键控制相机
    private Transform stickMidPos;//两个发射树杈的中间点
    public GameObject burst;//小鸟死亡的烟雾动画
    public float maxDistance = 1.8f;//拖拽小鸟的最大限制距离
    private bool CanClick = true;//可以拖动小鸟
    [HideInInspector]public SpringJoint2D sp;//小鸟的弹簧组件
    protected Rigidbody2D rg;//小鸟的刚体组件
    protected SpriteRenderer SR;//小鸟的图片渲染组件
    public Sprite Wait,Selected,Launched,Skilling,Injured;//等待、被点击、被发射、施展技能、受伤 各种状态的小鸟图片
    private bool lockrotation = true;//锁定小鸟的旋转角度
    private LineRenderer LauchStick_rightPoint_LineRenderer;//左右树杈两个画线渲染组件
    private LineRenderer LauchStick_leftPoint_LineRenderer;
    private Transform LauchStick_rightPosition;//左右两个树杈枝头的位置
    private Transform LauchStick_leftPosition;
    private Trail ItsTrail;//拖尾脚本组件
    private bool CanUseSkill;//可以使用技能
    public AudioClip birdClickSound;//小鸟被点击(选择)的声音
    public AudioClip birdFlySound;//小鸟被弹射瞬间的声音
    public float CameraSmooth = 3;//镜头跟随的平滑度
    private float BirdPosX;//用来存储鸟的x坐标
    public static Bird _instance;//Bird脚本的单例

    private void Awake()
    {
        _instance = this;//单例方法

        Camera.main.transform.position = new Vector3(1, Camera.main.transform.position.y, Camera.main.transform.position.z);//每个小鸟上弹射架的瞬间相机位置都强制被设置为一个初始位置
        sp = GetComponent();
        rg = GetComponent();//各种组件变量的赋初值
        SR = GetComponent();
        if(sp.enabled==false)SR.sprite = Wait;//若弹簧组件为禁用,小鸟的图像为等待状态,这时小鸟躺在地上
        LauchStick_rightPoint_LineRenderer = GameObject.Find("rightPos").GetComponent();
        LauchStick_leftPoint_LineRenderer = GameObject.Find("leftPos").GetComponent();
        LauchStick_rightPosition = GameObject.Find("rightPos").transform;
        LauchStick_leftPosition = GameObject.Find("leftPos").transform;
        ItsTrail = GetComponent();
        stickMidPos = GameObject.Find("midPos").transform;
    }

    private void OnMouseDown()//每当按下鼠标
    {


        if (CanClick)//在可以拖动小鸟的时候
        {
            LauchStick_rightPoint_LineRenderer.enabled = true;//两个画线组件激活
            LauchStick_leftPoint_LineRenderer.enabled = true;
            SR.sprite = Selected;//小鸟图像变化
            AudioSource.PlayClipAtPoint(birdClickSound, Camera.main.transform.position);//播放点击音效
            CanControlCamera = false;//这时不能通过方向键控制相机
            isClick = true;//正在点击
            rg.isKinematic = true;//此时小鸟的运动只受到脚本的控制
            CanUseSkill = false;//不能使用技能
        }



    }
    private void OnMouseUp()//鼠标抬起
    {

        isClick = false;

        //禁用划线
        LauchStick_rightPoint_LineRenderer.enabled = false;
        LauchStick_leftPoint_LineRenderer.enabled = false;
        ItsTrail.ShowTrail();//开始显示拖尾
        rg.isKinematic = false;//恢复动力学影响

        lockrotation = false;//不锁镜头


        if (CanClick) {
            CanUseSkill = true;

            AudioSource.PlayClipAtPoint(birdFlySound,Camera.main.transform.position);
            SR.sprite = Launched;
            Invoke("Fly", 0.1f);//加一点延迟不然飞不出去
        }
        CanClick = false;//这时不能再拖动小鸟





    }
    private void Update()
    {

        if (CanUseSkill)//使用技能
        {
            if (Input.GetMouseButtonDown(0))
            {
                UseSkill();//释放技能并改变图片
                SR.sprite = Skilling;
            }
        }

        //锁定角度
        if (lockrotation) transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0);

        if (isClick&&CanClick)//拖动小鸟
        {
            transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition)+new Vector3(0,0,10);
            if (Vector3.Distance(transform.position, stickMidPos.position) > maxDistance)//限制拖动长度
            {
                Vector3 e_pos = (transform.position - stickMidPos.position).normalized;
                transform.position = stickMidPos.position + maxDistance * e_pos;
            }
            DrawLauchStickLine();
        }
        //镜头跟随
        BirdPosX = GameManager._instance.birds[0].transform.position.x;//在update里每帧更新鸟的x坐标
        if(CanControlCamera==false)Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position,//将第一个Vector3坐标参数按照第三个参数的平滑度过渡到第二个Vector3坐标参数
            new Vector3(Mathf.Clamp(BirdPosX,1,39),//将摄像机坐标限制在1-39,当BirdPosX在这个范围,Vector3的第一个参数才有效
            Camera.main.transform.position.y,
            Camera.main.transform.position.z),
            CameraSmooth);

        //镜头控制
        if (CanControlCamera&&Input.GetKey(KeyCode.LeftArrow)&&Camera.main.transform.position.x>=1.15)
        {
            Camera.main.transform.position = new Vector3(Camera.main.transform.position.x-0.15f, Camera.main.transform.position.y,Camera.main.transform.position.z);
            //Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position,new Vector3(Camera.main.transform.position.x - 2, Camera.main.transform.position.y, Camera.main.transform.transform.position.z), CameraSmooth);
        }
        else if (CanControlCamera&&Input.GetKey(KeyCode.RightArrow)&&Camera.main.transform.position.x<38.85)
        {
            Camera.main.transform.position = new Vector3(Camera.main.transform.position.x + 0.15f, Camera.main.transform.position.y, Camera.main.transform.position.z);
            //Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, new Vector3(Camera.main.transform.position.x + 2, Camera.main.transform.position.y, Camera.main.transform.transform.position.z), CameraSmooth);
        }
    }

    void Fly()
    {
        sp.enabled = false;//弹簧组建取消
        Invoke("Dead", 5f);//飞出5秒后强制死亡

    }

    void DrawLauchStickLine()
    {
        LauchStick_leftPoint_LineRenderer.SetPosition(0,transform.position);
        LauchStick_leftPoint_LineRenderer.SetPosition(1, LauchStick_leftPosition.position);

        LauchStick_rightPoint_LineRenderer.SetPosition(0, transform.position);//通过给定一个画线组件的坐标点位置来画线
        LauchStick_rightPoint_LineRenderer.SetPosition(1, LauchStick_rightPosition.position);
    }
    public void RemoveBird()
    {


        dead = true;
        GameManager._instance.birds.Remove(this);
        Destroy(gameObject);//在从数组中移除后再实际摧毁它,如果物体已经被摧毁从数组中移除就会不可行
        if (GameManager._instance.birds.Count != 0) GameManager._instance.Initialize();//当还有鸟时初始化小鸟状态
        GameManager._instance.JudgeLevelResult();
        lockrotation = true;
        CanControlCamera = true;//可以自由控制相机观察场景
        CanClick = true;//可以拖动下一个小鸟

        Camera.main.transform.position = new Vector3(1, Camera.main.transform.position.y, Camera.main.transform.position.z);
    }

    public void Dead()
    {

        gameObject.SetActive(false);//在此先隐藏这个鸟
        if (dead == false)//这个条件是为了防止黑鸟爆炸后仍然播放小烟雾
        {
            Instantiate(burst, transform.position, Quaternion.identity);
        }

        Invoke("RemoveBird", 1.5f);//从播放烟雾到实际移除的延迟



    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        ItsTrail.CloseTrail();//当碰到任何物体时小鸟不再显示拖尾
        if(collision.gameObject.tag=="ground")CanUseSkill = false;//小鸟的技能只能在为接触到地面上时触发
        if (collision.relativeVelocity.magnitude > 3.0&&collision.gameObject.tag!="bird")//一定的相对速度导致小鸟受伤
        {
            SR.sprite = Injured;
        }
    }

    public virtual void UseSkill()
    {
        CanUseSkill = false;//每个小鸟活着的时候只能使用一次技能

    }
}

Break.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Break : MonoBehaviour {
    public float DeadSpeed;
    public float minSpeed;
    private SpriteRenderer state_image;
    public Sprite hurt;
    public GameObject burst;
    public AudioClip hurtSound;
    public AudioClip destroySound;
    public GameObject purplescore3000;
    private void Awake()
    {
        state_image = GetComponent();//获取一下图片渲染组件

    }

    private void OnCollisionEnter2D(Collision2D collision)
    {


            if (collision.relativeVelocity.magnitude > DeadSpeed)//当碰撞相对速度大于死亡速度直接死亡
            {
                if(gameObject.name == "TNT")//对于TNT建筑物体执行爆破函数
                {
                    gameObject.GetComponent().Explode();

                }
                DeadPig();//敌人死亡
            AudioSource.PlayClipAtPoint(destroySound,Camera.main.transform.position);
            }
            else if (collision.relativeVelocity.magnitude > minSpeed && collision.relativeVelocity.magnitude < DeadSpeed)//relative.magnitude相对速度的量化
            {
    //改猪的sprite为受伤状态
                state_image.sprite = hurt;
            AudioSource.PlayClipAtPoint(hurtSound,Camera.main.transform.position);
            }



    }

    public void DeadPig()
    {
        Destroy(gameObject);//先摧毁这个物体
        GameObject tempscore = Instantiate(purplescore3000,transform.position+new Vector3(0f,1f,0f),Quaternion.identity);//显示分数图片
        Destroy(tempscore, 1.2f);//分数浮一会儿就消失
        GameManager._instance.pigs.Remove(this);//从GameManager的pigs组里移除对应元素
        Instantiate(burst,transform.position,Quaternion.identity);//死亡烟雾

        GameManager._instance.JudgeLevelResult();
    }


}

MapSelect.cs

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

    public int StarsRequireNum = 0;//开启这个关卡需要的星星总数
    private bool CanSelectThisMap = false;//控制关卡是否可以选择的波尔变量
    private int sum;//临时用来统计加法运算和的变量
    public GameObject locked;//地图图片的锁图像
    public GameObject starDisplay;//每个地图图片上伴随的大星星和一个解锁要求星星数量

    private void Start()
    {
        //PlayerPrefs.DeleteAll();这句用来清除所有经过PlayerPrefs语句保存的键值对数据
        switch (gameObject.name)
        {
            case "map1":
                sum = 0;
                for (int i = 1; i <= 12; i++)
                {

                    sum += PlayerPrefs.GetInt("T1" + "level" + i);
                }
                gameObject.transform.Find("star").transform.Find("starscount").GetComponent().text = sum + "/36";//控制已获得的星星数量显示
                break;
            case "map2":
                sum = 0;
                for (int i = 1; i <= 8; i++)
                {
                    sum += PlayerPrefs.GetInt("T2" + "level" + i);
                }
                gameObject.transform.Find("star").transform.Find("starscount").GetComponent().text = sum + "/24";
                break;
            case "map3":
                sum = 0;
                for (int i = 1; i <= 1; i++)
                {
                    sum += PlayerPrefs.GetInt("T3" + "level" + i);
                }
                gameObject.transform.Find("star").transform.Find("starscount").GetComponent().text = sum + "/3";
                break;
        }
        if(PlayerPrefs.GetInt("CollectedNum") >= StarsRequireNum)
        {
    //当已收集的星星数量不小于地图要求的数量就执行解锁代码
            locked.SetActive(false);
            starDisplay.SetActive(true);
            CanSelectThisMap = true;
        }
        else
        {
            locked.SetActive(true);
            starDisplay.SetActive(false);
            CanSelectThisMap = false;
        }
        //print(PlayerPrefs.GetInt("CollectedNum"));//这一句用来调试
    }

    public void SelectedMap()
    {
        if (CanSelectThisMap)
        {
    //用来隐藏地图选择界面并显示对应的关卡界面

            if (gameObject.name == "map1")
            {
                gameObject.transform.parent.parent.transform.Find("LevelsPanel-1").gameObject.SetActive(true);
                PlayerPrefs.SetString("CurrentTheme","T1");
            }
            else if (gameObject.name == "map2") {
                gameObject.transform.parent.parent.transform.Find("LevelsPanel-2").gameObject.SetActive(true);
                PlayerPrefs.SetString("CurrentTheme","T2");
            }
            else if (gameObject.name == "map3"){
                gameObject.transform.parent.parent.transform.Find("LevelsPanel-3").gameObject.SetActive(true);
                PlayerPrefs.SetString("CurrentTheme","T3");
            }
            else if(gameObject.name == "map_more")
            {
                Debug.Log("暂未开发");
            }
            transform.parent.gameObject.SetActive(false);

        }
    }
}

LevelSelect.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LevelSelect : MonoBehaviour {
    private bool CanEnter = false;//每个关卡按钮有一个“可进入”波尔值
    public Sprite LockImage;//锁的图像
    public Sprite UnlockImage;//没锁的图像
    public Sprite nostar,onestar,twostar,fullstar;//小星星的图像,和按钮相联,和保存的关卡星星数值同步
    private void Start () {
        if (int.Parse(gameObject.transform.Find("num").GetComponent().text) == 1)//每个主题的关卡一都为开启状态
        {
            CanEnter = true;

        }
        else if(PlayerPrefs.GetInt(       PlayerPrefs.GetString("CurrentTheme")+"level" + (int.Parse(gameObject.transform.Find("num").GetComponent().text) -1)            ) > 0)//如果上一关星星数不为零,此关卡可进入
        {
            CanEnter = true;
        }
        if (CanEnter)
        {
            gameObject.GetComponent().sprite = UnlockImage;//对于可进入的关卡,按钮样貌会改变
            gameObject.transform.Find("starsGot").gameObject.SetActive(true);
            gameObject.transform.Find("num").gameObject.SetActive(true);

            switch (PlayerPrefs.GetInt(PlayerPrefs.GetString("CurrentTheme")+gameObject.name))
            {
            case 0://根据关卡星星数控制小星星图像的选择性显示
                gameObject.transform.Find("starsGot").gameObject.GetComponent().sprite = nostar;
                break;
            case 1:
                gameObject.transform.Find("starsGot").gameObject.GetComponent().sprite = onestar;
                break;
            case 2:
                gameObject.transform.Find("starsGot").gameObject.GetComponent().sprite = twostar;
                break;
            case 3:
                gameObject.transform.Find("starsGot").gameObject.GetComponent().sprite = fullstar;
                break;
            default:
                break;

           }

        }
        else
        {//锁上时的图像变化
            gameObject.GetComponent().sprite = LockImage;
            gameObject.transform.Find("starsGot").gameObject.SetActive(false);
            gameObject.transform.Find("num").gameObject.SetActive(false);
        }

    }

    public void BackToMapMenu()
    {//返回地图选择界面,使用transform.parent方法调用父对象
        gameObject.transform.parent.parent.gameObject.SetActive(false);
        gameObject.transform.parent.parent.parent.Find("Map").gameObject.SetActive(true);
    }

    public void LevelSelected()
    {
        if (CanEnter)
        {

            PlayerPrefs.SetString("CurrentLevel",gameObject.name);//改变当前关卡的数值,每个按钮游戏物体的名字都是“levelxx”

            SceneManager.LoadSceneAsync("003-game");//进入实际游戏
        }
    }
}

Burst.cs

用在烟雾动画上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Burst : MonoBehaviour {

    public void Destroying()
    {
        Destroy(gameObject);//给Burst脚本的特定摧毁物体方法,在动画的最后一帧执行
    }

}

BlackBird.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Black_bird : Bird {
    public List blocks = new List();//用来存储黑鸟检测范围内的可摧毁对象,实际上存的是Break组件
    public GameObject burst3;//黑鸟专用的爆炸动画
    public AudioClip boom;//一个爆炸声
    public override void UseSkill()//继承Bird里的UseSkill方法
    {
        base.UseSkill();

        AudioSource.PlayClipAtPoint(boom,Camera.main.transform.position);
        if (blocks != null && blocks.Count > 0)//摧毁黑鸟杀伤范围内的所有物体(执行blocks组里每个元素的Dead方法)
        {
            for(int i = 0; i < blocks.Count; i++)
            {
                blocks[i].DeadPig();
            }

        }

        gameObject.SetActive(false);//在此先隐藏这个鸟
        if(dead==false)Instantiate(burst3, transform.position, Quaternion.identity);
        dead = true;

    }
    private void OnTriggerEnter2D(Collider2D collision)//通过一个大圆形状的Trigger Collider动态的控制blocks组里的元素
    {
        if (collision.gameObject.tag == "pig" || collision.gameObject.tag == "wood")
        {
            blocks.Add(collision.gameObject.GetComponent());
            //Debug.Log("添加了一个周围物体");
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "pig" || collision.gameObject.tag == "wood")
        {
            blocks.Remove(collision.gameObject.GetComponent());
            //Debug.Log("离开了一个周围物体");
        }
    }
}

GreenBird.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GreenBird : Bird {

    public override void UseSkill()
    {
        base.UseSkill();
        rg.velocity = new Vector2(-rg.velocity.x,rg.velocity.y);//技能是横轴的分速度反向
        SR.flipX=true;//鸟的图像横向反转
    }
}

YellowBird.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class YellowBird : Bird {


    public override void UseSkill()
    {
        base.UseSkill();
        rg.velocity *= 2;//黄鸟的技能是刚体速度加倍,嗖的一下飞出去
    }
}

LoadLevel.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadLevel : MonoBehaviour
{

    private void Awake()
    {
        Instantiate(Resources.Load(PlayerPrefs.GetString("CurrentTheme") + PlayerPrefs.GetString("CurrentLevel")));//在Resources素材文件夹里根据“当前地图名+当前关卡名”读取预存的游戏关卡场景,
        //关卡预制体的文件名都是“Txlevelxx”
    }
}

setting.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class setting : MonoBehaviour {


    void Start () {
        if(gameObject.name == "BackAppear")
        {
            gameObject.GetComponent().sizeDelta = new Vector2(1240,600);//用以控制所有关卡判断结果时显示的背景暗淡图片的大小,因为当时关卡预制体已经做好了,手动调整太麻烦
        }
        Screen.SetResolution(1234,530,true);//设置游戏窗口为一个特定的值并全屏显示,因为并不懂如何做到屏幕自适应并保持比例合适,从一开始开发就没有想到这个问题
    }

}

TNT.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TNT : MonoBehaviour {
    //和blackbird脚本差不多
    public List blocks = new List();
    public GameObject burst3;
    public void Explode()
    {



        if (blocks != null && blocks.Count > 0)
        {
            for (int i = 0; i < blocks.Count; i++)
            {
                blocks[i].DeadPig();
            }

        }

        gameObject.SetActive(false);//在此先隐藏这个鸟


    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "pig" || collision.gameObject.tag == "wood")
        {
            blocks.Add(collision.gameObject.GetComponent());
            //Debug.Log("添加了一个周围物体");
        }
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "pig" || collision.gameObject.tag == "wood")
        {
            blocks.Remove(collision.gameObject.GetComponent());
            //Debug.Log("离开了一个周围物体");
        }
    }
}

Trail.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Trail : MonoBehaviour {
    //一个复制挪用来的刀光残影效果插件,改了改变成小鸟拖尾效果

    public WeaponTrail myTrail;


    private float t = 0.033f;
    private float tempT = 0;
    private float animationIncrement = 0.003f;

    void Start()
    {

        // 默认没有拖尾效果
        myTrail.SetTime(0.0f, 0.0f, 1.0f);
    }

    public void ShowTrail()
    {
        //设置拖尾时长
        myTrail.SetTime(0.5f, 0.0f, 1.0f);
        //开始进行拖尾
        myTrail.StartTrail(0.5f, 0.4f);
    }

    public void CloseTrail()
    {
        //清除拖尾
        myTrail.ClearTrail();
    }

    void LateUpdate()
    {
        t = Mathf.Clamp(Time.deltaTime, 0, 0.066f);

        if (t > 0)
        {
            while (tempT < t)
            {
                tempT += animationIncrement;

                if (myTrail.time > 0)
                {
                    myTrail.Itterate(Time.time - t + tempT);
                }
                else
                {
                    myTrail.ClearTrail();
                }
            }

            tempT -= t;

            if (myTrail.time > 0)
            {
                myTrail.UpdateTrail(Time.time, t);
            }
        }
    }


}

你可能感兴趣的:(Unity,unity,c#)