Summary of Unity3d Android development problems

After developing a Unity-based Android application for half a year, the project is almost finished. I encountered many pitfalls during the development process, but Unity's technical support is almost all in the English forum. Direct Baidu, it is really difficult to solve the problem. This article makes a summary of the development experience in the past six months, hoping to help other developers.

https://forum.unity.com/This  is the official forum of Unity. Personally, I think it is a good way to solve technical problems, but the reply is not very stable, depending on the situation, and the network access is not very stable. Let me start with my question above:

1. Gradle build failed 

Original post address: https://forum.unity.com/threads/gradle-build-failed.647581/#post-4910888

The error message is:

stderr[
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'gradleOut'.
> Failed to find Build Tools revision 29.0.0

This error message is actually that Unity chose a version of Android BuildToolsVersion that does not exist. This problem arises for two reasons:

a. Before the problem occurred, I had been using Android 28 on my PC, and the target API Level in the Player settings was also Android 9 (28). But suddenly Android Studio downloaded the "29.0.0-rc1" test version SDK for me one day (Unity uses the SDK downloaded by Android Studio). From then on, when Unity was in Gradle Build, it did not directly select the Android 28 version according to the API Level in my settings, but chose the latest version of BuildToolsVersion 29.0.0-rc1. This may be due to BuildToolsVersion always choose the latest.

b. Even though Unity chooses the 29.0.0-rc1 BuildToolsVersion version, when it writes the Gradle file, it only writes 29.0.0. Therefore, the BuildToolsVersion version 29.0.0 cannot be found in the final Gradle Build .

Solution:

1. Use Unity to download the SDK yourself, and choose to use the SDK path in the project when importing the project in Android Studio, that is, the SDK path of Unity.

2. Delete the 29.0.0-rc1 version in the Android SDK directory.

3. Use Unity to export the Android Studio project, and then change buildToolsVersion '29.0.0' to the one you want to use in Build.gradle.

 

2. Loading .XML files in StreamingAssets may result in parsing errors

Original post address: https://forum.unity.com/threads/how-to-load-a-xml-file-in-android.662320/

First of all, the resources in StreamingAssets are compressed files that can only be read. So you need to use UnityWebRequest to read, you can't directly use C# file stream to open. Otherwise, the simulation in the editor is fine, but it cannot be used on the device.

The error in parsing the .XML file is as follows: there is no problem in simulating loading in the editor, and there is no problem in opening it on the device. The read string looks normal, but an error occurs during deserialization. 

The reason is due to the BOM of the .XML file. BOM is a marker data at the beginning of the file, marked in byte order, and occupies three bytes: "EF BB BF". What follows these three bytes is the deserialized content. If the BOM is read on Android, it needs to be skipped, and the string after the BOM is deserialized.

Solution:

1. Do not bring BOM when generating the .XML file;

2. When reading, first judge whether the first three bytes are BOM, and if so, deserialize from the back of the BOM.

 

3. Image tearing problem when Unity2018.x is upgraded to Unity2019.1.0.f2

Original post address: https://forum.unity.com/threads/the-images-become-weird-wrong-scale-since-i-upgraded-my-project-to-2019-1-0f2.670078/

upload_2019-4-30_10-59-25.png

As shown in the figure above, the left and right images use the same Sprite, but the display effects are completely different. I also confirmed that the image on the right is not stretched or compressed, and the size is the same as the resolution of the Sprite. I feel that this problem should only exist during the upgrade process, because it never happened before the upgrade.

Solution:

Replace Image with RawImage.

 

4. The click event caused by the touch screen is abnormal

Original post address: https://forum.unity.com/threads/i-have-a-tough-problem-with-screen-touch.710951/

Phenomenon: Since we are using an infrared touch frame, for some reason, when the user clicks, Unity will receive two click events at the same time. This can cause serious bugs in some resource release operations. This is a very hidden problem, and the error symptoms caused by different codes are also different.

Solution:

When clicked, change the function to an unresponsive state. Respond to the function after a delay for a period of time (such as 1ms).

 

5. Precautions for loading Video Clip located in AssetBundle or Addressables

Original post address: https://forum.unity.com/threads/how-to-disable-caching-compressionenabled-when-caching-a-videoclip-included-addressable-bundle.740708/

First of all, whether it is AssetBundle or Addressables, the resource bundle where the Video Clip is located must be uncompressed. Otherwise you will probably get this error message:

AndroidVideoMedia::OpenExtractor could not translate archive:/CAB-5b4cd0a92d354858f98b1392dd686137/CAB-5b4cd0a92d354858f98b1392dd686137.resource to local file. Make sure file exists, is on disk (not in memory) and not compressed.
AndroidVideoMedia: Error opening extractor: -10004

Don't doubt any format problem, it is the problem caused by compression.

In addition, the mechanism of Addressables is to download AddressableAssets from the Remote server and save them in the local Cache, and then load the VideoClip into the program from the locally downloaded Cache file. Therefore, when requesting to update AddressableAssets containing VideoClip, set Caching.compressionEnabled to false, that is, do not compress and save.

But this will bring another problem: if Caching.compressionEnables is set to false globally, the space occupied by other materials will increase several times at once. At present, Unity does not support choosing whether to compress and then cache when downloading Addressables.

Solution:

Separate the Assets related to VideoClip and don't put them together with others. For example, uniformly turn off Caching compression before updating VideoClips. After the update is complete, turn on the Caching compression to update other Assets. The Addressables system does save a lot of work, but it doesn't feel mature enough yet.

Guess you like

Origin blog.csdn.net/luoyu510183/article/details/100941802