C#学习日记(用代码给按键添加功能)

今天用unity写一个石头剪刀布,界面如下:

 代码如下,简单实现了剪刀的按键功能

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

public class xindx : MonoBehaviour
{
    Image coumputerImg, playerImg;
    Text result;
    Button jiandao, shito, bu;
    public Image[] imgs;
    void Start()
    {
        coumputerImg = transform.GetChild(1).GetComponent<Image>();
        playerImg = transform.GetChild(3).GetComponent<Image>();
        result = transform.GetChild(4).GetComponent<Text>();
        jiandao = transform.GetChild(5).GetComponent<Button>();
        shito = transform.GetChild(6).GetComponent<Button>();
        bu = transform.GetChild(7).GetComponent<Button>();
        //GetChild(0)这个是获取组件位置,第0个组件,
        //GetComponent<Image>()这个是获取组件的类型


        //按钮事件
        jiandao.onClick.AddListener(Jiandao);
        shito.onClick.AddListener(Shito);
        bu.onClick.AddListener(Bu);
        //onClick这个是为按键添加对应的方法,就不需要去按键中拖放,括号中是要添加的方法名
        
    }
    void Jiandao() 
    {
        playerImg.sprite = imgs[0].sprite;
        int i = Random.Range(0,3);
        coumputerImg.sprite = imgs[i].sprite;
        //sprite这个是图片显示
    }
    void Shito() 
    {
        print("shito");
    }
    void Bu() 
    {
        print("bu");
    }

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

故渊9527

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值