UGUI—RawImage

作者:追风剑情 发布于:2020-9-29 12:38 分类:Unity3d

示例一:通过Texture内存指针显示画面

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 通过Texture内存指针显示画面
/// </summary>
public class IntPtrRawImage : RawImage
{
    public int textureWidth = 640;
    public int textureHeight = 360;
    private Texture2D m_Texture2D;
    private IntPtr m_TexturePtr = IntPtr.Zero;
    private bool m_Started = false;

    protected override void Awake()
    {
        base.Awake();
        m_Texture2D = new Texture2D(textureWidth, textureHeight, TextureFormat.RGB24, false);
        this.texture = m_Texture2D;
    }

    protected override void Start()
    {
        base.Start();
        m_Started = true;
        if (m_TexturePtr != IntPtr.Zero)
            UpdateExternalTexture(m_TexturePtr);
    }

    public void UpdateExternalTexture(IntPtr ptr)
    {
        m_TexturePtr = ptr;
        if (ptr == IntPtr.Zero)
            return;
        if (!m_Started)
            return;
        m_Texture2D.UpdateExternalTexture(ptr);
    }

    public void Show()
    {
        gameObject.SetActive(true);
    }

    public void Hide()
    {
        gameObject.SetActive(false);
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号