直接上的代码 这种办法虽然能实现 但是逻辑比较复杂,自己把我自己绕晕了
下面还有另一种写法
第一种写法
#region 模块信息
// **********************************************************************
// Copyright (C) 2018 Blazors
// Please contact me if you have any questions
// File Name: MButton.cs
// Author: 子龙
//QQ / Whacth 991959229 / taylorgege
// **********************************************************************
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System;
public class MButton : Button
{
private float M_Time = 2;
public float timmer = 10;
public bool isDown = false;
protected override void Awake()
{
isDown = true;
}
protected override void OnDestroy()
{
isDown = false;
}
protected override void OnEnable()
{
isDown = true;
}
protected override void OnDisable()
{
isDown = false;
}
private void Update()
{
if (isDown)
{
timmer += Time.deltaTime;
}
}
public override void OnPointerDown(PointerEventData data)
{
base.OnPointerDown(data);
if (timmer > M_Time)
{
call();
timmer = 0;
}
}
public UnityAction call;
}
调用方法的话使用
private MButton myButton;
private void Start()
{
myButton = GameObject.Find("Canvas/Button").GetComponent<MButton>();
//myButton.onClick.AddListener(TestOnClick);
myButton.call += TestOnClick;
}
public void TestOnClick()
{
Debug.Log("2");
}
第二种写法
#region 模块信息
// **********************************************************************
// Copyright (C) 2018 Blazors
// Please contact me if you have any questions
// File Name: MButton.cs
// Author: 子龙
//QQ / Whacth 991959229 / taylorgege
// **********************************************************************
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System;
public class MButton : Button
{
private const float M_Time = 2;
private float timmer = 0;
private bool isDown = false;
protected override void Awake() {
onClick.AddListener(OnClick);
}
private void Update()
{
if (isDown)
{
timmer += Time.deltaTime;
if (timmer >= M_Time)
{
timmer = 0;
interactable = true;
isDown = false;
}
}
}
public void OnClick()
{
interactable = false;
isDown = true;
}
}
第二种使用
public MButton myButton;
private void Start()
{
myButton.onClick.AddListener(TestOnClick);
}
public void TestOnClick()
{
Debug.Log("2");
}
}