2016-03-01 17:28:50 wuyt2008 阅读数 10383

这里,用了一个比较偷懒的办法,直接用高德提供的android定位sdk,没有重新编译。好处是省事,坏处是,没法修改默认的定位模式。部分信息获取不到。

如果需要完整的功能,还是需要重新编译高德地图的sdk。


unity3d 5.3.2

高德android 定位 sdk 2.3


首先,注册高德sdk的用户,并且获取key。这里的key和编译环境的sha1相关。



新建一个unity项目



先把项目编译成android,记得Bundle Identifier必须和高德key里面的pakage一致。



用apk逆向工具把编译好的apk文件解开,


找到cert.rsa,用命令行获取调式用的sha1。官方只提供了android studio调式用的sha1获取的办法。

keytool -printcert -file cert.rsa


下载sdk包



把下载的jar和逆相出来的AndroidManifest.xml导入到unity工程的Plugins/Android目录下



修改AndroidManifest.xml文件


<?xml version="1.0" encoding="utf-8" standalone="no"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal" package="com.nsh.gpsar" platformBuildVersionCode="23" platformBuildVersionName="6.0-2438415">
  <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:xlargeScreens="true"/>
  <application android:banner="@drawable/app_banner" android:debuggable="false" android:icon="@drawable/app_icon" android:isGame="true" android:label="@string/app_name" android:theme="@style/UnityThemeSelector">
    <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="输入高德的key" />
    <!-- 定位需要的服务 -->
    <service android:name="com.amap.api.location.APSService" ></service>
    <activity android:configChanges="locale|fontScale|keyboard|keyboardHidden|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode" android:label="@string/app_name" android:launchMode="singleTask" android:name="com.unity3d.player.UnityPlayerActivity" android:screenOrientation="portrait">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
        <category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
      </intent-filter>
      <meta-data android:name="unityplayer.UnityActivity" android:value="true"/>
    </activity>
  </application>
  <uses-feature android:glEsVersion="0x20000"/>
  <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
  <uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false"/>
  <uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false"/>
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <uses-permission android:name="android.permission.WRITE_SETTINGS" />
</manifest>

做个简单的界面



添加类AmapEvent,将android的事件转化成unity的事件

using UnityEngine;
using System.Collections;

public class AmapEvent : AndroidJavaProxy {

	public AmapEvent ()
		: base ("com.amap.api.location.AMapLocationListener")
	{
	}

	void onLocationChanged (AndroidJavaObject amapLocation)
	{
		if (locationChanged != null) {
			locationChanged (amapLocation);
		}
	}

	public delegate void DelegateOnLocationChanged(AndroidJavaObject amap);
	public event DelegateOnLocationChanged locationChanged;
}

添加locationmange类,调用高德的sdk。定位时间和定位速度无法获取到,会报错。似乎在android studio里可以。

using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;

public class LocationManage : MonoBehaviour
{

	public Text txtLocation;
	public Text txtInfo;
	private AmapEvent amap;
	private AndroidJavaClass jcu;
	private AndroidJavaObject jou;
	private AndroidJavaObject mLocationClient;
	private AndroidJavaObject mLocationOption;

