我想为 unity 创建一个 ios 插件。该功能是使用一些 sdk 发送文本消息。嗯,这是我的对象 c:
-(TextMessage*) CsendText:(NSString *)number CmsgContent:(NSString *)msg{
return [MessagingApi sendText:(NSString*) number msgContent:(NSString *) msg]
}
这是我的 c 换行代码:
TextMessage* SendMessage(const char* contactNumber,const char* content){
Messaging* msg = [[Messaging alloc] init];
NSString* nr = [NSString stringWithUTF8String:contactNumber];
NSString* contentText = [NSString stringWithUTF8String:content];
TextMessage* newText = [msg CsendText:nr CmsgContent:contentText];
return newText;
}
你可以看到我返回了一条文本消息,它是一个事件而不是一个字符,我如何将事件传递给 unity,
我的 C# 代码在这里:
#if UNITY_IPHONE
[DllImport("__Internal")]
private static extern string SendMessage (string contactNumber,string content);
#endif
string phoneNumber="";
string content="";
void OnGUI () {
phoneNumber = GUI.TextField ( new Rect (250, 125, 250, 25), phoneNumber, 40);
content = GUI.TextField (new Rect (250, 157, 250, 25), content, 40);
if (GUI.Button(new Rect (250, 250, 100, 30),"click me")) {
SendMessage(phoneNumber,content);
}
}
最佳答案
这不是它的工作方式。你不能只在 C 中返回一些东西并期望它出现在 Unity/C# 中。 要实现“双向通信”,您需要执行以下操作。
C#代码
[DllImport ("__Internal")]
private static extern void _SomeCMethod(string parameter);
(目标-)C代码
void _SomeCMethod(const char *parameter)
{
}
当您在 C# 中调用 _SomeMethod 时,将调用您的(目标)C 代码中的 _SomeMethod。
Objective-C 代码
- (void)callSomeCSharpMethod
UnitySendMessage("GO", "SomeCSharpMethod", "parameter");
}
C# 代码(MonoBehaviour 脚本附加到 GO)
void SomeCSharpMethod(string parameter)
{
}
当您在 Objective-C 中调用 callSomeCSharpMethod 时,将调用 C# 代码中的 SomeCSharpMethod。
您必须将 NSString 转换为 C 字符串 (const char *),反之亦然。
const char * => NSString[NSString stringWithCString:string encoding:NSUTF8StringEncoding];
NSString => const char *[string cStringUsingEncoding:NSUTF8StringEncoding];
注意:
这只是一个关于如何在 C# 和 native iOS 代码之间实现双向通信 channel 的简单示例。
当然你不想硬编码例如GameObject 在代码中的名称,因此您可以在一开始就将该名称(用于来自 Objective-C 的回调)传递给您的 native iOS 代码。
关于c# - Unity 从对象 c 接收事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30938859/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我想让一个yaml对象引用另一个,如下所示:intro:"Hello,dearuser."registration:$introThanksforregistering!new_message:$introYouhaveanewmessage!上面的语法只是它如何工作的一个例子(这也是它在thiscpanmodule中的工作方式。)我正在使用标准的rubyyaml解析器。这可能吗? 最佳答案 一些yaml对象确实引用了其他对象:irb>require'yaml'#=>trueirb>str="hello"#=>"hello"ir