Unity2d 垃圾分类的实现(按钮实现)

投放垃圾

走近垃圾桶时,会弹出对话框

进行垃圾分类,选择该垃圾桶应该投放的垃圾

离开垃圾桶时,对话框消失,并重置

Garbage.cs里有四个函数分别对应四种垃圾,预先给垃圾设定对应的函数用于计数。

难点分析:

一、获取当前按下的按钮并改变它的颜色

using UnityEngine.EventSystems;//使用事件系统,获取当前选择对象(按钮)

//通过EventSystem.current.currentSelectedGameObject.name获取当前选择对象的名字,再通过查找该对象对其操作

GameObject.Find(EventSystem.current.currentSelectedGameObject.name).GetComponent<Button>().colors = cb;

二、计数系统

主要使用了两个数组,注意这里数组的形式,为了便于其他脚本修改要加public static

    //垃圾总数据
    int[] Grouptotal = { 0, 6, 7, 2, 1 };
    //初始数据
    public static int [] Group = { 0, 0, 0, 0, 0 };

数据的计算方式

        //当前所需要分类的垃圾组别
        public static int curGroup=1;

        //实时刷新垃圾数据
        right = Group[curGroup];
        wrong = Group[1] + Group[2] + Group[3] + Group[4] - right;
        left = Grouptotal[curGroup] - right;

三、对话框的恢复

按了按钮过后,按钮会处于阴影+不激活的状态

在对话框关闭之前,应当使按钮恢复无阴影+激活的状态,以便下次使用

由于按钮的名字不能相同(相同将无法按下,不能正常工作),尝试使用转义字符批量处理按钮,但是无效

只能用最原始的办法一个一个的修改

UIManager代码(这里命名为Garbage)

挂载在一个空对象上,从而给垃圾对应的按钮添加点击函数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;//获取当前选择对象(按钮)

public class Garbage : MonoBehaviour
{
    //当前所需要分类的垃圾组别
    public static int curGroup=1;
    //四个垃圾桶依次为厨余垃圾、可回收物、有害垃圾、其他垃圾
    //垃圾总数据
    int[] Grouptotal = { 0, 6, 7, 2, 1 };
    //初始数据
    public static int [] Group = { 0, 0, 0, 0, 0 };

    //提示所需变量
    int right;
    int wrong;
    int left;

    //提示文本
    public Text righttext;//正确
    public Text wrongtext;//错误
    public Text lefttext;//剩余

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        //实时刷新垃圾数据
        right = Group[curGroup];
        wrong = Group[1] + Group[2] + Group[3] + Group[4] - right;
        righttext.text = "正确:" + right;
        wrongtext.text = "错误:" + wrong;
        left = Grouptotal[curGroup] - right;
        lefttext.text="剩余:"+left;
    }

    public void ChangeButtonColor()
    {
        //更改按钮颜色
        ColorBlock cb = new ColorBlock();
        cb.normalColor = Color.gray;
        cb.highlightedColor = Color.yellow;
        cb.pressedColor = Color.yellow;
        cb.disabledColor = Color.yellow;
        cb.colorMultiplier = 1;
        //对当前按下按钮进行操作
        GameObject.Find(EventSystem.current.currentSelectedGameObject.name).GetComponent<Button>().colors = cb;
    }

    public void CloseButton()//使按钮不能交互
    {
        GameObject.Find(EventSystem.current.currentSelectedGameObject.name).GetComponent<Button>().interactable = false;
    }
    public void GroupOne()//选定第一组垃圾(厨余垃圾)触发的函数
    {
        Group[1]++;//为第一组垃圾计数
        ChangeButtonColor();//按了之后改变颜色
        CloseButton();//按了一次后按钮失效
    }

    public void GroupTwo()//选定第二组垃圾(可回收垃圾)触发的函数
    {
        Group[2]++;
        ChangeButtonColor();
        CloseButton();
    }

    public void GroupThree()//选定第三组垃圾(有害垃圾)触发的函数
    {
        Group[3]++;
        ChangeButtonColor();
        CloseButton();
    }

    public void GroupFour()//选定第四组垃圾(其他垃圾)触发的函数
    {
        Group[4]++;
        ChangeButtonColor();
        CloseButton();
    }
    public static void Refrash()//刷新阴影
    {
        //恢复按钮颜色
        ColorBlock cb = new ColorBlock();
        cb.normalColor = Color.white;
        cb.highlightedColor = Color.white;
        cb.pressedColor = Color.white;
        cb.disabledColor = Color.white;
        cb.colorMultiplier = 1;
        
        //消除阴影
        GameObject.Find("Player/Canvas/garbage1").GetComponent<Button>().colors = cb;
        GameObject.Find("Player/Canvas/garbage2").GetComponent<Button>().colors = cb;
        GameObject.Find("Player/Canvas/garbage3").GetComponent<Button>().colors = cb;
        GameObject.Find("Player/Canvas/garbage4").GetComponent<Button>().colors = cb;
        GameObject.Find("Player/Canvas/garbage5").GetComponent<Button>().colors = cb;
        GameObject.Find("Player/Canvas/garbage6").GetComponent<Button>().colors = cb;
        GameObject.Find("Player/Canvas/garbage7").GetComponent<Button>().colors = cb;
        GameObject.Find("Player/Canvas/garbage8").GetComponent<Button>().colors = cb;
        GameObject.Find("Player/Canvas/garbage9").GetComponent<Button>().colors = cb;
        GameObject.Find("Player/Canvas/garbage10").GetComponent<Button>().colors = cb;
        GameObject.Find("Player/Canvas/garbage11").GetComponent<Button>().colors = cb;
        GameObject.Find("Player/Canvas/garbage12").GetComponent<Button>().colors = cb;
        GameObject.Find("Player/Canvas/garbage13").GetComponent<Button>().colors = cb;
        GameObject.Find("Player/Canvas/garbage14").GetComponent<Button>().colors = cb;
        GameObject.Find("Player/Canvas/garbage15").GetComponent<Button>().colors = cb;
        GameObject.Find("Player/Canvas/garbage16").GetComponent<Button>().colors = cb;

        //激活按钮交互
        GameObject.Find("Player/Canvas/garbage1").GetComponent<Button>().interactable = true;
        GameObject.Find("Player/Canvas/garbage2").GetComponent<Button>().interactable = true;
        GameObject.Find("Player/Canvas/garbage3").GetComponent<Button>().interactable = true;
        GameObject.Find("Player/Canvas/garbage4").GetComponent<Button>().interactable = true;
        GameObject.Find("Player/Canvas/garbage5").GetComponent<Button>().interactable = true;
        GameObject.Find("Player/Canvas/garbage6").GetComponent<Button>().interactable = true;
        GameObject.Find("Player/Canvas/garbage7").GetComponent<Button>().interactable = true;
        GameObject.Find("Player/Canvas/garbage8").GetComponent<Button>().interactable = true;
        GameObject.Find("Player/Canvas/garbage9").GetComponent<Button>().interactable = true;
        GameObject.Find("Player/Canvas/garbage10").GetComponent<Button>().interactable = true;
        GameObject.Find("Player/Canvas/garbage11").GetComponent<Button>().interactable = true;
        GameObject.Find("Player/Canvas/garbage12").GetComponent<Button>().interactable = true;
        GameObject.Find("Player/Canvas/garbage13").GetComponent<Button>().interactable = true;
        GameObject.Find("Player/Canvas/garbage14").GetComponent<Button>().interactable = true;
        GameObject.Find("Player/Canvas/garbage15").GetComponent<Button>().interactable = true;
        GameObject.Find("Player/Canvas/garbage16").GetComponent<Button>().interactable = true;

        /*这个方法行不通,只能用笨办法
        for (int i=5;i<=16;i++)
        {
            string way = "\"Player/Canvas/garbage"+i+"\"";
            Debug.Log(way);
            GameObject.Find(way).GetComponent<Button>().colors = cb;
        }*/
    }
}