	public void StartLocation ()
	{
		try {
			txtInfo.text = "start location...";

			txtInfo.text = txtInfo.text + "\r\n";
			jcu = new AndroidJavaClass ("com.unity3d.player.UnityPlayer"); 
			jou = jcu.GetStatic<AndroidJavaObject> ("currentActivity");
			txtInfo.text = txtInfo.text + "currentActivity get...";

			txtInfo.text = txtInfo.text + "\r\n";
			mLocationClient = new AndroidJavaObject ("com.amap.api.location.AMapLocationClient", jou);
			txtInfo.text = txtInfo.text + "AMapLocationClient get...";

			txtInfo.text = txtInfo.text + "\r\n";
			mLocationOption = new AndroidJavaObject ("com.amap.api.location.AMapLocationClientOption");
			txtInfo.text = txtInfo.text + "AMapLocationClientOption get...";

			txtInfo.text = txtInfo.text + "\r\n";
			mLocationClient.Call ("setLocationOption", mLocationOption);
			txtInfo.text = txtInfo.text + "setLocationOption...";

			amap = new AmapEvent ();
			amap.locationChanged += OnLocationChanged;

			txtInfo.text = txtInfo.text + "\r\n";
			mLocationClient.Call ("setLocationListener", amap);
			txtInfo.text = txtInfo.text + "setLocationListener...";

			txtInfo.text = txtInfo.text + "\r\n";
			mLocationClient.Call ("startLocation");
			txtInfo.text = txtInfo.text + "startLocation...";

		} catch (Exception ex) {
			txtInfo.text = txtInfo.text + "\r\n";
			txtInfo.text = txtInfo.text + "--------------------";
			txtInfo.text = txtInfo.text + ex.Message;

			EndLocation ();
		}
	}

	public void EndLocation ()
	{
		if (amap != null) {
			amap.locationChanged -= OnLocationChanged;
		}

		if (mLocationClient != null) {
			mLocationClient.Call ("stopLocation");
			mLocationClient.Call ("onDestroy");
		}

		txtLocation.text = "";
	}

	private void OnLocationChanged (AndroidJavaObject amapLocation)
	{
		if (amapLocation != null) {
			if (amapLocation.Call<int> ("getErrorCode") == 0) {
				txtLocation.text = ">>success:";

				try {
					txtLocation.text = txtLocation.text + "\r\n>>定位结果来源:" + amapLocation.Call<int> ("getLocationType").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>纬度:" + amapLocation.Call<double> ("getLatitude").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>经度:" + amapLocation.Call<double> ("getLongitude").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>精度信息:" + amapLocation.Call<float> ("getAccuracy").ToString ();
					//txtLocation.text = txtLocation.text + "\r\n>>定位时间:" + amapLocation.Call<AndroidJavaObject> ("getTime").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>地址:" + amapLocation.Call<string> ("getAddress").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>国家:" + amapLocation.Call<string> ("getCountry").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>省:" + amapLocation.Call<string> ("getProvince").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>城市:" + amapLocation.Call<string> ("getCity").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>城区:" + amapLocation.Call<string> ("getDistrict").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>街道:" + amapLocation.Call<string> ("getStreet").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>门牌:" + amapLocation.Call<string> ("getStreetNum").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>城市编码:" + amapLocation.Call<string> ("getCityCode").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>地区编码:" + amapLocation.Call<string> ("getAdCode").ToString ();

					txtLocation.text = txtLocation.text + "\r\n>>海拔:" + amapLocation.Call<double> ("getAltitude").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>方向角:" + amapLocation.Call<float> ("getBearing").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>定位信息描述:" + amapLocation.Call<string> ("getLocationDetail").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>兴趣点:" + amapLocation.Call<string> ("getPoiName").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>提供者:" + amapLocation.Call<string> ("getProvider").ToString ();
					txtLocation.text = txtLocation.text + "\r\n>>卫星数量:" + amapLocation.Call<int> ("getSatellites").ToString ();
					//txtLocation.text = txtLocation.text + "\r\n>>当前速度:" + amapLocation.Call<string> ("getSpeed").ToString ();

				} catch (Exception ex) {
					txtLocation.text = txtLocation.text + "\r\n--------------ex-------------:";
					txtLocation.text = txtLocation.text + "\r\n" + ex.Message;
				}

			} else {
				txtLocation.text = ">>amaperror:";
				txtLocation.text = txtLocation.text + ">>getErrorCode:" + amapLocation.Call<int> ("getErrorCode").ToString ();
				txtLocation.text = txtLocation.text + ">>getErrorInfo:" + amapLocation.Call<string> ("getErrorInfo");
			}
		} else {
			txtInfo.text = "amaplocation is null.";
		}
	}

}

编译以后,运行,结果如下



源码及apk下载:http://download.csdn.net/detail/wuyt2008/9449732

2012-05-18 18:22:18 zhangao0086 阅读数 29406

