标签 Unity 下的文章

https://github.com/homuler/MediaPipeUnityPlugin

You cannot build libraries for Android with the following steps.

1、安装msys2配置系统环境变量Path添加 C:\msys64\usr\bin
执行 pacman -Su
执行 pacman -S git patch unzip
2、安装Python3.9.10 勾选系统环境变量 执行pip install numpy --user
3、安装VS2019,勾选使用C++的桌面开发
4、下载bazel-5.0.0-windows-x86_64.exe 重命名为bazel.exe 并配置系统环境变量
设置Bazel变量
set BAZEL_VS=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
set BAZEL_VC=C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC
set BAZEL_VC_FULL_VERSION=14.29.30133
set BAZEL_WINSDK_FULL_VERSION=10.0.19041.0
5、下载nuget 并配置系统环境变量
6、下载opencv-3.4.16-vc14_vc15.exe 运行解压在C盘根目录
7、Android Build Tools<31 Android NDT <22 配置系统环境变量sdk ANDROID_HOME ndk ANDROID_NDK_HOME
步骤7不需要,该工程无法创建Android libraries

最后MediaPipeUnityPlugin工程目录下执行 python build.py build --desktop cpu --opencv=cmake -v
编译
Unity编辑器运行

.ffmpeg -i .test.mov -vcodec libvpx -auto-alt-ref 0 -acodec libvorbis test.webm
vcodec libvpx 是视频vp8 -acodec libvorbis是音频vorbis
unity不支持vp9

截取视频前3秒
.ffmpeg -i test.webm -vcodec copy -acodec copy -ss 00:00:00 -to 00:00:03 testnew.webm

