如何在Unity中使用C#代码在移动设备上工作? [英] How can I use C# code in Unity to work on mobile?

查看:157
本文介绍了如何在Unity中使用C#代码在移动设备上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一段C#代码在Unity中的移动设备中工作.我在网络摄像头上进行了测试,效果很好.

I wrote a piece of C# code to work in mobile devices in Unity. I tested on a webcam, and it worked perfectly.

但是,当我将其构建为APK时,它无法在我的移动设备上运行,问题显然出在C#代码上.我不熟悉移动环境,不太确定C#代码是否可以由Android设备运行,但是我认为Unity可以解决这个问题.

However, when I build it as APK, it did not work on my mobile device and the problem is clearly the C# code. I am unfamiliar with the mobile environment and not quite sure that C# code can be run by Android devices, but I thought Unity would handle that.

我应该接受C#无法在移动设备上运行还是有解决方案吗?

Should I basically accept that C# cannot be run on mobile devices or is there a solution?

这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;


public class MenuScript : MonoBehaviour {

    Texture2D screenCap;
    Texture2D border;
    bool shot = false;
    public Button m_YourFirstButton;

    // Use this for initialization
    void Start () {

        screenCap = new Texture2D(300, 200, TextureFormat.RGB24, false); // 1
        border = new Texture2D(2, 2, TextureFormat.ARGB32, false); // 2
        border.Apply();
        m_YourFirstButton.onClick.AddListener(TaskOnClick);
    }

    // Update is called once per frame
    void Update () {
    }

    public void TaskOnClick()
    {
        StartCoroutine("Capture");
    }

    void OnGUI()
    {
        GUI.DrawTexture(new Rect(200, 100, 300, 2), border, ScaleMode.StretchToFill); // Top
        GUI.DrawTexture(new Rect(200, 300, 300, 2), border, ScaleMode.StretchToFill); // Bottom
        GUI.DrawTexture(new Rect(200, 100, 2, 200), border, ScaleMode.StretchToFill); // Left
        GUI.DrawTexture(new Rect(500, 100, 2, 200), border, ScaleMode.StretchToFill); // Right

        if (shot)
        {
            GUI.DrawTexture(new Rect(10, 10, 60, 40), screenCap, ScaleMode.StretchToFill);
        }
    }

    IEnumerator Capture()
    {
        yield return new WaitForEndOfFrame();
        screenCap.ReadPixels(new Rect(198, 98, 298, 198), 0, 0);
        screenCap.Apply();
        byte[] bytes = screenCap.EncodeToPNG();
        //Object.Destroy(screenCap);

        // For testing purposes, also write to a file in the project folder
        File.WriteAllBytes(Application.dataPath + "/SavedScreen.png", bytes);
        shot = true;
    }

}

推荐答案

我假设您已经在Unity中编写了此代码,并且可以在编辑器中使用.您对问题的陈述有点怪异,所以我不得不承担这些责任.

I am assuming you have written this code in Unity and it works in the editor. Your statement of the problem is kind of weird so I had to assume these.

之所以无法在移动设备上运行,很可能是因为您使用的是Application.dataPath.不应将其用于Android.最明显的原因是Android权限,它可能不允许您写入SD卡或内部存储设备.另外,您最有可能拥有UnauthorizedAccessException.

The reason why this is not working in mobile is most likely because you are using Application.dataPath. This should not be used for Android. The most obvious reason for this is Android permissions and it probably won't let you write to the SD card or internal storage. Also you most likely have a UnauthorizedAccessException.

要检查您在Android上的构建有什么问题,应使用logcat.当您的设备通过电缆连接到PC时,打开命令提示符访问包含adb.exe的文件夹,然后使用以下命令:adb.exe logcat -s Unity.

To check what is wrong with your build on Android, you should use logcat. While your device in connected to the PC via a cable, open a command prompt access to the folder that contains adb.exe and then use this command: adb.exe logcat -s Unity.

然后在移动设备上启动APK.这将向您显示所有Debug.Log执行和所有异常.然后,您可以调试代码.

Then start your APK on the mobile device. This will show you all your Debug.Log executions and all exceptions you have. Then you can debug your code.

有关日志文件的更多信息,您可以在此处

For more information about log files you can refer to here

祝你好运!

这篇关于如何在Unity中使用C#代码在移动设备上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