有时候我们可能需要复制UILabel上的文本,或者UIImageView的图片,而UILabel和UIImageView默认是不响应Touch事件的,也无法复制,那么我们就需要自己实现一个可复制的UILabel。新添加一个类继承自UILabel:

@interface UICopyLabel : UILabel

@end
#import "UICopyLabel.h"

@implementation UICopyLabel

@end

为了能接收到事件(能成为第一响应者),我们需要覆盖一个方法:

-(BOOL)canBecomeFirstResponder{
    return YES;
}

还需要针对复制的操作覆盖两个方法:

//"反馈"关心的功能
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    return (action == @selector(copy:));
}
//针对于copy的实现
-(void)copy:(id)sender{
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    pboard.string = self.text;
}
有了以上三个方法,我们就能处理copy了,当然,在能接收到事件的情况下:
//UILabel默认是不接收事件的,我们需要自己添加touch事件
-(void)attachTapHandler{
    self.userInteractionEnabled = YES;  //用户交互的总开关
    UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    touch.numberOfTapsRequired = 2;
    [self addGestureRecognizer:touch];
    [touch release];
}
//绑定事件
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self attachTapHandler];
    }
    return self;
}
//同上
-(void)awakeFromNib{
    [super awakeFromNib];
    [self attachTapHandler];
}

我们已经可以接收到事件了!由于我在上方将tap数设为2,所以需要双击才能捕获,接下来,我们需要处理这个tap,以便让菜单栏弹出来:

-(void)handleTap:(UIGestureRecognizer*) recognizer{
    [self becomeFirstResponder];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setTargetRect:self.frame inView:self.superview];
    [menu setMenuVisible:YES animated:YES];
}

这样一来,一个可复制的UILabel就诞生了!它能处理接收点击、弹出菜单栏、处理copy,这是一个很普通的可复制控件。

-----------------------------------------猥琐的分界线-----------------------------------------

接下来我们做一个可复制的UIImageView,创建一个新的viewController,放两个imageView,默认显示不同的图:


然后把上面的代码直接拷过来,改三个地方:

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    return (action == @selector(copy:) || action == @selector(paste:));
}

-(void)copy:(id)sender{
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    pboard.image = self.image;
}

-(void)paste:(id)sender{
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    self.image = pboard.image;
}
-----------------------------------------猥琐的分界线-----------------------------------------
UIPasteboard不仅可以在应用程序内通信,还能在应用程序间通信,你应该已经见到过了,比如我复制一个url,然后打开safari,粘贴到地址栏去,而我们可以在应用程序间“悄悄”的通信、共享数据。

注:这里的“悄悄”只是说其他的应用程序不知道,而系统是允许的。

我们用模板Single View Application来创建两个简单的工程,一个叫PasteBoardWrite,另一个叫PasteBoardRead,界面分别如下:

     

在PasteBoardWrite里面点“写入”后把textField中的文本写入粘贴板,然后切换到PasteBoardRead的时候显示出来。如果我们的粘贴板只想给“自己人”用的话,就不能用系统的通用粘贴板,需要我们自己创建一个:

//需要提供一个唯一的名字,一般使用倒写的域名:com.mycompany.myapp.pboard
//后面的参数表示,如果不存在,是否创建一个
UIPasteboard *pb = [UIPasteboard pasteboardWithName:@"testBoard" create:YES];
使用这个粘贴板,我们可以把文本存进去,然后在另一个app里面读出来,一些常用的类型已经被设置为属性了:


除此之外,如果是能够转换成plist的数据类型(NSString, NSArray, NSDictionary, NSDate, NSNumber 和 NSURL),我们可以调用setValue:forPasteboardType:方法去存储数据,其他类型只能调用setData:forPasteboardType:方法(plist数据类型也可使用),类似于这样:

NSDictionary *dict = [NSDictionary dictionaryWithObject:textField.text forKey:@"content"];
NSData *dictData = [NSKeyedArchiver archivedDataWithRootObject:dict];
[pb setData:dictData forPasteboardType:@"myType"];
获取就类似于这样:

