RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-215964

eastwing's questions

Martin Hope
eastwing
Asked: 2020-04-15 21:28:12 +0000 UTC

在应用程序之间直播视频

  • 1

有一个任务:从设备(例如,从 Android 手机)广播实时视频,而另一个应用程序应该读取流。但我无法以任何方式组织流:远程应用程序最多只能接收和显示第一帧。在这种情况下,如果我理解正确,设备上的应用程序会发送流。

发送类:

using System;
using System.Collections;
using System.Net.Sockets;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using System.IO;
using System.Threading.Tasks;

public class Connector : MonoBehaviour
{
    TcpClient tcp;
    public TextMeshProUGUI label;
    public Button sendButton;
    public Button connectButton;

NetworkStream stream;
BinaryWriter writer;

bool connected = false;

void Awake() => Application.targetFrameRate = 30;

void Start()
{

}

void Update(){
    sendButton.gameObject.SetActive(connected);
    connectButton.gameObject.SetActive(!connected);
}

public void Connect(){
    try {
        label.text = "Connecting...";
        tcp = new TcpClient {NoDelay = true};
        tcp.Connect(
            Options.i.serverAddress, 
            Options.i.serverPort);
        label.text = "Connection established.";

        stream = tcp.GetStream();
        writer = new BinaryWriter(stream);

        connected = true;
    }
    catch (Exception ex){
        label.text = ex.Message;
    }
}

public void Send() => StartCoroutine(ProcessSending());

IEnumerator ProcessSending(){
    var endOfFrame = new WaitForEndOfFrame();
    bool readyToGetFrame;

    while (true){
        yield return endOfFrame;

        byte[] frameBytesLength = new byte[Options.i.sendRecieveCount];
        byte[] rawFrame = GetCameraLastImage();

        ByteLengthToFrameByteArray(rawFrame.Length, frameBytesLength);

        readyToGetFrame = false;
        Task.Run(() => {
            //Send total byte count first
            writer.Write(frameBytesLength, 0, frameBytesLength.Length);
            //label.text = "Sent Image byte Length: " + frameBytesLength.Length;

            //Send the image bytes
            writer.Write(rawFrame, 0, rawFrame.Length);
            label.text = "Sending Image byte array data : " + rawFrame.Length;

            readyToGetFrame = true;
        });

        while (!readyToGetFrame)
            yield return null;
    }
}

void ByteLengthToFrameByteArray(int byteLength, byte[] fullBytes)
{
    Array.Clear(fullBytes, 0, fullBytes.Length);
    byte[] bytesToSendCount = BitConverter.GetBytes(byteLength);
    bytesToSendCount.CopyTo(fullBytes, 0);
}

public byte[] GetCameraLastImage(){
    //получение изображения с камеры устройства
}
}

接收器类:

using System;
using System.Net.Sockets;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

public class Listener : MonoBehaviour
{
    TcpListener server = null;
    TcpClient client = null;
    public RawImage projector;
    public AspectRatioFitter fitter;
bool running = false;

byte[] frameData { get; set; }
bool needUpdate = false;
bool readyToUpdate = true;

Texture2D texture;

void Start()
{
    texture = new Texture2D(1, 1, TextureFormat.RGB24, false, true);

    server = new TcpListener(Options.i.serverAddress, Options.i.serverPort);

    server.Start();
    Debug.Log("Waiting for connections...");
    running = true;
    Task.Run(async () => await Listen());
}

void Update(){
    if (!needUpdate)
        return;

    texture.LoadImage(frameData);
    texture.Apply();
    projector.texture = texture;
    projector.gameObject.SetActive(true);
    fitter.aspectRatio = (float)texture.width / texture.height;

    needUpdate = false;
    readyToUpdate = true;
}

async Task Listen()
{
    var strem =  client.GetStream();
    while (running){
        var size = Options.i.sendRecieveCount;
        bool disconnected = false;

        client = await server.AcceptTcpClientAsync();
        //HandleClient(await server.AcceptTcpClientAsync());
        var stream = client.GetStream();
        var imageBytesCount = new byte[size];

        var total = 0;
        do {
            var read = stream.Read(imageBytesCount, total, size - total);
            //Debug.LogFormat("Client recieved {0} bytes", total);
            if (read == 0)
            {
                disconnected = true;
                break;
            }
            total += read;
        } 
        while (total != size);

        var imageSize = disconnected
            ? -1
            : GetLength(imageBytesCount);

        await ReadStream(imageSize, stream);
    }
}

int GetLength(byte[] frameBytesLength)
{
    int byteLength = BitConverter.ToInt32(frameBytesLength, 0);
    return byteLength;
}

async Task ReadStream(int size, NetworkStream clientStream)
{
    bool disconnected = false;

    byte[] imageBytes = new byte[size];
    var total = 0;
    do
    {
        var read = clientStream.Read(imageBytes, total, size - total);
        if (read == 0)
        {
            disconnected = true;
            break;
        }
        total += read;
    } 
    while (total != size);
    Debug.LogFormat("{0} bytes recieved", total);

    //Display Image
    if (!disconnected)
    {
        //Display Image on the main Thread
        readyToUpdate = false;
        UpdateFrame(imageBytes);
    }

    //Wait until old Image is displayed
    while (!readyToUpdate)
        await Task.Delay(1);
        //System.Threading.Thread.Sleep(1);
}

private void UpdateFrame(byte[] rawFrame)
{
    frameData = rawFrame;
    needUpdate = true;
}
}

