从 Java 调用 Unity 函数 [英] Call Unity function from Java
问题描述
我是 unity 的新手:
I'm new to unity :
我在 Unity 中创建了一个简单的立方体并添加了一些纹理.我旋转立方体......移动相机......然后导出到android studio.当我运行时,一切都在 Unity 中.
I create in Unity a simple cube and put some texture over. I rotate the cube... move camera....then export to android studio. When I run everything looks like in Unity.
但我想从 android studio 代码(编程线)移动相机或立方体,我找不到任何方法来 .."findViewById" 或类似的能够找到我的立方体 :)
But I want to move camera or the cube from android studio code ( programming lines ) and I can not find any way to .."findViewById" or similar to be able to find my cube :)
我尝试制作一个 C# 文件(我只是在资产文件夹中创建一个)并放入:
I try to make a C# file ( I just crate one in assets folder ) and put :
public class test : MonoBehaviour {
public GameObject respawn;
void Start () {
Debug.Log("aaaaaaaaaaaaa1111111111111111");
if (respawn == null)
respawn = GameObject.FindWithTag("mamaie");
respawn.transform.Rotate(10f, 50f, 10f);
}
// Update is called once per frame
void Update () {
transform.Rotate(10f, 50f, 10f);
}
void LateUpdate()
{
transform.Rotate(10f, 50f, 10f);
}
}
那么……我如何从编程线控制我的立方体(统一设计并在 android studio 中导入)?
So... how can I control my cube ( designed in unity and imported in android studio ) from programming lines ?
推荐答案
您可以使用 UnityPlayer.UnitySendMessage
从 Java 调用 C# 函数.
You can call C# function from Java with UnityPlayer.UnitySendMessage
.
这是参数的样子:
UnityPlayer.UnitySendMessage("GameObjectName", "MethodName", "parameter to send");
为了访问这个函数,你必须从
目录到您的 Android Studio 项目中,然后在 Android Studio 项目中使用 import com.unity3d.player.UnityPlayer;
将其导入.
In order to have access to this function, you have to include classes.jar from the <UnityInstallDirectory>EditorDataPlaybackEnginesAndroidPlayerVariationsmonoReleaseClasses
directory into your Android Studio project then import it with import com.unity3d.player.UnityPlayer;
in the Android Studio Project.
您的 C# 代码:
bool rotate = false;
void startRotating()
{
rotate = true;
}
void stopRotating()
{
rotate = false;
}
void Update()
{
if (rotate)
transform.Rotate(10f, 50f, 10f);
}
<小时>
假设上面的脚本附加到名为Cube"的游戏对象上.
Let's assume that the script above is attached to GameObject called "Cube".
从 Java 开始轮换:
To start rotation from Java:
UnityPlayer.UnitySendMessage("Cube", "startRotating", null);
从 Java 停止旋转:
To stop rotation from Java:
UnityPlayer.UnitySendMessage("Cube", "stopRotating", null);
这篇关于从 Java 调用 Unity 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!