UIPasteboard *pb = [UIPasteboard pasteboardWithName:@"testBoard" create:YES];
NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithData:[pb dataForPasteboardType:@"myType"]];
caption.text = [dict objectForKey:@"content"];

上面提到了一个PasteboardType,这是一个统一类型标识符(Uniform Type Identifier  UTI),能帮助app获取自己能处理的数据。比如你只能处理文本的粘贴,那给你一个UIImage显然是无用的。你可以使用公用的UTI,也可以使用任意字符,苹果建议使用倒写的域名加上类型名:com.myCompany.myApp.myType。

用自己创建的粘贴板,能只在你的本地应用之间通信,有时候它能让你的用户体验变得更好,比如微信就是这么做的。

UPDATED:

iOS7修改了UIPasteboard的部分设计。
这此之前,你只要知道任何一个UIPasteboard的Name,就能访问其中的内容,而现在,只有在同一CFBundleIdentifier标识下的App才能共享内容,如com.mycompany.a和com.mycompany.b,它们的com.mycompany部分是一样的,就能共享。
2017-02-27 13:39:00 R15010198466 阅读数 16

在unity3d中,Renderer组件有两个属性:material和sharedMaterial,它们都可以用来获取Renderer的材质属性。但是它们之间却又很大的区别,下面通过示例来讲解一下。

准备工作:unity3d中新建一个空场景;创建两个cube,分别命名为Cube0、Cube1;在Project中新建一个材质球,取名M0,shader选择Unlit/Color,shader的Main Color属性设为白色,即(255,255,255,255);将M0分别赋值给Cube0、Cube1;新建一个脚本TestMaterial,将脚本拖到Cube0上。如下所示:

1、测试material

TestMaterial脚本的内容如下所示:

复制代码
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class TestMaterial : MonoBehaviour
 5 {
 6     Renderer thisRenderer;
 7     // Use this for initialization
 8     void Start()
 9     {
10         thisRenderer = GetComponent<Renderer>();
11         thisRenderer.material.color = Color.red;
12     }
13 }
复制代码

运行,效果如下图所示:

注意,只有Cube0的颜色改变,在mesh renderer中,材质球的名字是M0(Instance)。

2、测试sharedMaterial

代码如下:

复制代码
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class TestMaterial : MonoBehaviour
 5 {
 6     Renderer thisRenderer;
 7     // Use this for initialization
 8     void Start()
 9     {
10         thisRenderer = GetComponent<Renderer>();
11         thisRenderer.sharedMaterial.color = Color.red;
12     }
13 }
复制代码

运行,效果如下所示:

注意,Cube0、Cube1的颜色均发生改变,但是在mesh renderer中,材质球的名字还是M0。但是,此时在Project中的M0的Main Color属性发生了改变,如下所示:

 

总结:使用material属性的时候,每次都会new一份新的material作用与它,但不会改变本地工程中的材质material;sharedMaterial是共享材质,无论使用多少次,内存中都只会占用一份内存,但是会影响其他使用同一材质球的对象。所以,从效率上来说,sharedMaterial的效率更高。

 

重点来了:在实际使用的时候,我们可以在开始的时候使用material产生一个新的材质球作用于该renderer,然后之后的操作都使用sharedMaterial,这样,效率更高,而且不会影响其他使用同一个材质球的renderer。

最后,附上示例代码:

 

复制代码
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class TestMaterial : MonoBehaviour
 5 {
 6     Renderer thisRenderer;
 7     float delay = 3f;
 8     float changeColorTm;
 9     bool isChangeOnce;
10 
11     void Awake()
12     {
13         thisRenderer = GetComponent<Renderer>();
14         thisRenderer.material.color = Color.red;
15         changeColorTm = Time.time + delay;
16         isChangeOnce = false;
17     }
18 
19     void Update()
20     {
21         if (!isChangeOnce && Time.time >= changeColorTm)
22         {
23             isChangeOnce = true;
24             thisRenderer.sharedMaterial.color = Color.green;
25         }
26     }
27 }
复制代码

 

