将数据从unity发送到java android

时间:2017-07-19 02:04:25

标签: java c# android unity3d

我创建了一个统一项目,我试图将数据从c#发送到unity。在我的c#代码中我实现了这段代码:

AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
jo.Call("shareText","test","test");

它适用于我的活动:

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的自定义视图。

所以现在我有UnityActivity包含UnityView(这是一个java类),最后一个包含我的自定义视图(扩展linearLayout)和unityPlayer,并使用相同的代码,它没有工作:

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);
    }
}

任何人都知道它为什么不起作用?!

1 个答案:

答案 0 :(得分:0)

所以你的问题是你的自定义线性布局中的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);
}