我创建了一个unity项目,并且试图将数据从c#发送到unity。在我的C#代码上,我实现了以下代码:
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("shareText","test","test");
它在我的活动上适用于android:
public class UnityActivity extends AppCompatActivity {
public void shareText(String AppId,String PublisherID) {
Log.e("test","test");
Log.e("test",AppId);
Log.e("test",PublisherID);
}
}
但在另一种情况下,我创建了一个包含unityPlayer的自定义视图。
因此,现在我有了包含UnityView(这是一个Java类)的UnityActivity,最后一个包含了带有unityPlayer的自定义视图(扩展linearLayout),并且使用了相同的代码,但它不起作用:
public class CstUnityView extends LinearLayout {
private UnityPlayer mUnityPlayer;
public void shareText(String AppId,String PublisherID) {
Log.e("test","test");
Log.e("test",AppId);
Log.e("test",PublisherID);
}
}
任何人都知道为什么它不起作用吗?
最佳答案
因此,您的问题是,除非在活动中进行声明,否则自定义线性布局中的shareText()
不能统一调用。需要在您的活动中对其进行声明,以使您的统一Call
点燃该功能。
您可以先检查控制台日志以确保已调用它。
之后,您可以将从活动中收到的内容用于自定义布局视图。
在你的活动中
protected UnityPlayer mUnityPlayer;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//this creates unityplayer with context to your activity
mUnityPlayer = new UnityPlayer(this);
//this requests focus so that the currentactivity is set to your activity
//that is why shareText() can be called from your Unity
mUnityPlayer.requestFocus();
//call layout here
yourLinearlayout customlayout = new yourLinearLayout();
//do what you want to do with this customlayout
//....
}
public void shareText(String AppId,String PublisherID) {
Log.e("test","test");
Log.e("test",AppId);
Log.e("test",PublisherID);
}