简体   繁体   English

从Android调用C#Unity脚本

[英]Calling C# Unity Script from Android

I have been trying to communicate with my Unity C# script through my Android Application using "UnityPlayer.UnitySendMessage("Cube", "Test","HELLO") - where "Cube" is my Unity Object, "Test" is the name of the method present in the C# script it is using and "HELLO" is the message String I want to pass. 我一直在尝试使用“ UnityPlayer.UnitySendMessage(” Cube“,” Test“,” HELLO“)通过我的Android应用程序与Unity C#脚本进行通信-其中“ Cube”是我的Unity对象,“ Test”是它正在使用的C#脚本中存在的方法,“ HELLO”是我要传递的消息String。

This line is placed in my onClick function, like this: 此行放置在我的onClick函数中,如下所示:

      ImageButton right_button = (ImageButton) findViewById(R.id.right_arrow);
      right_button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            UnityPlayer.UnitySendMessage("Cube", "Test","HELLO");
       }
      });

But when I run my android application and as soon as I click this button, I get this error: 但是,当我运行我的android应用程序并单击该按钮时,出现此错误:

    09-11 14:22:37.526: E/AndroidRuntime(1330): java.lang.NoClassDefFoundError:         com.unity3d.player.UnityPlayer

I have included classes.jar in my build path also. 我在构建路径中也包含了classes.jar。 Is there anything else I am missing out? 还有其他我想念的吗?

Thanks in advance! 提前致谢!

Check if the class.jar file is in the Build Path of your Project. 检查class.jar文件是否在项目的“构建路径”中。 And if so, check if it is selected (check box is marked). 如果是这样,请检查是否已选中(已标记复选框)。

Tip: if you get ClassDefNotFound runtime errors (or similar), you might need to perform this extra step: 提示:如果收到ClassDefNotFound运行时错误(或类似错误),则可能需要执行以下额外步骤:

go to Project > Properties > Java Build Path -> Order and Export tab and check (tick) the classes.jar, Android xy and Android Dependencies items; 转到“项目”>“属性”>“ Java构建路径”->“顺序和导出”选项卡,然后检查(勾号)classes.jar,Android xy和Android Dependencies项目; then Apply and rebuild the probject. 然后应用并重建probject。

Happened to me the other time too. 另一天也发生在我身上。 I clean, re-link and rebuild my eclipse project. 我清理,重新链接并重建我的Eclipse项目。 Re-Export my jar file to unity and also restart Unity. 将我的jar文件重新导出为unity,然后重新启动Unity。 Build again and the error is gone. 重新生成,错误消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM