我正在统一构建一个基于位置的游戏作为 WebGL 构建。在我添加脚本更改之前,该文件正在构建,但位置输入在站点上不起作用(托管在 itch.io 上)。然后,我使用 chatgpt 添加了一个 javascript 文件并对位置服务进行了一些更改(位置输入脚本是从 Mapbox api 导入的)。现在文件本身尚未构建。
javascript 文件:
var EXPORTED_FUNCTIONS = ['_requestWebGLLocation'];
mergeInto(LibraryManager.library, {
requestWebGLLocation: function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function(position) {
unityInstance.SendMessage('LocationProviderFactory',
'OnLocationReceived',
position.coords.latitude + ',' +
position.coords.longitude);
},
function(error) {
console.error('Error obtaining location: ', error);
}
);
} else {
console.error('Geolocation is not supported by this browser.');
}
}
})
位置输入代码:
#if !UNITY_EDITOR
#define NOT_UNITY_EDITOR
#endif
namespace Mapbox.Unity.Location
{
using UnityEngine;
using Mapbox.Unity.Map;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
/// <summary>
/// Singleton factory to allow easy access to various LocationProviders.
/// This is meant to be attached to a game object.
/// </summary>
public class LocationProviderFactory : MonoBehaviour
{
[SerializeField]
public AbstractMap mapManager;
[SerializeField]
[Tooltip("Provider using Unity's builtin 'Input.Location' service")]
AbstractLocationProvider _deviceLocationProviderUnity;
[SerializeField]
[Tooltip("Custom native Android location provider. If this is not set above
provider is used")]
DeviceLocationProviderAndroidNative _deviceLocationProviderAndroid;
[SerializeField]
AbstractLocationProvider _editorLocationProvider;
[SerializeField]
AbstractLocationProvider _transformLocationProvider;
[SerializeField]
bool _dontDestroyOnLoad;
[DllImport("__Internal")]
private static extern void requestWebGLLocation();
/// <summary>
/// The singleton instance of this factory.
/// </summary>
private static LocationProviderFactory _instance;
public static LocationProviderFactory Instance
{
get
{
return _instance;
}
private set
{
_instance = value;
}
}
ILocationProvider _defaultLocationProvider;
/// <summary>
/// The default location provider.
/// Outside of the editor, this will be a <see
cref="T:Mapbox.Unity.Location.DeviceLocationProvider"/>.
/// In the Unity editor, this will be an <see
cref="T:Mapbox.Unity.Location.EditorLocationProvider"/>
/// </summary>
/// <example>
/// Fetch location to set a transform's position:
/// <code>
/// void Update()
/// {
/// var locationProvider =
LocationProviderFactory.Instance.DefaultLocationProvider;
/// transform.position =
Conversions.GeoToWorldPosition(locationProvider.Location,
///
MapController.ReferenceTileRect.Center,
///
MapController.WorldScaleFactor).ToVector3xz();
/// }
/// </code>
/// </example>
public ILocationProvider DefaultLocationProvider
{
get
{
return _defaultLocationProvider;
}
set
{
_defaultLocationProvider = value;
}
}
/// <summary>
/// Returns the serialized <see
cref="T:Mapbox.Unity.Location.TransformLocationProvider"/>.
/// </summary>
public ILocationProvider TransformLocationProvider
{
get
{
return _transformLocationProvider;
}
}
/// <summary>
/// Returns the serialized <see
cref="T:Mapbox.Unity.Location.EditorLocationProvider"/>.
/// </summary>
public ILocationProvider EditorLocationProvider
{
get
{
return _editorLocationProvider;
}
}
/// <summary>
/// Returns the serialized <see
cref="T:Mapbox.Unity.Location.DeviceLocationProvider"/>
/// </summary>
public ILocationProvider DeviceLocationProvider
{
get
{
return _deviceLocationProviderUnity;
}
}
/// <summary>
/// Create singleton instance and inject the DefaultLocationProvider upon
Initialization of this component.
/// </summary>
protected virtual void Awake()
{
if (Instance != null)
{
DestroyImmediate(gameObject);
return;
}
Instance = this;
if (_dontDestroyOnLoad)
{
DontDestroyOnLoad(gameObject);
}
InjectEditorLocationProvider();
InjectDeviceLocationProvider();
}
/// <summary>
/// Injects the editor location provider.
/// Depending on the platform, this method and calls to it will be stripped
during compile.
/// </summary>
[System.Diagnostics.Conditional("UNITY_EDITOR")]
void InjectEditorLocationProvider()
{
Debug.LogFormat("LocationProviderFactory: Injected EDITOR Location Provider
- {0}", _editorLocationProvider.GetType());
DefaultLocationProvider = _editorLocationProvider;
}
/// <summary>
/// Injects the device location provider.
/// Depending on the platform, this method and calls to it will be stripped
during compile.
/// </summary>
[System.Diagnostics.Conditional("NOT_UNITY_EDITOR")]
void InjectDeviceLocationProvider()
{
if (Application.platform == RuntimePlatform.WebGLPlayer)
{
Debug.Log("Using WebGL Geolocation.");
requestWebGLLocation();
}
else
{
int AndroidApiVersion = 0;
var regex = new Regex(@"(?<=API-)-?\d+");
Match match = regex.Match(SystemInfo.operatingSystem);
if (match.Success) { int.TryParse(match.Groups[0].Value, out
AndroidApiVersion); }
Debug.LogFormat("{0} => API version: {1}", SystemInfo.operatingSystem,
AndroidApiVersion);
if (Application.platform == RuntimePlatform.Android &&
AndroidApiVersion >= 24)
{
DefaultLocationProvider = _deviceLocationProviderAndroid;
}
else
{
DefaultLocationProvider = _deviceLocationProviderUnity;
}
}
}
public void OnLocationReceived(string locationData)
{
string[] coordinates = locationData.Split(',');
if (coordinates.Length == 2)
{
float latitude = float.Parse(coordinates[0]);
float longitude = float.Parse(coordinates[1]);
Debug.Log($"WebGL Location Received: Latitude: {latitude}, Longitude:
{longitude}");
// You can further process this location data as required
}
}
}
}
构建错误:
Library\Bee rtifacts\WebGL uild\debug_WebGL_wasm uild.js: 未定义符号:requestWebGLLocation(由顶级引用 编译的 C/C++ 代码) UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
Library\Bee rtifacts\WebGL uild\debug_WebGL_wasm uild.js: 由于之前的错误而中止编译 UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
构建 Library\Bee rtifacts\WebGL uild\debug_WebGL_wasm uild.js 失败,输出: 错误:未定义符号:requestWebGLLocation(由顶级编译的 C/C++ 代码引用) 警告:与
-s LLD_REPORT_UNDEFINED
链接以获取有关未定义符号的更多信息
警告:要禁用未定义符号的错误,请使用 -s ERROR_ON_UNDEFINED_SYMBOLS=0
警告:如果 _requestWebGLLocation 来自系统库,则可能需要将其添加到 EXPORTED_FUNCTIONS
错误:由于之前的错误而中止编译
emcc: 错误: '"C:/Program Files/Unity/Hub/Editor/2022.3.46f1/Editor/Data/PlaybackEngines/WebGLSupport/BuildTools/Emscripten/node/node.exe" "C:\Program文件\Unity\Hub\Editor�2.3.46f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\Emscripten mscripten\sr