c# - 将 uintptr_t 转换为 id<MTLTexture>

标签 c# ios unity-game-engine metal

我想创建一个简单的 iOS 插件,可以将纹理绘制为统一的 Texture2D。我已经通过 CreateExternalTexture() 和 UpdateExternalTexture() 完成了它,它工作正常,但我很好奇我是否真的可以直接从 iOS 端填充 Unity 纹理。这是我的 iOS 插件代码:

//
//  testTexturePlugin.m
//  Unity-iPhone
//
//  Created by user on 18/01/16.
//
//

#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>
#import <UIKit/UIKit.h>

#include "UnityMetalSupport.h"

#include <stdlib.h>
#include <stdint.h>

static UIImage* LoadImage()
{
    NSString* imageName = @"logo"; //[NSString stringWithUTF8String: filename];
    NSString* imagePath = [[NSBundle mainBundle] pathForResource: imageName ofType: @"png"];

    return [UIImage imageWithContentsOfFile: imagePath];
}

// you need to free this pointer
static void* LoadDataFromImage(UIImage* image)
{
    CGImageRef imageData    = image.CGImage;
    unsigned   imageW       = CGImageGetWidth(imageData);
    unsigned   imageH       = CGImageGetHeight(imageData);

    // for the sake of the sample we enforce 128x128 textures
    //assert(imageW == 128 && imageH == 128);

    void* textureData = ::malloc(imageW * imageH * 4);
    ::memset(textureData, 0x00, imageW * imageH * 4);

    CGContextRef textureContext = CGBitmapContextCreate(textureData, imageW, imageH, 8, imageW * 4, CGImageGetColorSpace(imageData), kCGImageAlphaPremultipliedLast);
    CGContextSetBlendMode(textureContext, kCGBlendModeCopy);
    CGContextDrawImage(textureContext, CGRectMake(0, 0, imageW, imageH), imageData);
    CGContextRelease(textureContext);

    return textureData;
}

static void CreateMetalTexture(uintptr_t texRef, void* data, unsigned w, unsigned h)
{
#if defined(__IPHONE_8_0) && !TARGET_IPHONE_SIMULATOR

    NSLog(@"texRef iOS = %lu", texRef);

    id<MTLTexture> tex = (id<MTLTexture>)(size_t)texRef;

    MTLRegion r = MTLRegionMake3D(0, 0, 0, w, h, 1);
    [tex replaceRegion: r mipmapLevel: 0 withBytes: data bytesPerRow: w * 4];

#else

#endif
}

extern "C" void FillUnityTexture(uintptr_t texRef)
{
    UIImage*    image       = LoadImage();
    void*       textureData = LoadDataFromImage(image);

    if (UnitySelectedRenderingAPI() == apiMetal)
        CreateMetalTexture(texRef, textureData, image.size.width, image.size.height);

    ::free(textureData);
}

这是 Unity 代码:

using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;

public class TextureHandler : MonoBehaviour {

    [SerializeField]
    private Renderer _mesh;

    private Texture2D _meshTexture;

    [DllImport("__Internal")]
    private static extern void FillUnityTexture(IntPtr texRef);

    void Start () {

        _meshTexture = new Texture2D(200, 200, TextureFormat.ARGB32, false);
        _mesh.material.SetTextureScale ("_MainTex", new Vector2 (-1, -1));
        _mesh.material.mainTexture = _meshTexture;

        IntPtr texPtr = _meshTexture.GetNativeTexturePtr();
        Debug.Log("texPtr Unity = " + texPtr);

        FillUnityTexture(texPtr);
    }
}

我检查过,Unity 纹理上的指针已正确传递到 iOS 插件。但我在 iOS 插件上的这一行发生了崩溃:

[tex replaceRegion: r mipmapLevel: 0 withBytes: data bytesPerRow: w * 4];

而且我很确定我遇到这个问题是因为 Unity 纹理 (uintptr_t) 上的指针错误地转换为 Metal 纹理 (id)。

所以我的问题是 - 如何正确地将指向纹理的指针转换为 MTLTexture?

最佳答案

我想你应该阅读有关 ARC 的内容。

您可以使用__bridge_retained转让新创建的所有权id<MTLTexture>反对 uintptr_t代码。当你想转换uintptr_t时返回id<MTLTexture>使用__bridge如果您不想将所有权转回或使用 __bridge_transfer当你这样做时。

关于c# - 将 uintptr_t 转换为 id<MTLTexture>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34854854/

相关文章:

ios - 如何在用户快速点击搜索栏时显示表格 View

ios - 我正在尝试将 firebase 合并到我的 Unity 项目中,但不断收到有关 "Firebase.Crashlytics.Editor.dll"的错误

unity-game-engine - 多个SubShader,只有一个正在渲染

unity-game-engine - 如何知道安装了哪个版本的 oculus 集成?

c# - 如何在运行时更改按钮的背景图像?

c# - 强制定时器在设定的时间内循环[C#]

c# - 使用泛型方法返回 list<T>,其中 T 可以是共享相同结构的三种类型之一

javascript - Visual Studio 显示错误

ios - 如何检测语言环境?

ios - WKWebView 在加载另一个 URL 时不会重新呈现内容