1.unity2018后支持安卓源文件在unity中编译 常用模板
package com.fgbbt.FileTransfer; //包名 改称自己的import android.os.Bundle;
import android.util.Log;import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;public class MainActivity extends UnityPlayerActivity
{@Overrideprotected void onCreate(Bundle savedInstanceState){Log.d("Unity","安卓 活动 初始化");super.onCreate(savedInstanceState);UnityPlayer.UnitySendMessage("Canvas","OnAndroidMsg","安卓消息");}@Overrideprotected void onDestroy(){super.onDestroy();}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class AndroidBehaviour : MonoBehaviour
{AndroidJavaObject activity;void Awake(){#if UNITY_ANDROID && !UNITY_EDITORAndroidJavaClass ja = new AndroidJavaClass("com.unity3d.player.UnityPlayer");activity = ja.GetStatic("currentActivity");#endif}void OnAndroidMsg(string msg){Debug.Log(msg);}}
2.部分Android系统无法接受udp广播 处理如下(原理未明)
import android.content.Context;
import android.net.wifi.WifiManager;WifiManager manager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock lock= manager.createMulticastLock("test wifi");//广播锁lock.acquire(); //获取锁 接受udp广播之前调用
lock.release(); //释放 用完后及时释放 否则可能出现问题 //所需权限
3.打包时‘android.enableR8’ 异常:
原因:gradle版本高于:3.6.0
解决方法:勾选“ProjectSettings/Player/PublishingSettings/CustomBaseGradleTemplate” 修改gradle版本
4.存储读写权限:ProjectSettings/Player/OtherSettings/WritePermission 选择 External(SDCard)
//AndroidManifest.xml 修改配置 无需添加读写权限