问题描述
我正在尝试更改长时间单击的精灵的图像。
我的下面的代码有效,除了在LongPress方法中,它更改了将此脚本分配为检查器组件的所有精灵的图像。此外,在长时间点击屏幕上的任意位置后,代码会触发,而不是将其自身仅隔离为带有脚本的精灵。
OnMouseDownAsButton可以解决很多问题,但是将我的代码包装在OnMouseDownAsButton函数中以隔离对撞机似乎可以抵消update()方法。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class LongClickButton : MonoBehaviour
{
public Sprite flagTexture;
public Sprite defaulttexture;
public float ClickDuration = 2;
public UnityEvent OnLongClick;
bool clicking = false;
float totalDownTime = 0;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
totalDownTime = 0;
clicking = true;
}
if (clicking && Input.GetMouseButton(0))
{
totalDownTime += Time.deltaTime;
if (totalDownTime >= ClickDuration)
{
clicking = false;
OnLongClick.Invoke();
}
}
if (clicking && Input.GetMouseButtonUp(0))
{
clicking = false;
}
}
public void LongPress()
{
if (GetComponent<SpriteRenderer>().sprite == flagTexture)
{
GetComponent<SpriteRenderer>().sprite = defaulttexture;
}
else if(GetComponent<SpriteRenderer>().sprite == defaulttexture)
GetComponent<SpriteRenderer>().sprite = flagTexture;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)