嫌得蛋疼做了个接鸡蛋

桶的触发器

using UnityEngine;
public class EggCollider : MonoBehaviour
{
    void OnTriggerEnter(Collider co)
    {
        if (co.tag == "Egg")
        {
            ObjectPool.PushEgg(co.gameObject);
            Camera.main.GetComponent<GetEgg>().Clearing(false);
        }
    }
}

蛋的移动

using UnityEngine;
public class EggMove : MonoBehaviour
{
    Vector3 pos;//过渡的变量
    void Update()
    {
        pos = transform.position;//使物体下落
        pos.y -= Time.deltaTime * GetEgg.eggMoveSpeed;
        transform.position = pos;
    }
}

对象池?

using System.Collections.Generic;
using UnityEngine;
public class ObjectPool : MonoBehaviour
{
    static Queue<GameObject> pool = new Queue<GameObject>();//使用队列,先进先出,避免连续生成同一个对象
    static GameObject egg;//预制:鸡蛋
    static Vector3 _position;//初始位置
    static Quaternion _rotation;//初始旋转
    static Transform tran;
    void Awake()
    {
        tran = transform;
        egg = Resources.Load("GetEgg/Egg") as GameObject;//加载
        _position = egg.transform.position;//(记录初始变换,回收时重置)
        _rotation = egg.transform.rotation;
    }
    public static void GetObject()//得到一个对象
    {
        GameObject _egg = null;
        if (pool.Count >= 5)//给予一定的周转空间?
        {
            _egg = pool.Dequeue();//得到队列底部的对象
        }
        else
        {
            _egg = Instantiate(egg);//池子没有可用的资源,增加
            _egg.AddComponent<EggMove>();
            _egg.transform.SetParent(tran);
            pool.Enqueue(_egg);
        }
        GetEgg.eggMoveSpeed += 0.2f;//下落速度逐渐加快
        _egg.transform.position = new Vector3(Random.Range(-14, 14), 0, -20);//随机生成一个位置下落
        _egg.SetActive(true);
    }
    public static void PushEgg(GameObject obj)//回收一个对象
    {
        obj.transform.position = _position;
        obj.transform.rotation = _rotation;
        obj.SetActive(false);
        if (pool.Contains(obj))
            return;
        pool.Enqueue(obj);
    }
}

地面上的触发器

using UnityEngine;
public class PlaneCollider : MonoBehaviour
{
    void OnTriggerEnter(Collider co)
    {
        if (co.tag == "Egg")
        {
            ObjectPool.PushEgg(co.gameObject);
            Camera.main.GetComponent<GetEgg>().Clearing(true);
        }
    }
}

计时器

看我上一篇文章
http://blog.csdn.net/u013452440/article/details/64441095