目前有截图、点击的功能,其他只有测试(划屏,装软件,输入中文)代码,需要修改

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class Main : MonoBehaviour
{
    private string adbPath;
    private string filePath;
    /// <summary>
    /// 列出设备按钮
    /// </summary>
    public Button btnList;

    public Transform togglePrefab;

    /// <summary>
    /// 所有设备
    /// </summary>
    Dictionary<string, GameObject> advs = new Dictionary<string, GameObject>();
    /// <summary>
    /// 选中的设备
    /// </summary>
    public List<string> sdvs = new List<string>();

    public InputField inputFieldX;
    public InputField inputFieldY;
    /// <summary>
    /// 点击指定位置按钮
    /// </summary>
    public Button btnClick;
    public Image screenImage;
    public Button btnCapture;
    public Button getCapture;

    private void Awake()
    {
        Loom.Initialize();
    }

    // Use this for initialization
    void Start()
    {
        adbPath = @Application.streamingAssetsPath + "/adb.exe";
        filePath = @Application.streamingAssetsPath;
        btnList.onClick.AddListener(ClickList);
        togglePrefab.gameObject.SetActive(false);
        btnClick.onClick.AddListener(ClickPose);
        btnCapture.onClick.AddListener(Capture);
    }

    string StartProcess(string args, Action action)
    {
        Process process = new Process();
        process.StartInfo.FileName = adbPath;
        process.StartInfo.Arguments = args;
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
        process.Start();
        process.WaitForExit();
        if (action != null)
        {
            action();
        }
        return process.StandardOutput.ReadToEnd();
    }

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

    }

    private void OnGUI()
    {
        //if (GUI.Button(new Rect(0, 0, 200, 50), "列出设备"))
        //{
        //    string args = "devices";
        //    UnityEngine.Debug.Log(StartProcess(args));
        //}
        //if (GUI.Button(new Rect(500, 50, 200, 50), "点击"))
        //{
        //    string args = "-s de81f1c shell input tap 400 400";
        //    UnityEngine.Debug.Log(StartProcess(args));
        //}
        //if (GUI.Button(new Rect(500, 100, 200, 50), "手机截屏"))
        //{
        //    string args = "-s de81f1c shell screencap -p /sdcard/screenshot.jpg";
        //    UnityEngine.Debug.Log(StartProcess(args));
        //}
        //if (GUI.Button(new Rect(500, 150, 200, 50), "获取截屏"))
        //{
        //    string args = "-s de81f1c pull /sdcard/screenshot.jpg " + @Application.streamingAssetsPath + "/screenshot.jpg";
        //    UnityEngine.Debug.Log(StartProcess(args));
        //}
        //if (GUI.Button(new Rect(500, 200, 200, 50), "划屏"))
        //{
        //    string args = "-s de81f1c shell input swipe 800 300 200 300";
        //    UnityEngine.Debug.Log(StartProcess(args));
        //}
        //if (GUI.Button(new Rect(500, 250, 200, 50), "安装ADB键盘"))
        //{
        //    string args = "-s 127.0.0.1:62001 install " + Application.streamingAssetsPath + "/ADBKeyboard.apk";
        //    UnityEngine.Debug.Log(StartProcess(args));
        //}
        //if (GUI.Button(new Rect(500, 300, 200, 50), "设置ADB默认键盘"))
        //{
        //    string args = "-s 127.0.0.1:62001 shell ime set com.android.adbkeyboard/.AdbIME";
        //    UnityEngine.Debug.Log(StartProcess(args));
        //}
        //if (GUI.Button(new Rect(500, 350, 200, 50), "输入文字"))
        //{
        //    string args = "-s 127.0.0.1:62001 shell am broadcast -a ADB_INPUT_TEXT --es msg '曹尼玛终于出中文了!'";
        //    UnityEngine.Debug.Log(StartProcess(args));
        //}

    }

    void ClickList()
    {
        string args = "devices";
        string opt = StartProcess(args, null);
        List<string> list = opt.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
        list = list.Where(s => !string.IsNullOrEmpty(s)).ToList();
        if (list.Count < 2)
        {
            return;
        }
        for (int i = 1; i < list.Count; i++)
        {
            string[] dns = list[i].Split('\t');
            string id = dns[0];
            string name = dns[1];
            if (advs.ContainsKey(id))
            {
                continue;
            }
            Transform item = CreateItem(togglePrefab);
            item.Find("Label").GetComponent<Text>().text = "设备ID:" + id + "  设备名:" + name;
            item.name = dns[0];
            item.GetComponent<Toggle>().onValueChanged.AddListener(state => { ToggleClick(state, item); });
            item.gameObject.SetActive(true);
            advs.Add(id, item.gameObject);
        }
        UnityEngine.Debug.Log(opt);
    }

    void ToggleClick(bool state, Transform go)
    {
        if (state)
        {
            sdvs.Add(go.name);
        }
        else
        {
            sdvs.Remove(go.name);
        }
    }

    /// <summary>
    /// 点击
    /// </summary>
    void ClickPose()
    {
        for (int i = 0; i < sdvs.Count; i++)
        {
            string args = "-s " + sdvs[i] + " shell input tap " + inputFieldX.text + " " + inputFieldY.text;
            Loom.RunAsync(() =>
            {
                string opt = StartProcess(args, Capture);
                UnityEngine.Debug.Log(opt);
            });
        }
    }

    /// <summary>
    /// 截图
    /// </summary>
    void Capture()
    {
        for (int i = 0; i < sdvs.Count; i++)
        {
            string args = "-s " + sdvs[i] + " shell screencap -p /sdcard/screenshot.jpg";
            Loom.RunAsync(() =>
            {
                string opt = StartProcess(args, GetCapture);
                UnityEngine.Debug.Log(opt);
            });
        }
    }

    /// <summary>
    /// 获取截图
    /// </summary>
    void GetCapture()
    {
        for (int i = 0; i < sdvs.Count; i++)
        {
            string args = "-s " + sdvs[i] + " pull /sdcard/screenshot.jpg " + filePath + "/screenshot.jpg";
            Loom.RunAsync(() =>
            {
                string opt = StartProcess(args, ShowCapture);
                UnityEngine.Debug.Log(opt);
            });
        }
    }

    /// <summary>
    /// 显示截图
    /// </summary>
    void ShowCapture()
    {
        Loom.QueueOnMainThread(() =>
        {
            StartCoroutine(LoadImg());
        });
    }

    /// <summary>
    /// 加载图片
    /// </summary>
    /// <returns></returns>
    IEnumerator LoadImg()
    {
        WWW www = new WWW("file:///" + filePath + "/screenshot.jpg");
        yield return www;
        Sprite sprite = Sprite.Create(www.texture,new Rect(0, 0, www.texture.width, www.texture.height), Vector2.zero);
        screenImage.sprite = sprite;
        www.Dispose();
        www = null;
    }

    private Transform CreateItem(Transform prefab)
    {
        Transform item = Instantiate(prefab);
        item.SetParent(prefab.parent);
        item.localPosition = Vector3.zero;
        item.localScale = new Vector3(1, 1, 1);
        return item;
    }

    private void OnApplicationQuit()
    {

    }
}

TIM截图20190321164754.jpg

工程地址https://gitee.com/awnuxcvbn/GroupControl

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
public class Tools : Editor
{
    [MenuItem("Tools/添加所有场景到BuildSettings")]
    static void CheckSceneSetting()
    {
        List<string> dirs = new List<string>();
        GetDirs(Application.dataPath + "/", ref dirs);
        EditorBuildSettingsScene[] newSettings = new EditorBuildSettingsScene[dirs.Count];
        for (int i = 0; i < newSettings.Length; i++)
        {
            newSettings[i] = new EditorBuildSettingsScene(dirs[i], true);
        }
        EditorBuildSettings.scenes = newSettings;
        AssetDatabase.SaveAssets();
    }
    private static void GetDirs(string dirPath, ref List<string> dirs)
    {
        foreach (string path in Directory.GetFiles(dirPath))
        {
            if (Path.GetExtension(path) == ".unity")
            {
                dirs.Add(path.Substring(path.IndexOf("Assets/")));
            }
        }
        if (Directory.GetDirectories(dirPath).Length > 0)
        {
            foreach (string path in Directory.GetDirectories(dirPath))
            {
                GetDirs(path, ref dirs);
            }
        }
    }
}

TIM截图20190320145202.jpg