unity,记录一次完整的安卓真机测试流程

1、设备

- 设备/软件 备注
1 windows10 64位 工作电脑
2 unity

2022.3.2f1c1.git.8986298
Revision: 2022.3/china_unity/release 891eba2b6e38
Built: Wed,05Jul 2023 09:24:20 GMT

3 安卓触摸一体机 rk3368,Android 6.0.1
4 小米CC9 Pro Android 11,MIUI 13.0.4
5 T-Mobile  Android 7.0
6    
7    
8    
     

 

2、Unity项目代码

ShowText.cs

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

public class ShowText : MonoBehaviour
{
    public TMP_Text showText;
    public Button WriteBtn, ReadBtn, CreateBtn;

    private void Start()
    {
        WriteBtn.onClick.AddListener(WriteEvent);//响应写入按钮
        ReadBtn.onClick.AddListener(ReadEvent);//响应读取按钮
        CreateBtn.onClick.AddListener(CreateEvent1);//响应创建

        StreamWriter st = File.CreateText(Application.persistentDataPath + "/Test.txt");
        st.Write("123456789");
        st.Close();
    }

    private void WriteEvent()
    {

        StreamWriter st = File.CreateText(Application.persistentDataPath + "/Test.txt");
        DateTime nowTime = DateTime.Now;
        st.Write(nowTime);

        Debug.Log(string.Format("写入当前时间:{0}",nowTime));
        st.Close();
    }
    private void ReadEvent()
    {
        showText.text = File.ReadAllText(Application.persistentDataPath + "/Test.txt");
    }

    /// <summary>
    /// 创建目录,这个在安卓模拟器上能行,测试一下真机
    /// </summary>
    private void CreateEvent()
    {
        string newFolderPath = Path.Combine(Application.persistentDataPath, "/mymedia");//合并两个及两个以上的路径字符串
        Debug.Log("创建目录:" + newFolderPath);
        if (!Directory.Exists(newFolderPath)) { 
            Directory.CreateDirectory(newFolderPath);//安卓上提示Read-only file system
        }
    }

    /// <summary>
    /// 在存储卡根目录下创建文件夹
    /// </summary>
    private void CreateEvent1()
    {
#if UNITY_ANDROID

        string path = "/sdcard/ZjhData/";
        Debug.Log("创建目录:" + path);

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        //判断是否有此文件夹
        if (Directory.Exists(path))
        {
            DirectoryInfo direction = new DirectoryInfo(path);
            FileInfo[] files = direction.GetFiles("*");
            Debug.Log("文件总数为" + files.Length);

            CreateFileAndWriteTime(path,files.Length);//创建文件,并写入时间
        }

#elif UNITY_EDITOR

        string path = Application.streamingAssetsPath;
        path = path + "/ZjhData/";
        Debug.Log("创建目录:" + path);

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        //判断是否有此文件夹
        if (Directory.Exists(path))
        {
            DirectoryInfo direction = new DirectoryInfo(path);
            FileInfo[] files = direction.GetFiles("*");
            Debug.Log("文件总数为" + files.Length);

            CreateFileAndWriteTime(path,files.Length);//创建文件,并写入时间

        }

#endif
    }

    /// <summary>
    /// 在指定的目录下,创建数字文件名并写入当前时间
    /// 如:/sdcard/ZjhData/8.txt
    /// 调用示例:CreateFileAndWriteTime("/sdcard/ZjhData/",5)
    /// </summary>
    private void CreateFileAndWriteTime(string path,int num)
    {
        string filePath = path + num + ".txt";//形如:/sdcard/ZjhData/5.txt

        StreamWriter st = File.CreateText(filePath);
        DateTime nowTime = DateTime.Now;
        st.Write(nowTime);

        Debug.Log(string.Format("写入{0},时间:{0}", filePath, nowTime));
        st.Close();
    }
}

  

3、创建Canvas和Panel

创建UI->Panel,创建几个按钮

(图1)

按钮创建后改一下按钮名称和上面的文字即可,其它不需要任何操作(事件绑定在脚本里);

write按钮:每次点击将当前日期和时间写到一个TXT;

read按钮:每次点击从一个TXT中读取内容,显示到New Text上;

GetAndroidPath按钮:每次点击创建一个TXT文件,文件名分别为0.txt、1.txt、2.txt,以此类推;

Button C#按钮:此按钮没有用到;

4、挂载脚本到Panel上

见图1

5、把对象拖到右侧inspector的相应变量上

见图1

6、编缉器内运行

(图二)

在编缉器内运行,在图二所示的目录下,可以看到创建的数字文件

 

7、真机调试

将手机的开发者选项、USB安装软件、USB调试全部打开,使用USB连接到电脑上,同时记下手机的IP地址。

Unity->Edit/Preferences/External Tools/,找到platform-tools的地址

D:\Program Files\Unity\2022.3.2f1c1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\platform-tools

File->Build Settings->Player Setting

修改Company Name,Product Name,Version (直接影响生成的图标的名称和安卓APP的包名)

Player Setting,安卓设置中,Minimun API Level:Android 5.1

Build Setting中,对以下几项打勾

Development Build

Autoconnect Profiler

Deep Profiling Support

Script Debugging

Wait For Managed Debugger

Project Setting->Player->安卓->Build->Custom Main Mainfest
这样会在Assets/Plugins/Android/AndroidMainfest.xml
添加授权代码 android:requestLegacyExternalStorage="true"
即<application android:requestLegacyExternalStorage="true">

写权限
Write Permission:External(SDCard)

 

并选择一台安卓设备

 (图7.1 选择目标测试设备)

(图7.2 打勾这几项,才能回显日志)

 (图7.3 真机测试后的结果)

(图7.4 真机测试后的结果)

8、安卓触摸一体机真机测试

9、小米手机真机测试

10、T-Mobile真机测试

见7部分

 

posted @ 2023-08-02 18:18  牛大胆V5  阅读(259)  评论(1)    收藏  举报