gpt4 book ai didi

android 应用程序在 unity3d 中无法正常工作

转载 作者:行者123 更新时间:2023-11-29 02:41:47 25 4
gpt4 key购买 nike

我从我的 friend 那里收到了一个统一项目,要统一完成它的工作,它是 unity3d 的 android 应用程序,所以在我打开该项目后,当我尝试从场景模式播放动画时,它不起作用,并显示此消息在控制台中:NullReferenceException:对象引用未设置为对象的实例
character.Update () (位于 Assets/character.cs:64)
UnityEditor.Toolbar:OnGUI()

我的统一版本是 (5.5.1f1),我的 friend 版本是 (5.5.0f3)。这是我的代码:

字符.cs

using System.Collections;
using System.Collections.Generic;

using UnityEngine;

public class character : MonoBehaviour {

// Use this for initialization
private Animator avatarAnimator;
public words passingtext;
private AndroidJavaObject VoicePlug = null;
private AndroidJavaObject activityContext = null;

void Start()
{
avatarAnimator = gameObject.GetComponentInChildren<Animator>();

if (VoicePlug == null)
{
using (AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
}

using (AndroidJavaClass pluginClass = new AndroidJavaClass("com.bas.MyProject.VoicePlug"))
{
if (pluginClass != null)
{
VoicePlug = pluginClass.CallStatic<AndroidJavaObject>("instance");
VoicePlug.Call("setContext", activityContext);
activityContext.Call("runOnUiThread", new AndroidJavaRunnable(() => {}));
}
}
}
}

Dictionary<string, int> signs = new Dictionary<string, int>()
{
{"start", 0},
{"hello", 1},


};


// Update is called once per frame
void Update () {
if (signs.ContainsKey(passingtext.gettext()))
{
avatarAnimator.SetInteger("stateNum", signs[passingtext.gettext()]);

}
else
{
//message for unstored signs
VoicePlug.Call("showMessage", "sorry I can't understand you");
passingtext.settext(" ");
}

}
}

最佳答案

您可以在 Android 设备上运行您的游戏。您有一个仅适用于 Android 的代码。请参阅:https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

确保编辑器没有错误,以便您可以在编辑器中测试您的应用程序以进行开发。使用

if (!Application.isEditor)

Application.isEditor 在 Unity 中运行时为 true,如果您的游戏在 android 或其他平台上运行则为 false。

如下所示编辑您的代码

    public class character : MonoBehaviour {

// Use this for initialization
private Animator avatarAnimator;
public words passingtext;
private AndroidJavaObject VoicePlug = null;
private AndroidJavaObject activityContext = null;

void Start()
{
avatarAnimator = gameObject.GetComponentInChildren<Animator>();
if (!Application.isEditor)
{
if (VoicePlug == null)
{
using (AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
}

using (AndroidJavaClass pluginClass = new AndroidJavaClass("com.bas.MyProject.VoicePlug"))
{
if (pluginClass != null)
{
VoicePlug = pluginClass.CallStatic<AndroidJavaObject>("instance");
VoicePlug.Call("setContext", activityContext);
activityContext.Call("runOnUiThread", new AndroidJavaRunnable(() => { }));
}
}
}
}
}

Dictionary<string, int> signs = new Dictionary<string, int>()
{
{"start", 0},
{"hello", 1},


};


// Update is called once per frame
void Update()
{
if (signs.ContainsKey(passingtext.gettext()))
{
avatarAnimator.SetInteger("stateNum", signs[passingtext.gettext()]);

}
else
{
// message for unstored signs
if (!Application.isEditor) {
VoicePlug.Call("showMessage", "sorry I can't understand you");
}
passingtext.settext(" ");
}

}
}

关于android 应用程序在 unity3d 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43574654/

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