1

所以我尝试在 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 时,基本通信很好。但只要我添加功能代码,统一就会崩溃。我怀疑一些本机代码并没有运行到最后。

我刚刚发现当我添加锁定焦点并失去焦点时会出现问题。我应该怎么做才能避免这样做但实现我的目标?

4

1 回答 1

0

你如何处理你的类“文本”并不明显,但你可以试试这个:(我在没有编辑器的情况下写作,所以可能有错别字)

@implementation text
//.... 
-(int)textToTexture:(int)A andValue:(int)B
{
   int result = 0;
   // do stuff
   return result
}
@end

static text* myObject = nil;

extern "C"{
   int textToTexture(int a, int b)
  {
     if(myObject == nil)
         myObject = [[text alloc] init];

     return [myObject textToTexture:a andValue:b];
  }
}

在你的 C# 文件中

[DllImport ("__Internal")]
public static extern int textTotexture(int a, int b);

然后调用函数

于 2012-08-17T15:51:20.713 回答