gpt4 book ai didi

c# - Unity3D - Android 通信?

转载 作者:行者123 更新时间:2023-12-01 18:32:02 27 4
gpt4 key购买 nike

所以我编写了一个 Android 插件,可以在 Android 版 Unity3D 游戏中显示广告。我希望能够在 Unity C# 代码中关闭和打开广告。但是,没有任何工作。

如果我尝试调用 com/mycompany/mygame/MainActivity.java 中找到的以下函数:

public void hideAd() {
adView.setVisibility(View.INVISIBLE);
}

public void showAd() {
adView.setVisibility(View.VISIBLE);
}

从我的游戏中的脚本:

using UnityEngine;
using System.Collections;

public class callMyPlugin : MonoBehaviour {
AndroidJavaObject ajo;
float o = 0f;
bool ohBool = true;

void Start() {
ajo = new AndroidJavaObject("com.mycompany.mygame.MainActivity");
}

void Update () {
...
// Touch
foreach (Touch touch in Input.touches) {
// Other game code involving screen touch
...
if (touch.phase == TouchPhase.Began) {
// Call my function
float touchLoc = Camera.main.ScreenToWorldPoint(touch.position).y;
if (touchLoc > 0.0f) {
if (ohBool) {
ajo.Call("hideAd");
//hideAd();
ohBool = false;
} else {
ajo.Call("showAd");
//showAd();
ohBool = true;
}
}

}
}
// More game code
...
}
}

编辑:这是我的 list (如果有帮助的话):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mycompany.mygame">

<!-- User permissions -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATS"/>

<!-- Application -->
<application android:icon="@drawable/app_icon" android:label="@string/app_name">

<!-- Ad Banner -->
<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<!-- UnityPlayerAcivity -->
<activity android:name="com.mycompany.mygame.MainActivity"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
</activity>
<!-- UnityPlayerNativeActivity -->
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<meta-data android:name="android.app.lib_name" android:value="unity" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
</activity>
<!-- UnityPlayerProxyActivity -->
<activity android:name="com.unity3d.player.UnityPlayerProxyActivity"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
</activity>
<!-- VideoPlayer -->
<activity android:name="com.unity3d.player.VideoPlayer"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
</activity>

</application>
</manifest>

最佳答案

首先,您尝试调用静态方法,而 Java 方法是非静态的。

ajo.CallStatic("someMethod") 替换为 ajo.Call("someMethod")

其次,通过使用 new AndroidJavaObject("com.mycompany.mygame.MainActivity") 您将创建 MainActivity 的新实例。我们稍后会处理这个问题

AndroidJavaClass 与 AndroidJavaObject

AndroidJavaObject 将创建给定类的新实例。
AndroidJavaClass不会创建新实例,您可以访问静态方法和字段

调用当前 Activity 中的方法

AndroidJavaClass playerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
AndroidJavaObject curActivity = playerClass.GetStatic<AndroidJavaObject>("currentActivity");

curActivity.Call("showAd");

关于c# - Unity3D - Android 通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23745918/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com