首先,本功能使用UGUI的InputField实现。
正常来说,InputField是单击便可进行编辑的,所以开始关闭的它的可编辑功能,取消勾选“interactable”即可。
接下来便要实现双击功能,再次附上以前已写好的点击事件:
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class ClickEvent: MonoBehaviour,IPointerClickHandler
{
public UnityEvent singleClick = new UnityEvent();
public UnityEvent doubleClick = new UnityEvent();
public UnityEvent rightClick = new UnityEvent();
public UnityEvent middleClick = new UnityEvent();
public void OnPointerClick(PointerEventData eventData)
{
if (eventData.clickCount == 1 && eventData.button == PointerEventData.InputButton.Left)
{
singleClick.Invoke();
}
else if (eventData.clickCount == 2 && eventData.button == PointerEventData.InputButton.Left)
{
doubleClick.Invoke();
}
else if (eventData.button == PointerEventData.InputButton.Right)
{
rightClick.Invoke();
}
else if (eventData.button == PointerEventData.InputButton.Middle)
{
middleClick.Invoke();
}
}
}
然后给InputField添加双击事件,将可编辑状态打开,并使内容出于选中状态:
/// <summary>
/// 题目双击事件
/// </summary>
private void TitleDoubleClick()
{
gom.inp_ZhiDaoYuName.gameObject.AddComponent<ClickEvent>().doubleClick.AddListener(() => {
gom.inp_ZhiDaoYuName.interactable = true;
gom.inp_ZhiDaoYuName.Select();//使内容处于选中状态
});
}
添加完成编辑事件,关闭可编辑状态:
/// <summary>
/// 添加完成编辑事件
/// </summary>
private void AddEndEditEvent()
{
gom.inp_ZhiDaoYuName.onEndEdit.AddListener(TitleEndEdit);
}
/// <summary>
/// 题目完成编辑事件
/// </summary>
private void TitleEndEdit(string arg0)
{
gom.inp_ZhiDaoYuName.interactable = false;
if (gom.inp_ZhiDaoYuName.text == "")
gom.inp_ZhiDaoYuName.text = "默认值";
}