1

だから私はc#コードをユニティで伝え、ObjectiveCコードをXcodeで伝えようとしています。これが私のコードです:

text.h

extern "C" {
int textTotexture(int hello,int world);
}

text.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 に答える