玩家代码

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

public class FPlayreControl : MonoBehaviour
{
    public Rigidbody2D rb;//rbody
    public Animator anim;
    public float speed;
    public float jump;
    public LayerMask ground;
    public Collider2D coll;

    private bool isGround;//判断是否在地面
    public Text GroupText;
    public GameObject Frame;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Movement();
        Switchanim();
    }

    void Movement()
    {
        float hormove;
        hormove = Input.GetAxis("Horizontal");
        float facedirection = 1;//Input.GetAxisRaw("Horizontal");
        //角色移动
        if (hormove != 0)
        {
            rb.velocity = new Vector2(hormove * speed * Time.deltaTime, rb.velocity.y);
            anim.SetFloat("running", Mathf.Abs(facedirection));
        }
        if (facedirection != 0)
        {
            transform.localScale = new Vector3(facedirection, 1, 1);
        }

        //角色跳跃
        if (Input.GetButtonDown("Jump") && isGround)
        {
            rb.velocity = new Vector2(rb.velocity.x, jump * Time.deltaTime);
            isGround = false;//离地
            //anim.SetBool("Jumping", true);
        }
    }

    void Switchanim()
    {
        anim.SetBool("idle", false);
        if (anim.GetBool("Jumping"))
        {
            if (rb.velocity.y < 0)
            {
                anim.SetBool("Jumping", false);
            }
        }
        else if (coll.IsTouchingLayers(ground))
        {
            anim.SetBool("idle", true);
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log("人物靠近垃圾桶");
        
        if (collision.tag == "collection")
        {
            Destroy(collision.gameObject);
        }
        if(collision.tag=="Group1")
        {
            Frame.SetActive(true);
            FGarbage.curGroup=1;
            GroupText.text = "投放厨余垃圾";
        }
        if (collision.tag == "Group2")
        {
            Frame.SetActive(true);
            FGarbage.curGroup = 2;
            GroupText.text = "投放可回收垃圾";
        }
        if (collision.tag == "Group3")
        {
            Frame.SetActive(true);
            FGarbage.curGroup = 3;
            GroupText.text = "投放有害垃圾";
        }
        if (collision.tag == "Group4")
        {
            Frame.SetActive(true);
            FGarbage.curGroup = 4;
            GroupText.text = "投放其他垃圾";
        }
        
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        
        FGarbage.Refrash();//刷新阴影,要在对话框失效前执行
        Debug.Log("刷新");
        Frame.SetActive(false);
        FGarbage.Group[1] = 0;
        FGarbage.Group[2] = 0;
        FGarbage.Group[3] = 0;
        FGarbage.Group[4] = 0;
        
    }

    //F:脚下踩了东西,进入触发
    private void OnCollisionStay2D(Collision2D collision)
    {
        //如果踩的是地面
        if (collision.collider.tag == "Ground")
        {
            isGround = true;//触地
        }
    }
}

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 点我我会动 设计师:白松林 返回首页