一:Unity访问Android
1.Android方面所需要修改的文件
build.gradle
MainActivity
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xingame.bottlefilp3d">
<application
android:allowBackup="true"
android:supportsRtl="true">
<activity android:name="com.xingame.bottlefilp3d.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</application>
</manifest>
1.打包后将app-debug.arr包打开,外面的class.jar文件替换掉libs中的class.jar,复制这三个文件到unity中Plugins/Android目录下
需要解压class.jar文件,将文件中的BuildConfig文件删除,再重新打包。
将style删除
包名要与unity包名相同
2.Unity方面调用Android,具体请百度。
private void Awake()
{
AndroidJavaClass androidClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
m_androidObj = androidClass.GetStatic<AndroidJavaObject>("currentActivity");
}
void Start()
{
jo.Call("UnityCallAndroid");
}
二:Unity调用TopOn_Android开屏广告
1.首先AS集成TopOn Android SDK
使用AAR/JAR库文件集成splash。目前Jcenter的集成方式没有成功
2.编写Unity脚本
public class Splash : MonoBehaviour
{
private AndroidJavaObject m_androidObj = null;
AndroidJavaObject m_activity;
private void Awake()
{
AndroidJavaClass androidClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
m_androidObj = androidClass.GetStatic<AndroidJavaObject>("currentActivity");
}
void Start()
{
}
}
3.编写Android脚本
Android MainActivity代码
public class MainActivity extends UnityPlayerActivity {
String TopOnAppID = "appid";
String TopOnAppKey = "appkey";
String TopOnPlacementId = "splashid";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
//ATSDK.setNetworkLogDebug(true);//SDK日志功能,集成测试阶段建议开启,上线前必须关闭
//ATSDK.integrationChecking(getApplicationContext());//检查广告平台的集成状态
ATSDK.init(getApplicationContext(), TopOnAppID, TopOnAppKey);//初始化SDK
Intent intent = new Intent(MainActivity.this, SplashAdShowActivity.class);
intent.putExtra("placementId", TopOnPlacementId);
startActivity(intent);
}
}
创建Activity:SplashAdShowActivity。
public class SplashAdShowActivity extends Activity implements ATSplashExListener {
private static final String TAG = SplashAdShowActivity.class.getSimpleName();
ATSplashAd splashAd;
FrameLayout container;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_ad_show);
String placementId = getIntent().getStringExtra("placementId");
container = findViewById(R.id.splash_ad_container);
ViewGroup.LayoutParams layoutParams = container.getLayoutParams();
Configuration cf = getResources().getConfiguration();
int ori = cf.orientation;
/**You should set size to the layout param.**/
if (ori == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
layoutParams.width = (int) (getResources().getDisplayMetrics().widthPixels * 1);
layoutParams.height = getResources().getDisplayMetrics().heightPixels;
} else if (ori == Configuration.ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
layoutParams.width = getResources().getDisplayMetrics().widthPixels;
layoutParams.height = (int) (getResources().getDisplayMetrics().heightPixels * 1);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
layoutParams.width = getResources().getDisplayMetrics().widthPixels;
layoutParams.height = (int) (getResources().getDisplayMetrics().heightPixels * 1);
}
ATMediationRequestInfo atMediationRequestInfo = null;
//Mintegral
atMediationRequestInfo = new MintegralATRequestInfo("142414", "8960a39ef00317b2846f4a2d758bfd37", "280021", "440882");
atMediationRequestInfo.setAdSourceId("400631");
// atMediationRequestInfo = new AdmobATRequestInfo("ca-app-pub-9488501426181082~6354662111", "ca-app-pub-3940256099942544/1033173712", AdmobATRequestInfo.ORIENTATION_PORTRAIT);
// atMediationRequestInfo.setAdSourceId("145022");
splashAd = new ATSplashAd(this, placementId, atMediationRequestInfo, this, 5000);
Map<String, Object> localMap = new HashMap<>();
localMap.put(ATAdConst.KEY.AD_WIDTH, layoutParams.width);
localMap.put(ATAdConst.KEY.AD_HEIGHT, layoutParams.height);
splashAd.setLocalExtra(localMap);
if (splashAd.isAdReady()) {
splashAd.show(this, container);
} else {
splashAd.loadAd();
}
ATSplashAd.checkSplashDefaultConfigList(this, placementId, null);
}
@Override
public void onAdLoaded() {
splashAd.show(this, container);
}
@Override
public void onNoAdError(AdError adError) {
}
@Override
public void onAdShow(ATAdInfo entity) {
Log.i(TAG, "onAdShow:\n" + entity.toString());
}
@Override
public void onAdClick(ATAdInfo entity) {
Log.i(TAG, "onAdClick:\n" + entity.toString());
}
@Override
public void onAdDismiss(ATAdInfo entity) {
jumpToMainActivity();
}
boolean hasHandleJump = false;
public void jumpToMainActivity() {
if (!hasHandleJump) {
hasHandleJump = true;
finish();
//Toast.makeText(this, "start your MainActivity.", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (splashAd != null) {
splashAd.onDestory();
}
}
@Override
public void onDeeplinkCallback(ATAdInfo atAdInfo, boolean b) {
}
}
activity_splash_ad_show.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<FrameLayout
android:id="@+id/splash_ad_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
></FrameLayout>
</RelativeLayout>
为了能在unity中获取aar文件,在build.gradle文件中dependencies添加
compileOnly fileTree(dir: "libs", include: ["*.jar", "*.aar"])
compileOnly files('libs/classes.jar')
如图:
之后使用Unity调用,步骤入同一。