所以我尝试在 Xcode 中统一交流 c# 代码和客观 c 代码。这是我的代码:
文本.h
extern "C" {
int textTotexture(int hello,int world);
}
文本.mm
int textTotexture(int hello,int world){
NSString *myString =[[NSString alloc] init];
NSSize size = [myString sizeWithAttributes:0];
NSImage* newImage = [[NSImage alloc] initWithSize: size];
[newImage lockFocus];
/* if you have a second image you're going to overlay on top of the first, do the same except use NSCompositeSourceOver as the operation */
//NSRect myRect = NSMakeRect(0,0,size.width,size.height);
//[myString drawInRect:myRect withAttributes:0];
[newImage unlockFocus];
//NSData *imageData = [newImage TIFFRepresentation];
//NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
//NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor];
//imageData = [imageRep representationUsingType:NSPNGFileType properties:imageProps];
//int len = [imageData length];
//memcpy(data, [imageData bytes], len);
return hello+world;
}
调用函数:
[DllImport("CubePlugin")]
public static extern int textTotexture(int s, int w);
Debug.Log(textTotexture(1,2));
调试日志返回 3 时,基本通信很好。但只要我添加功能代码,统一就会崩溃。我怀疑一些本机代码并没有运行到最后。
我刚刚发现当我添加锁定焦点并失去焦点时会出现问题。我应该怎么做才能避免这样做但实现我的目标?