点击物体一次执行一次事件,再次点击再执行一次事件(bool)

bool确实好用

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class On_Click : MonoBehaviour
{
    //定义显隐物体 定义布尔值
    public SpriteRenderer 画板;
    public bool b = true;

    // Use this for initialization
    void Start()
    {
        //初始画板默认不显示
        画板.transform.gameObject.SetActive(false);
    }

    private void OnMouseDown()
    {
        if (b)
        {
            //当点击鼠标,b=ture 画板显示  (!b 意为取b的反值)
            b = !b;
            画板.transform.gameObject.SetActive(true);
        }
        else
        {
            //当再次点击鼠标,b=false 画板不显示(!b 意为取b的反值)
            b = !b;
            画板.transform.gameObject.SetActive(false);
        }
    }

    private void Update()
    {
       
    }

}

 这是挂在物体上的,拖入画板

猜你喜欢

转载自blog.csdn.net/weixin_64625272/article/details/122362160