77 lines
2.3 KiB
C#
77 lines
2.3 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace ZXL.Scripts.UI
|
|
{
|
|
public class RenameItem : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
|
|
{
|
|
Toggle parentToggle;
|
|
InputField inputField;
|
|
bool isEnter = false;
|
|
bool isCanRename = false;
|
|
private int clickCount = 0;
|
|
public Action<string> RenameAction;
|
|
public string oldName;
|
|
|
|
private void Awake()
|
|
{
|
|
parentToggle = transform.parent.GetComponent<Toggle>();
|
|
inputField = gameObject.GetComponent<InputField>();
|
|
inputField.image.raycastTarget = false;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (isCanRename)
|
|
{
|
|
if (!isEnter && (Input.GetMouseButtonDown(0) ||
|
|
Input.GetMouseButtonDown(1) ||
|
|
Input.GetMouseButtonDown(2)))
|
|
{
|
|
isCanRename = false;
|
|
parentToggle.isOn = true;
|
|
inputField.image.raycastTarget = false;
|
|
inputField.text = oldName;
|
|
Debug.Log("rename cancel !!");
|
|
}
|
|
|
|
if (Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetKeyDown(KeyCode.Return))
|
|
{
|
|
isCanRename = false;
|
|
inputField.image.raycastTarget = false;
|
|
RenameAction?.Invoke(inputField.text);
|
|
Debug.Log("rename finish !!");
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
isEnter = true;
|
|
clickCount = 0;
|
|
if (!isCanRename)
|
|
{
|
|
oldName = inputField.text;
|
|
}
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
isEnter = false;
|
|
clickCount = 0;
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
parentToggle.isOn = true;
|
|
clickCount++;
|
|
if (clickCount >= 2)
|
|
{
|
|
isCanRename = true;
|
|
inputField.image.raycastTarget = true;
|
|
}
|
|
}
|
|
}
|
|
} |