- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在构建一个基于位置的游戏,有点像 pokemon go。我在我的 android 手机上读取位置没有任何问题,但是当我在 unity 编辑器中开发时我无法获取任何位置数据,因为 Input.location.isEnabledByUser 在编辑器中为 false
可以模拟/硬编码一个位置,这样我就可以在不部署到手机的情况下进行尝试。
我试着像这样硬编码:
LocationInfo ReadLocation(){
#if UNITY_EDITOR
var location = new LocationInfo();
location.latitude = 59.000f;
location.longitude = 18.000f;
location.altitude= 0.0f;
location.horizontalAccuracy = 5.0f;
location.verticalAccuracy = 5.0f;
return location;
#elif
return Input.location.lastData;
#endif
}
但是该位置的所有属性都是只读的,所以我遇到了编译错误。有没有办法在编辑器中启用位置服务,或者对位置进行硬编码?
最佳答案
Is there a way to enable location service in the editor, or hardcode a location?
这就是为什么 Unity Remote 的原因之一。被制成。设置 Unity Remote,然后将您的移动设备连接到编辑器。您现在可以从编辑器中获取真实位置。
But all of the properties of the location are read only
如果你真的想开发一种模拟位置的方法,你必须放弃 Unity 的 LocationInfo
结构。制作您自己的自定义 LocationInfo
并将其命名为 LocationInfoExt
。 Ext = 扩展。
也为 LocationService
做同样的事情,然后将官方 LocationService
包装到您的自定义 LocationServiceExt
类中。您可以使用 LocationServiceExt
来决定是否应该使用 LocationInfoExt
模拟位置,或者不通过在内部使用 LocationInfo
来提供结果来模拟位置。
在下面的示例中,官方的 LocationService
、LocationInfo
和 LocationServiceStatus
类/结构/枚举被替换为 LocationServiceExt
、LocationInfoExt
和 LocationServiceStatusExt
。它们还实现了相同的功能和属性。唯一的区别是您可以将 true/false 传递给 LocationServiceExt
的构造函数,以便在编辑器中使用它。
LocationServiceExt
包装类:
创建一个名为 LocationServiceExt
的类,然后将下面的代码复制到其中:它具有原始 LocationService
类的所有功能和属性。
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LocationServiceExt
{
private LocationService realLocation;
private bool useMockLocation = false;
private LocationInfoExt mockedLastData;
private LocationServiceStatusExt mockedStatus;
private bool mIsEnabledByUser = false;
public LocationServiceExt(bool mockLocation = false)
{
this.useMockLocation = mockLocation;
if (mockLocation)
{
mIsEnabledByUser = true;
mockedLastData = getMockLocation();
}
else
{
realLocation = new LocationService();
}
}
public bool isEnabledByUser
{
//realLocation.isEnabledByUser seems to be failing on Android. Input.location.isEnabledByUser is the fix
get { return useMockLocation ? mIsEnabledByUser : Input.location.isEnabledByUser; }
set { mIsEnabledByUser = value; }
}
public LocationInfoExt lastData
{
get { return useMockLocation ? mockedLastData : getRealLocation(); }
set { mockedLastData = value; }
}
public LocationServiceStatusExt status
{
get { return useMockLocation ? mockedStatus : getRealStatus(); }
set { mockedStatus = value; }
}
public void Start()
{
if (useMockLocation)
{
mockedStatus = LocationServiceStatusExt.Running;
}
else
{
realLocation.Start();
}
}
public void Start(float desiredAccuracyInMeters)
{
if (useMockLocation)
{
mockedStatus = LocationServiceStatusExt.Running;
}
else
{
realLocation.Start(desiredAccuracyInMeters);
}
}
public void Start(float desiredAccuracyInMeters, float updateDistanceInMeters)
{
if (useMockLocation)
{
mockedStatus = LocationServiceStatusExt.Running;
}
else
{
realLocation.Start(desiredAccuracyInMeters, updateDistanceInMeters);
}
}
public void Stop()
{
if (useMockLocation)
{
mockedStatus = LocationServiceStatusExt.Stopped;
}
else
{
realLocation.Stop();
}
}
//Predefined Location. You always override this by overriding lastData from another class
private LocationInfoExt getMockLocation()
{
LocationInfoExt location = new LocationInfoExt();
location.latitude = 59.000f;
location.longitude = 18.000f;
location.altitude = 0.0f;
location.horizontalAccuracy = 5.0f;
location.verticalAccuracy = 5.0f;
location.timestamp = 0f;
return location;
}
private LocationInfoExt getRealLocation()
{
if (realLocation == null)
return new LocationInfoExt();
LocationInfo realLoc = realLocation.lastData;
LocationInfoExt location = new LocationInfoExt();
location.latitude = realLoc.latitude;
location.longitude = realLoc.longitude;
location.altitude = realLoc.altitude;
location.horizontalAccuracy = realLoc.horizontalAccuracy;
location.verticalAccuracy = realLoc.verticalAccuracy;
location.timestamp = realLoc.timestamp;
return location;
}
private LocationServiceStatusExt getRealStatus()
{
LocationServiceStatus realStatus = realLocation.status;
LocationServiceStatusExt stats = LocationServiceStatusExt.Stopped;
if (realStatus == LocationServiceStatus.Stopped)
stats = LocationServiceStatusExt.Stopped;
if (realStatus == LocationServiceStatus.Initializing)
stats = LocationServiceStatusExt.Initializing;
if (realStatus == LocationServiceStatus.Running)
stats = LocationServiceStatusExt.Running;
if (realStatus == LocationServiceStatus.Failed)
stats = LocationServiceStatusExt.Failed;
return stats;
}
}
public struct LocationInfoExt
{
public float altitude { get; set; }
public float horizontalAccuracy { get; set; }
public float latitude { get; set; }
public float longitude { get; set; }
public double timestamp { get; set; }
public float verticalAccuracy { get; set; }
}
public enum LocationServiceStatusExt
{
Stopped = 0,
Initializing = 1,
Running = 2,
Failed = 3,
}
用法:
创建模拟位置
LocationServiceExt locationServiceExt = new LocationServiceExt(true);
创建真实位置
LocationServiceExt locationServiceExt = new LocationServiceExt(false);
稍后修改位置
LocationInfoExt locInfo = new LocationInfoExt();
locInfo.latitude = 59.000f;
locInfo.longitude = 18.000f;
locInfo.altitude = -3.0f; //0.0f;
locInfo.horizontalAccuracy = 5.0f;
locInfo.verticalAccuracy = 5.0f;
locationServiceExt.lastData = locInfo; //Apply the location change
来自 Unity Doc 的完整移植工作示例.
public Text text;
IEnumerator StartGPS()
{
text.text = "Starting";
//Pass true to use mocked Location. Pass false or don't pass anything to use real location
LocationServiceExt locationServiceExt = new LocationServiceExt(true);
LocationInfoExt locInfo = new LocationInfoExt();
locInfo.latitude = 59.000f;
locInfo.longitude = 18.000f;
locInfo.altitude = -3.0f; //0.0f;
locInfo.horizontalAccuracy = 5.0f;
locInfo.verticalAccuracy = 5.0f;
locationServiceExt.lastData = locInfo;
// First, check if user has location service enabled
if (!locationServiceExt.isEnabledByUser)
{
text.text = "Not Enabled";
yield break;
}
else
{
text.text = "Enabled!";
}
// Start service before querying location
locationServiceExt.Start();
// Wait until service initializes
int maxWait = 20;
while (locationServiceExt.status == LocationServiceStatusExt.Initializing && maxWait > 0)
{
text.text = "Timer: " + maxWait;
yield return new WaitForSeconds(1);
maxWait--;
}
// Service didn't initialize in 20 seconds
if (maxWait < 1)
{
print("Timed out");
text.text = "Timed out";
yield break;
}
// Connection has failed
if (locationServiceExt.status == LocationServiceStatusExt.Failed)
{
print("Unable to determine device location");
text.text = "Unable to determine device location";
yield break;
}
else
{
// Access granted and location value could be retrieved
string location = "Location: " + locationServiceExt.lastData.latitude + " "
+ locationServiceExt.lastData.longitude + " " + locationServiceExt.lastData.altitude
+ " " + locationServiceExt.lastData.horizontalAccuracy + " " + locationServiceExt.lastData.timestamp;
Debug.Log(location);
text.text = location;
}
// Stop service if there is no need to query location updates continuously
locationServiceExt.Stop();
}
关于c# - 如何在统一编辑器中使用 Input.Location 和模拟位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38828748/
我正在尝试在 python 中编写正则表达式来查找目录路径:我的文本如下所示: text = "The public disk is: \\\\diskA\\FolderB\\SubFolderC\\
我想写一个LocationListener,它把最近最精确的位置作为它的位置。我打算在我的 LocationListener 中使用此代码: @Override public void
我想建立一个有光泽和 plotly 的交互式图表。 Shiny 有一个内置功能来获取有关用户交互的信息。比如:input$plot_click、input$plot_dblclick、input$pl
我正在使用 MobileFirst 提供的 WL.Device.Geo.acquirePosition(onGeoLocationSuccess, onGeoLocationFailure, opti
我最近开始使用 ionic 框架,它里面有 angular js。为了在屏幕之间导航,我使用了 $location.path 并且效果很好。但是,在我下载的一个示例中,我看到 $state.go 被用
谁能解释一下这种行为?我使用 history.pushState(null, null, '#test'); 推送历史状态。然后,当尝试使用console.log(window.location.ha
这里是相关代码: https://www.facebook.com/sharer/sharer.php?u={{$location.absUrl()}} https://www.facebook.c
这两个重定向之间有什么区别?我有一个应用程序,当我使用时,它可以在 chrome 和 android 4 上正常工作,但在 android 2.x.x 上不能正常工作 document.locatio
JavaScript 的区别是什么 window.location.href = window.location.href 和 window.location.reload() 功能? 最佳答案 如果
有什么区别 window.location.href="http://example.com"; window.location.replace("http://example.com"); wind
JavaScript 的区别是什么 window.location.href = window.location.href 和 window.location.reload() 功能? 最佳答案 如果
以下 3 个指令之间有区别吗? location ~* \.(png)$ { expires max; log_not_found off; } location ~ \.(png)$ {
位于正文末尾之前的以下脚本在 Internet Explorer 和 Chrome(以及任何其他浏览器)中都会被调用。但重定向到指定的 URL 仅发生在 IE 中。我还尝试了 window.locat
我正在使用 Angular ngRouter。我需要更改 url 路径以及搜索参数。我知道 $location.path 和 $location.search,但是有没有一个函数可以同时设置它们? 最
在angularjs中用$location和window.location哪个更好。 例如,我们可以使用$location.path() 或window.location.href 来完成同样的工作。
我在我的网站上使用上述 2 个命令。似乎它们对 95% 访问它应该触发的页面的人有效,但对其他人则不会。 有谁知道是否可以完全阻止这些 javascript 命令?我真的很头疼为什么它们有时不起作用。
这是我无法弄清楚的另一个错误。 我有这个类ExtendedLocation extends Location实例化时抛出 ClassCastExceptioncurrentGpsLocation =
我一直在尝试简单地将一个包含两个变量(一个字符串和一个位置)的类推送和读取到 firebase,但我一直收到此错误。 **com.google.firebase.database.DatabaseEx
我注意到 iPhone 上的“常用位置”似乎比监控 iOS 访问的应用程序 (https://developer.apple.com/reference/corelocation/clvisit) 使
在我的 javascript 代码中,在某些时候,我需要刷新窗口(用户上传了新图片但在页面中仍然可以看到它) 我想知道为什么 location.href = location.href 不刷新窗口?
我是一名优秀的程序员,十分优秀!