我需要做什么才能使流工作?

c#
  • 1 个回答
  • 10 Views
Martin Hope
eastwing
Asked: 2020-08-27 22:47:27 +0000 UTC

六边形 Tilemap 中的相邻单元格

  • 2

有没有办法获取给定 Tilemap 的相邻单元格?六边形网格使用的是直角坐标,但我不明白它们是如何分布的,所以无法设置标准偏移量。可以冗余返回(即 8,对于正方形),但此选项不适合我。

我的直觉认为答案是基本的,但我无法证明:)

unity3d
  • 1 个回答
  • 10 Views
Martin Hope
eastwing
Asked: 2020-05-20 04:50:43 +0000 UTC

Unity UI 的着色器图

  • 1

我正在尝试创建一个可以使 UI 元素发光的着色器。

在此处输入图像描述

在“场景”选项卡中,着色器工作:

在此处输入图像描述

但是,已经在“游戏”选项卡上(无论项目是否正在运行),以及在组装项目中 - 否:

在此处输入图像描述

这是一个错误还是应该是这样的?我不擅长着色器,我承认我做错了什么。

目前是否可以使用 Shader Graph 为 UI 元素创建工作着色器?

统一版本:2019.1.2f1

更新

着色器在应用于其画布在世界空间或屏幕空间 - 相机模式下呈现的 UI 时起作用。但它仍然不一样,它需要在叠加层中工作。可能吗?

unity3d
  • 1 个回答
  • 10 Views
Martin Hope
eastwing
Asked: 2020-08-29 13:28:53 +0000 UTC

<| 运算符如何工作?

  • 0

你好!

帮助我弄清楚反向管道是如何工作的。

假设有一段代码:

let add x y =
    x + y

[<EntryPoint>]
let main argv =
    add 2 <| add 3 <| 4 |> add 7 |> add 8

此代码不正确,但删除后将变为正确add 2 <|

为什么这段代码现在不正确?传递了add 2什么?

f#
  • 1 个回答
  • 10 Views
Martin Hope
eastwing
Asked: 2020-11-02 23:20:56 +0000 UTC

“分形”界面没有意义?

  • 2

在消除我知识上的灾难性差距的过程中,我勾画了一个特定的类,像这样(我没有在列表中包括构造函数和方法,无论如何这个问题都很难理解):

public class SomeFractal
{
        public int Generation;
        public SomeFractal Parent { get; private set; }
        public SomeFractal Root
        {
            get
            {
                if (Generation == 0) return this;
                else return Parent.Root;
            }
        }

    List<Fractal> Children {get;}
}

这样做是为了计算递归算法,但我不是在谈论那个。

出现了描述几个细节不同的“分形”类并实现单个接口的想法,让它成为 IFractal。该界面应包含主要属性(父、根、子分支)。

大多数属性自然会返回描述它们的类的对象(我们有一个分形,还记得吗?),所以如果我希望它们在接口中被描述,它必须是协变的。结果是这样的:

interface IFractal <out T> where T : IFractal<T>
{
    T Root { get; }
    T Parent { get; }

    IEnumerable<T> Children { get; }
}

实际上,没有问题——接口当然是由上述类实现的:

class SomeFractal : IFractal<SomeFractal>

如果我想在其他地方使用类型变量IFractal<T>来从特定的实现类中抽象出来,问题就会出现。毕竟,我必须指定参数 T,它只能是实现了 IFractal 的类——也就是说,没有获得任何抽象,在这种情况下它与使用类本身是一样的。

有没有办法绕过这个限制?也许我在协方差方面失败了?

请不要讨论实际应用的可行性,这纯粹是出于教育目的的理论化。

更新

我将用一个例子来解释非泛型

interface IFractal 
 {
     IFractal Root { get; }
     IEnumerable<IFractal> Children { get; }
 }


 class Fractal : IFractal 
 {
     public IFractal Root { get {
             if (parent == null) return this;
             else return parent.Root;
         } }
     public IEnumerable<IFractal> Children { get; }
     private Fractal parent;

     private string prettyCoolField;

     public string GetRootField() {
         return Root.prettyCoolField;
     }
 }

GetRootField()它不会工作,因为它IFractal没有 field prettyCoolField。返回(Fractal)Root.prettyCoolField;也不起作用。还是我错误地转换了类型?

c#
  • 2 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5