Unity中使用RawImage和RenderTexture在UI界面上投影其他相机拍摄的内容,并实现控制投影在RawImage中摄像机的移动

这篇博客介绍了如何在Unity中利用RenderTexture和RawImage组件,将一个摄像机捕捉的内容投影到UI界面,并且详细阐述了如何设置和控制投影在RawImage上的摄像机进行移动,包括物体跟随运动和摄像机围绕特定点旋转的实现方法。

a.单纯的用小窗显示物体

个人认为所谓的 “ 物体A跟随物体B运动 ” 表现为:二者表现为运动(平移、旋转)方向,速度在任何时刻表现一致。

1.在Project界面创建RenderTexture

在这里插入图片描述

2.在Hierarchy界面创建RawImage

在这里插入图片描述

3.选中第二个摄像机,将New Render Texture挂载上

在这里插入图片描述

4.选中RawImage,将New Render Texture挂载上

在这里插入图片描述
最终效果如下图所示
在这里插入图片描述

b.当小窗显示时,可以控制小窗移动

场景中有两个摄像机,分别挂在不同的脚本

第一个脚本MoveScript.cs

using UnityEngine;
using System.Collections;

public class MoveScript : MonoBehaviour {
   
   

    public enum RotationAxes {
   
    MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    public RotationAxes axes = RotationAxes.MouseXAndY;

    public float sensitivityX = 15F;
    public float sensitivityY = 15F;

    public float minimumX = -360F;
    public float maximumX = 360F;

    public float minimumY = -60F;
    public float maximumY = 60F;
    
    public static bool isEnableMove;

    float moveSensitivity = 1.0f;

    float rotationX = 0F;
    float rotationY = 0F;

    //Quaternion originalRotation;
    //鼠标缩放
    public float sensitivity = 60f;
    public float minFov = 50f;
    public float maxFov = 70f;

    public float speedMove = 10f;
    private float speed;

     void Start()
    {
   
   
        // Make the rigid body not change rotation
        if (GetComponent<Rigidbody>())
            GetComponent<Rigidbody>().freezeRotation = true;
       // originalRotation = transform.localRotation;
        speed = speedMove;
        isEnableMove = true;
    }

    void Update()
    {
   
   
        fnRotate();
        if (isEnableMove)
            fnMove();
       // fnScroll();
    }
  
    void fnRotate()
    {
   
   
        CharacterController controller = gameObject.GetComponent<CharacterController>();
        if (Input.GetMouseButton(1))//0对应左键 , 1对应右键 , 2对应中键
        {
   
   
            if (axes == RotationAxes.MouseXAndY)
            {
   
   
                float rotationX 
可以使用国标SDK在Unity环境的RawImageRenderTexture中播放视频流。从技术原理上,国标SDK完成视频流的拉取与解码,之后将解码后的视频帧数据传递给Unity进行渲染。 以常见的实现方式为例,在获取到视频流后,若使用RawImage显示视频流,可将其作为载体,为其添加Render Texture来显示视频。像在基于Agora声网SDK实现音视频通话的案例中,以视频通话为例,视频流的显示可以使用Raw Image作为载体,为其添加Render Texture来显示视频,首先制作一个Prefab预制件,当用户推送本地的视频流时,加载该预制件资产Instantiate实例化、设置在用户Avatar人物实例头显上方。当用户停止视频流发布时,将上述预制件的实例进行销毁[^2]。 若采用RenderTexture,可将其关联到RawImage或者模型面片上,把解码后的视频帧数据更新到RenderTexture中,进而实现视频流的显示。例如在unity海康威视原生SDK拉取网络摄像头画面的场景中,可将脚本所在的Plane的材质指定为特定材质,或者将rawimage的material属性指定为该材质,以此来展示视频画面[^3]。 以下是一个简单的伪代码示例,展示如何将视频帧数据更新到RenderTexture: ```csharp using UnityEngine; public class VideoDisplay : MonoBehaviour { public RawImage rawImage; public RenderTexture renderTexture; void Start() { rawImage.texture = renderTexture; } public void UpdateVideoFrame(byte[] frameData, int width, int height) { // 这里需要实现将frameData转换为RenderTexture可接受的格式 // 例如使用Texture2D作为中间转换 Texture2D tempTexture = new Texture2D(width, height); tempTexture.LoadRawTextureData(frameData); tempTexture.Apply(); Graphics.Blit(tempTexture, renderTexture); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值