其他。。。

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class GetEgg : MonoBehaviour
{
    GameObject player;//玩家

    int score = 0;//玩家分数
    int maxScore;//最高分数
    int minScore;//最低分数
    int lose = 0;//丢失的分数
    Text showScore;//显示玩家分数
    Text showLose;//显示玩家丢失分数
    GameObject showClearing;//显示结算结果
    float nextTime;//计时
    float oriSpeed;//下一次出现的时间
    float shift;//难度
    public static float eggMoveSpeed;//下落的速度
    GameObject panelScore;//排行榜分数预制
    List<int> scoreList = new List<int>();//所有的分数
    List<int> timeList = new List<int>();//所有的时间
    public List<GameObject> obj = new List<GameObject>();
    GameObject scoreView;
    void Start()
    {
        scoreView = GameObject.Find("Scroll View");
        for (int index = 0; index < scoreView.transform.GetChild(0).GetChild(0).childCount; index++)
        {
            obj.Add(scoreView.transform.GetChild(0).GetChild(0).GetChild(index).gameObject);
        }
        player = GameObject.Find("Player");
        player.AddComponent<EggCollider>();
        panelScore = Resources.Load("GetEgg/ScorePanel") as GameObject;
        showScore = GameObject.Find("ShowScore").GetComponent<Text>();
        showLose = GameObject.Find("ShowLose").GetComponent<Text>();
        showClearing = GameObject.Find("ShowClearing");
        showScore.text = "Score:0";
        showLose.text = "Lose:0";
        Button reset = GameObject.Find("ResetButton").GetComponent<Button>();
        reset.GetComponentInChildren<Text>().text = "重试";
        reset.onClick.AddListener(delegate { ResetGame(); });
        Button exit = GameObject.Find("ExitButton").GetComponent<Button>();
        exit.GetComponentInChildren<Text>().text = "Exit";
        exit.onClick.AddListener(delegate { ExitGame(); });
        Button sv = GameObject.Find("ScoreViewButton").GetComponent<Button>();
        sv.GetComponentInChildren<Text>().text = "排行榜";
        sv.onClick.AddListener(delegate { ScoreListState(); });

        showClearing.SetActive(false);
        scoreView.SetActive(false);

        ResetGame();
    }
    float moveSpeed;//玩家的移动速度
    Vector3 move;//玩家将要移动的位置
    float minX = -14f;
    float maxX = 14f;
    void Update()
    {
    //这里是桶的移动
        move.x += Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;//获取水平移动
        if (move.x < minX)//出左边界则从右边界出现
            move.x = maxX;
        else if (move.x > maxX)//出右边界则从左边界出现
            move.x = minX;
        player.transform.position = move;//移动

   //这里是蛋的生成时机
        if (nextTime < oriSpeed)//计时
            nextTime += Time.deltaTime;
        else//到达下一次出现的时间
        {
            nextTime -= oriSpeed;//重新计时
            oriSpeed *= shift;//难度增加
            if (oriSpeed < 0.5f)
                oriSpeed = 0.5f;
            else if (oriSpeed > 3f)
                oriSpeed = 3f;//限制难度的最大最小值
            ObjectPool.GetObject();
        }
    }
    public void Clearing(bool isLose)//结算
    {
        if (isLose)
        {
            showLose.text = "Lose:" + ++lose;
            if (lose >= 10)
            {
                if (score > maxScore)
                {
                    maxScore = score;
                    showClearing.GetComponentInChildren<Text>().text = "不错哟,你又进步了";
                }
                else if (score < minScore)
                {
                    minScore = score;
                    showClearing.GetComponentInChildren<Text>().text = "又偷懒了吧你";
                }
                else
                {
                    showClearing.GetComponentInChildren<Text>().text = "怎么又挂了,我想静静...";
                }
                scoreList.Add(score);
                timeList.Add(Camera.main.GetComponent<TimeCount>().CurrentSecond);
                Time.timeScale = 0;
                showClearing.SetActive(true);
            }
        }
        else
            showScore.text = "Score:" + ++score;
    }
    void ResetGame()//初始化参数
    {
        lose = 0;
        score = 0;
        showLose.text = "Lose:" + lose;
        showScore.text = "Score:" + score;
        eggMoveSpeed = 10;
        shift = 0.98f;
        nextTime = 0;
        oriSpeed = 2;
        moveSpeed = 12;
        move = new Vector3(0, -30f, -20f);
        showClearing.SetActive(false);
        Camera.main.GetComponent<TimeCount>().ResetTime();
        Transform tran = GameObject.Find("ObjectPool").transform;
        for (int index = 0; index < tran.childCount; index++)
            tran.GetChild(index).gameObject.SetActive(false);
        Time.timeScale = 1;
    }
    void ExitGame()
    {
        Application.Quit();
    }
    bool isOpen = true;
    void ScoreListState()
    {
        if (isOpen)
        {
            Time.timeScale = 0;
            scoreList.Sort();
            Vector2 pos = new Vector2(0, 0);
            int shift = -35;
            for (int count = 0; count < 10; count++)
            {
                pos.y += shift;
                int mIndex = scoreList.Count - count - 1;
                obj[count].transform.GetChild(0).GetComponentInChildren<Text>().text = (count + 1).ToString();
                if (count < scoreList.Count)
                {
                    obj[count].SetActive(true);
                    obj[count].transform.GetChild(1).GetComponentInChildren<Text>().text = timeList[mIndex] + "秒钟接了" + scoreList[mIndex] + "个!";
                }
                else
                    obj[count].SetActive(false);
            }
        }
        else
        {
            Time.timeScale = 1;
        }
        scoreView.SetActive(isOpen);
        isOpen = !isOpen;
    }
}

结果

这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值