mergeInto(LibraryManager.library, {
  //弹出js原生输入框
  Prompt: function (name, message, defaultValue) {
    //if(UnityLoader.SystemInfo.mobile){
    const name_str = Pointer_stringify(name);
    const message_str = Pointer_stringify(message);
    const defaultValue_str = Pointer_stringify(defaultValue);
    const result = window.prompt(message_str, defaultValue_str);
    if (result === null) {
      SendMessage(name_str, 'OnPromptCancel');
    } else {
      SendMessage(name_str, 'OnPromptOk', result);
    }
    //}
  },

  //复制文本
  CopyText: function (text) {
    navigator.clipboard.writeText(Pointer_stringify(text));
  }
});

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class InputFieldMobileSupport : MonoBehaviour
#if UNITY_WEBGL && !UNITY_EDITOR
    ,IPointerClickHandler
#endif
    {
    public InputField Input;

    public string message;

    private void Awake()
    {
        Input = GetComponent<InputField>();
    }

#if UNITY_WEBGL && !UNITY_EDITOR
    [DllImport("__Internal")]
    private static extern void Prompt(string name, string message, string defaultValue);

    public void OnPointerClick(PointerEventData eventData)
    {
        var str =  Input.text;
        if (string.IsNullOrWhiteSpace(str))
        {
            str = message;
        }
        Prompt(name, message, str);
    }

    public void OnPromptOk(string message)
    {
        Input.text = message;
    }

    public void OnPromptCancel()
    {
        Input.text = "";
    }
#endif
}

最后修改:2023 年 06 月 16 日
如果觉得我的文章对你有用,请随意赞赏