随意耕种,只为自己!

转载于:https://www.cnblogs.com/huilixieqi/p/6473568.html

2015-07-27 17:39:14 lins87 阅读数 0
在GFW和google拐弯抹角的文档的关照下,调试googlePlay的登录和充值花了好多时间,分享一下遇到的问题和解决方法

1.充值测试手机环境
如果能登录play store,也绑定了visa/master信用卡,但是只能见到免费游戏和APP,请寻找一个境外ip的vpn……嗯,ip是重点。
直到能看到收费项目,就可以开始测试了,在这之前,如果进行iab的初始化,大概会报
Error checking for billing v3 support



==============================================================


2.添加测试账号
登录play game services需要在这里的“添加测试人员”加入测试账号:
[img]http://dl2.iteye.com/upload/attachment/0111/0002/2bd2e274-a815-3248-88e9-57f214d0a05c.png[/img]
也可以直接加入一个群组:
[img]http://dl2.iteye.com/upload/attachment/0111/0004/30434993-32d4-3ada-aa04-77a040799fca.png[/img]


充值测试需要在这里的“许可测试”加:

[img]http://dl2.iteye.com/upload/attachment/0111/0006/4a30767d-9a9b-316c-afeb-c98cd7b7cb00.png[/img]


==============================================================


3.各种报错
================================
In-app billing error: Unable to buy item, Error response: 7:Item Already Owned

这个问题源于iab的流程:[url]http://developer.android.com/google/play/billing/api.html[/url]
一个商品支付完成后,需要被消费掉才能继续购买,所以参照例子在QueryInventoryFinishedListener和OnIabPurchaseFinishedListener适当的地方调用IabHelper的consumeAsync方法

================================
Error consuming response: 6:Error

加完consumeAsync我就遇到这个报错,消费失败,隐约记得这个报错是跟测试环境有关,但是搜半天搜不到解决办法,而且也无法继续测试支付了,就放着没管,时间是周五,结果周一回来打开手机居然就成功了,嗯,隐约记得上一次也好像是这样……好吧……google好像什么地方都有缓存,改个什么都得等半天才生效

================================
Can't start async operation (launchPurchaseFlow) because another async operation(launchPurchaseFlow) is in progress

这是我一次支付完成之后,想要进行第二次支付报的错,大概就是第一次的支付没有结束云云,其实就是IabHelper的状态没有改变,支付完成之后没有调flagEndAsync()这个方法,后来发现是粗心了,在例子的MainActivity上,有这么一段代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," + data);
if (mHelper == null) return;

// Pass on the activity result to the helper for handling
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
// not handled, so handle it ourselves (here's where you'd
// perform any handling of activity results not related to in-app
// billing...
super.onActivityResult(requestCode, resultCode, data);
}
else {
Log.d(TAG, "onActivityResult handled by IABUtil.");
}
}

里面的注释写得很明白了,需要在onActivityResult调一下IabHelper的handleActivityResult,里面就有一个操作
// end of async purchase operation that started on launchPurchaseFlow
flagEndAsync();


================================
Unable to retrieve application xxxxxx from network
BasicNetwork.performRequest: Unexpected response code 401 for https://www.googleapis.com/games/v1/players/me?language=zh_CN
There is no linked app associated with this client ID.


Application ID xxxxxxx is not associated with package xx.xx.xx. Check the application ID in your manifest.



这都是使用非测试人员登录googlePlay出现的报错,或者play services没有打开alpha/beta测试人员的开关,参照第2点加入测试人员即可


================================
Access Not Configured. Please use Google Developers Console to activate the API for your project.


去到 [url]https://console.developers.google.com/project[/url]
点开“APIs & auth”->"Credentials"->点击项目名

Signing-certificate fingerprint
填入keystore的SHA1的值


5. 参考文档
G+登录 [url]https://developers.google.com/identity/sign-in/android/[/url]
play game services [url]https://developers.google.com/games/services/android/quickstart[/url]
没有更多推荐了,返回首页