1

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

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.

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

Here is my piece of code:

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

}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

2

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.

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.

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.

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

Good Luck!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ali Kanat
  • 1,888
  • 3
  • 13
  • 23
0

The .NET library that Unity uses is an old subset of .NET. More information: Why does Unity use .NET 2.0 when Mono supports .NET 3.5?

You can change the .NET version that Unity uses, but then it won't work on mobile.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gazihan Alankus
  • 11,256
  • 7
  • 46
  • 57