На сколько мне известно GPS API Unity3d не выдает getSpeed()
Я начал писать свой GPS плагин.
Файл Speedometer.java
Синтаксис:
Используется javascript
package com.zaqxs1995.speedometer;
import com.unity3d.player.UnityPlayerActivity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;
public class Speedometer extends UnityPlayerActivity {
private static final String TAG = "GPS_Unity";
public static Location currentLocation;
public static LocationManager myLocationManager;
static LocationListener networkLocationListener;
static LocationListener gpsLocationListener;
public static void startLocationListeners() {
gpsLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
currentLocation = location;
Log.i(TAG, "Getting Location over GPS " + currentLocation.toString());
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
};
networkLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
currentLocation = location;
Log.i(TAG,
"Getting Location over GPS" + currentLocation.toString());
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
};
myLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0, 0,
networkLocationListener);
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
gpsLocationListener);
}
@Override
protected void onCreate(Bundle myBundle) {
super.onCreate(myBundle);
myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
startLocationListeners();
}
@Override
protected void onResume() {
if (Config.DEBUG)
Log.d(TAG, "onResume");
super.onResume();
startLocationListeners();
}
@Override
protected void onPause()
{
myLocationManager.removeUpdates(networkLocationListener);
myLocationManager.removeUpdates(gpsLocationListener);
super.onPause();
}
@Override
protected void onStop() {
if (Config.DEBUG)
Log.d(TAG, "onStop");
myLocationManager.removeUpdates(networkLocationListener);
myLocationManager.removeUpdates(gpsLocationListener);
super.onStop();
}
public static String getSpeed()
{
if(currentLocation!=null)
return "" + currentLocation.getSpeed()*3.6;
else
return "Unknown";
}
}
import com.unity3d.player.UnityPlayerActivity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;
public class Speedometer extends UnityPlayerActivity {
private static final String TAG = "GPS_Unity";
public static Location currentLocation;
public static LocationManager myLocationManager;
static LocationListener networkLocationListener;
static LocationListener gpsLocationListener;
public static void startLocationListeners() {
gpsLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
currentLocation = location;
Log.i(TAG, "Getting Location over GPS " + currentLocation.toString());
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
};
networkLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
currentLocation = location;
Log.i(TAG,
"Getting Location over GPS" + currentLocation.toString());
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
};
myLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0, 0,
networkLocationListener);
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
gpsLocationListener);
}
@Override
protected void onCreate(Bundle myBundle) {
super.onCreate(myBundle);
myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
startLocationListeners();
}
@Override
protected void onResume() {
if (Config.DEBUG)
Log.d(TAG, "onResume");
super.onResume();
startLocationListeners();
}
@Override
protected void onPause()
{
myLocationManager.removeUpdates(networkLocationListener);
myLocationManager.removeUpdates(gpsLocationListener);
super.onPause();
}
@Override
protected void onStop() {
if (Config.DEBUG)
Log.d(TAG, "onStop");
myLocationManager.removeUpdates(networkLocationListener);
myLocationManager.removeUpdates(gpsLocationListener);
super.onStop();
}
public static String getSpeed()
{
if(currentLocation!=null)
return "" + currentLocation.getSpeed()*3.6;
else
return "Unknown";
}
}
Скомпилировал Jar в папку Assets\Plugins\Android
Создал GUIText, назвал его gps_output.
Создал GPSManager.cs в проекте и кинул его на камеру
Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
public class GPSManager : MonoBehaviour {
static string speedMessage;
AndroidJavaClass gpsActivityJavaClass;
void Start () {
AndroidJNI.AttachCurrentThread();
gpsActivityJavaClass = new AndroidJavaClass("com.zaqxs1995.speedometer.Speedometer");
}
void Update() {
speedMessage = gpsActivityJavaClass.CallStatic<string>("getSpeed");
float speed = 0;
if(speedMessage!="Unknown")
{
speed = float.Parse(speedMessage);
GameObject.Find("gps_output").GetComponent<GUIText>().text = speed + "км\ч";
}
else
{
GameObject.Find("gps_output").GetComponent<GUIText>().text = "Скорость не доступна.";
}
}
}
using System.Collections;
public class GPSManager : MonoBehaviour {
static string speedMessage;
AndroidJavaClass gpsActivityJavaClass;
void Start () {
AndroidJNI.AttachCurrentThread();
gpsActivityJavaClass = new AndroidJavaClass("com.zaqxs1995.speedometer.Speedometer");
}
void Update() {
speedMessage = gpsActivityJavaClass.CallStatic<string>("getSpeed");
float speed = 0;
if(speedMessage!="Unknown")
{
speed = float.Parse(speedMessage);
GameObject.Find("gps_output").GetComponent<GUIText>().text = speed + "км\ч";
}
else
{
GameObject.Find("gps_output").GetComponent<GUIText>().text = "Скорость не доступна.";
}
}
}
При открытии программы на телефоне она начинает искать спутники(мигает значок).
Но скорость в GUIText(gps_output) отправлять не хочет!
Вот что пишет в консоли:
NullReferenceException: Object reference not set to an instance of an object
GPSManager.Update () (at Assets/Location/GPSManager.cs:14)
А вот и сама 14 строчка в GPSManager.cs
Синтаксис:
Используется csharp
speedMessage = gpsActivityJavaClass.CallStatic<string>("getSpeed");
По моему я достаточно подробно объяснил Unity где экземпляр объекта... Но он его в упор видеть не хочет.
Есть у кого какие идеи?