unityr in action 中文版有中文版吗

问题:Unity3D 中的 IronPython
描述:我想用IronPython作为Unity的外部语言脚本。IronPython执行加载所需的DLL放在Assets\Plugins。然而,当我运行脚本的时候出现了如下错误:PythonImportErrorException: No module named UnityEngine
IronPython.Modules.Builtin.__import__ (IronPython.Runtime.Calls.ICallerContext,string,object,object,object) &IL 0x0003b, 0x001cc&
(wrapper dynamic-method) object.__import__##5 (IronPython.Runtime.Calls.ICallerContext,object,object,object,object) &IL 0x0000e, 0x0004d&
IronPython.Runtime.Calls.FastCallableWithContextAny.Call (IronPython.Runtime.Calls.ICallerContext,object,object,object,object) &IL 0x067&
IronPython.Runtime.Calls.BuiltinFunction.Call (IronPython.Runtime.Calls.ICallerContext,object,object,object,object) &IL 0x0000d, 0x00058&
IronPython.Runtime.Operations.Ops.CallWithContext (IronPython.Runtime.Calls.ICallerContext,object,object,object,object,object) &IL 0x0b0&
IronPython.Runtime.Importer.Import (IronPython.Runtime.PythonModule,string,IronPython.Runtime.List) &IL 0x0000d, 0x0006c&
IronPython.Runtime.Operations.Ops.Import (IronPython.Runtime.PythonModule,string) &IL 0x03b&
(wrapper dynamic-method) object.&string&##1 (IronPython.Runtime.ModuleScope) &IL 0x0006b, 0x00210&
脚本和UnityEngine.dll是在同一个文件夹里的。这个是脚本。import clr
clr.LoadAssemblyFromFile("UnityEngine.dll")
import UnityEngine
from UnityEngine import *
Debug.Log("Hello World from Python!")
原问题:IronPython in Unity3D解决方案1:来自@Storm Kiernan 的回答:
从Unity的脚本:PythonEngine engine = new PythonEngine();
engine.LoadAssembly(Assembly.GetAssembly(typeof(GameObject)));
engine.ExecuteFile("apple.py");
python脚本里的(我的pple.py和game.exe放在一个文件夹里的):import UnityEngine
from UnityEngine import *
Debug.Log("Hello From IronPython!")
Edit #1我要指出我出错的原因是运行时版本指定为4.0,而不是3.5或者更低。Edit #2如果你需要从IronPython 访问脚本,你也可以通过加载程序集:engine.LoadAssembly(Assembly.GetAssembly(typeof(MyPlayerScriptOrSomething)));
然后用到脚本里:import MyPlayerScriptOrSomething
注意你不需要为每个脚本都加载程序集,只需一次获得程序集。Edit #3IronPython DLL应该放在Assets下的 Plugins文件夹,这是我的设置。& Assets
& & Plugins
& & & IronMath.dll
& & & IronPython.dll
& & & Microsoft.Scripting.dll
& & & Microsoft.Scripting.Core.dll
Edit #4脚本可以放在任何程序访问的到的地方。例如,你想直接把apple.py"放在C:\,你可以通过以下方法来执行:engine.ExecuteFile(@"c:\apple.py");
Edit #5我现在用的版本是:
以上介绍了“Unity3D 中的 IronPython”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:/itwd/1498921.html
上一篇: 下一篇:1040人阅读
android的没问题
iOS的一直输入不了中文
解决办法:原文地址http://my.oschina.net/araya/blog/680943
#include &Keyboard.h&
#include &DisplayManager.h&
#include &UnityForwardDecls.h&
#include &string&
// Respect values passed from prefix header or CFlags
#ifndef FILTER_EMOJIS_IOS_KEYBOARD
// Set this flag to 0 in order to allow emoji symbols to be entered in the iOS keyboard.
#define FILTER_EMOJIS_IOS_KEYBOARD 1
static KeyboardDelegate* _keyboard =
static bool
_shouldHideInput =
static bool
_shouldHideInputChanged =
static const unsigned
kToolBarHeight = 64;
@implementation KeyboardDelegate
// UI handling
// in case of single line we use UITextField inside UIToolbar
// in case of multi-line input we use UITextView with UIToolbar as accessory view
// toolbar buttons are kept around to prevent releasing them
// tvOS does not support multiline input thus only UITextField option is implemented
#if UNITY_IOS
UITextView*
UIToolbar*
viewToolbarI
UITextField* textF
// keep toolbar items for both single- and multi- line edit in NSArray to make sure they are kept around
#if UNITY_IOS
UIToolbar*
fieldToolbarI
// inputView is view used for actual input (it will be responder): UITextField [single-line] or UITextView [multi-line]
// editView is the &root& view for keyboard: UIToolbar [single-line] or UITextView [multi-line]
UIKeyboardType keyboardT
@synthesize active
@synthesize done
@synthesize canceled = _
// While emoji symbols are still shown in the iOS keyboard, they are all filtered by the
// shouldChangeCharactersInRange method below.
#if FILTER_EMOJIS_IOS_KEYBOARD
bool stringContainsEmoji(NSString *string)
__block BOOL returnValue = NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock: ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
const unichar hs = [substring characterAtIndex:0];
// Surrogate pair
if(hs &= 0xD800 && hs &= 0xDBFF)
if(substring.length & 1)
// Compute the code point in the U+10000 - U+10FFFF plane.
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xD800) * 0x400) + (ls - 0xDC00) + 0x10000;
// The ranges for the various emoji tables are as follows.
// Musical -& [U+1D000, U+1D24F]
// Miscellaneous Symbols and Pictographs -& [U+1F300, U+1F5FF]
// Emoticons -& [U+1F600, U+1F64F]
// Transport and Map Symbols -& [U+1F680, U+1F6FF]
// Supplemental Symbols and Pictographs -& [U+1F900, U+1F9FF]
if(uc &= 0x1D000 && uc &= 0x1F9FF)
returnValue = YES;
else if(substring.length & 1)
const unichar ls = [substring characterAtIndex:1];
if(ls == 0x20E3)
// Filter all the emojis for numbers.
returnValue = YES;
else if(hs &= 0x270A && hs &= 0x270D)
// Filter all the various hand symbols (e.g., victory sign, writing hand, etc).
returnValue = YES;
// Non surrogate pair.
if(hs &= 0x2100 && hs &= 0x27FF)
// Filter the following emoji ranges.
// Letterlike Symbols -& [U+2100, U+214F]
// Number Forms -& [U+2150, U+218F]
// Arrows -& [U+2190, U+21FF]
// Dingbats -& [U+2700, U+27BF]
// Supplemental Arrows-A -& [U+27F0–U+27FF]
returnValue = YES;
else if(hs &= 0x2900 && hs &= 0x297F)
// Filter Supplemental Arrows-B -& [U+2900, U+297F]
returnValue = YES;
else if(hs &= 0x2B05 && hs &= 0x2BFF)
// Filter Miscellaneous Symbols and Arrows -& [U+2B00, U+2BFF]
returnValue = YES;
return returnV
// See the documentation for this method in http://apple.co/1OMnz8D.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
// Process the input string using the 'stringContainsEmoji' function and return NO or YES
// depending on whether it needs to be added to the UITexField or skipped altogether, respectively.
// We need to do this because Unity's UI doesn't provide proper Unicode support yet.
return !stringContainsEmoji(string);
#endif // FILTER_EMOJIS_IOS_KEYBOARD
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@&\n&]){
//判断输入的字是否是回车,即按下return
//在这里做你响应return键的代码
[self hide];
return NO; //这里返回NO,就代表return键值失效,即页面上按下return,不会出现换行,如果为yes,则输入页面会换行
return YES;
- (BOOL)textViewShouldReturn:(UITextView*)textFieldObj
[self hide];
return YES;
- (BOOL)textFieldShouldReturn:(UITextField*)textFieldObj
[self hide];
return YES;
- (void)textInputDone:(id)sender
[self hide];
- (void)textInputCancel:(id)sender
_canceled =
[self hide];
- (BOOL)textViewShouldBeginEditing:(UITextView*)view
#if !UNITY_TVOS
view.inputAccessoryView = viewT
return YES;
#if UNITY_IOS
- (void)keyboardDidShow:(NSNotification*)
if (notification.userInfo == nil || inputView == nil)
CGRect srcRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect rect
= [UnityGetGLView() convertRect:srcRect fromView:nil];
[self positionInput:rect x:rect.origin.x y:rect.origin.y];
_active = YES;
- (void)keyboardWillHide:(NSNotification*)
[self systemHideKeyboard];
- (void)keyboardDidChangeFrame:(NSNotification*)
CGRect srcRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect rect
= [UnityGetGLView() convertRect:srcRect fromView: nil];
if(rect.origin.y &= [UnityGetGLView() bounds].size.height)
[self systemHideKeyboard];
[self positionInput:rect x:rect.origin.x y:rect.origin.y];
+ (void)Initialize
NSAssert(_keyboard == nil, @&[KeyboardDelegate Initialize] called after creating keyboard&);
if(!_keyboard)
_keyboard = [[KeyboardDelegate alloc] init];
+ (KeyboardDelegate*)Instance
if(!_keyboard)
_keyboard = [[KeyboardDelegate alloc] init];
#if UNITY_IOS
struct CreateToolbarResult
UIToolbar*
- (CreateToolbarResult)createToolbarWithView:(UIView*)view
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,160,320, kToolBarHeight)];
UnitySetViewTouchProcessing(toolbar, touchesIgnored);
toolbar.hidden = NO;
UIBarButtonItem* inputItem = view ? [[UIBarButtonItem alloc] initWithCustomView:view] :
UIBarButtonItem* doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(textInputDone:)];
UIBarButtonItem* cancelItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(textInputCancel:)];
NSArray* items = view ? @[inputItem, doneItem, cancelItem] : @[doneItem, cancelItem];
toolbar.items =
inputItem =
doneItem =
cancelItem =
CreateToolbarResult ret = {toolbar, items};
- (id)init
NSAssert(_keyboard == nil, @&You can have only one instance of KeyboardDelegate&);
self = [super init];
#if UNITY_IOS
textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 480, 480, 30)];
textView.delegate =
textView.font = [UIFont systemFontOfSize:18.0];
textView.hidden = YES;
textView.returnKeyType = UIReturnKeyD
textField = [[UITextField alloc] initWithFrame:CGRectMake(0,0,120,30)];
textField.delegate =
textField.borderStyle = UITextBorderStyleRoundedR
textField.font = [UIFont systemFontOfSize:20.0];
textField.clearButtonMode = UITextFieldViewModeWhileE
#define CREATE_TOOLBAR(t, i, v)
CreateToolbarResult res = [self createToolbarWithView:v]; \
} while(0)
#if UNITY_IOS
//CREATE_TOOLBAR(viewToolbar, viewToolbarItems, nil);
CREATE_TOOLBAR(fieldToolbar, fieldToolbarItems, textField);
#undef CREATE_TOOLBAR
#if UNITY_IOS
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textInputDone:) name:UITextFieldTextDidEndEditingNotification object:nil];
- (void) setTextInputTraits: (id&UITextInputTraits&) traits
withParam: (KeyboardShowParam) param
withCap: (UITextAutocapitalizationType) capitalization
traits.keyboardType = param.keyboardT
traits.autocorrectionType = param.autocorrectionT
traits.secureTextEntry = param.
traits.keyboardAppearance = param.
traits.autocapitalizationType =
- (void)setKeyboardParams:(KeyboardShowParam)param
if(_active)
[self hide];
initialText = param.text ? [[NSString alloc] initWithUTF8String: param.text] : @&&;
UITextAutocapitalizationType capitalization = UITextAutocapitalizationTypeS
if(param.keyboardType == UIKeyboardTypeURL || param.keyboardType == UIKeyboardTypeEmailAddress)
capitalization = UITextAutocapitalizationTypeN
#if UNITY_IOS
// _multiline = param.
_multiline =
if (_multiline)
textView.text = initialT
[self setTextInputTraits:textView withParam:param withCap:capitalization];
textField.text = initialT
[self setTextInputTraits:textField withParam:param withCap:capitalization];
textField.placeholder = [NSString stringWithUTF8String:param.placeholder];
inputView = _multiline ? textView : textF
editView = _multiline ? textView : fieldT
#else // UNITY_TVOS
textField.text = initialT
[self setTextInputTraits:textField withParam:param withCap:capitalization];
textField.placeholder = [NSString stringWithUTF8String:param.placeholder];
inputView = textF
editView = textF
[self shouldHideInput:_shouldHideInput];
_canceled = NO;
// we need to show/hide keyboard to react to orientation too, so extract we extract UI fiddling
- (void)showUI
// if we unhide everything now the input will be shown smaller then needed quickly (and resized later)
// so unhide only when keyboard is actually shown (we will update it when reacting to ios notifications)
editView.hidden = YES;
[UnityGetGLView() addSubview:editView];
[inputView becomeFirstResponder];
- (void)hideUI
[inputView resignFirstResponder];
[editView removeFromSuperview];
editView.hidden = YES;
- (void)systemHideKeyboard
_active = editView.isFirstR
editView.hidden = YES;
_area = CGRectMake(0,0,0,0);
- (void)show
[self showUI];
- (void)hide
[self hideUI];
_done = YES;
- (void)updateInputHidden
if(_shouldHideInputChanged)
[self shouldHideInput:_shouldHideInput];
_shouldHideInputChanged =
textField.returnKeyType = _inputHidden ? UIReturnKeyDone : UIReturnKeyD
editView.hidden
= _inputHidden ? YES : NO;
inputView.hidden = _inputHidden ? YES : NO;
#if UNITY_IOS
- (void)positionInput:(CGRect)kbRect x:(float)x y:(float)y
if(_multiline)
// use smaller area for iphones and bigger one for ipads
//int height = UnityDeviceDPI() & 300 ? 75 : 100;
//editView.frame = CGRectMake(0, y - kToolBarHeight, kbRect.size.width, height);
int height = UnityDeviceDPI() & 300 ? 38 : 100;
editView.frame = CGRectMake(0, y - height, kbRect.size.width, height);
statusFrame = [UIApplication sharedApplication].statusBarF
unsigned statusHeight = statusFrame.size.
editView.frame = CGRectMake(0, y - kToolBarHeight - statusHeight, kbRect.size.width, kToolBarHeight);
inputView.frame = CGRectMake(inputView.frame.origin.x,
inputView.frame.origin.y,
kbRect.size.width - 3*18 - 2*50,
inputView.frame.size.height);
_area = CGRectMake(x, y, kbRect.size.width, kbRect.size.height);
[self updateInputHidden];
- (CGRect)queryArea
return editView.hidden ? _area : CGRectUnion(_area, editView.frame);
+ (void)StartReorientation
if(_keyboard && _keyboard.active)
[CATransaction begin];
[_keyboard hideUI];
[CATransaction commit];
// not pretty but seems like easiest way to keep &we are rotating& status
_keyboard-&_rotating = YES;
+ (void)FinishReorientation
if(_keyboard && _keyboard-&_rotating)
[CATransaction begin];
[_keyboard showUI];
[CATransaction commit];
_keyboard-&_rotating = NO;
- (NSString*)getText
if (_canceled)
return initialT
#if UNITY_TVOS
return [textField text];
return _multiline ? [textView text] : [textField text];
- (void) setTextWorkaround:(id&UITextInput&)textInput text:(NSString*)newText
UITextPosition* begin = [textInput beginningOfDocument];
UITextPosition* end = [textInput endOfDocument];
UITextRange* allText = [textInput textRangeFromPosition:begin toPosition:end];
[textInput setSelectedTextRange:allText];
[textInput insertText:newText];
- (void)setText:(NSString*)newText
#if UNITY_IOS
// We can't use setText on iOS7 because it does not update the undo stack.
// We still prefer setText on other iOSes, because an undo operation results
// in a smaller selection shown on the UI
if(_ios70orNewer && !_ios80orNewer)
[self setTextWorkaround: (_multiline ? textView : textField) text:newText];
if(_multiline)
textView.text = newT
textField.text = newT
textField.text = newT
- (void)shouldHideInput:(BOOL)hide
switch(keyboardType)
case UIKeyboardTypeDefault:
hide = YES;
case UIKeyboardTypeASCIICapable:
hide = YES;
case UIKeyboardTypeNumbersAndPunctuation:
hide = YES;
case UIKeyboardTypeURL:
hide = YES;
case UIKeyboardTypeNumberPad:
hide = NO;
case UIKeyboardTypePhonePad:
hide = NO;
case UIKeyboardTypeNamePhonePad:
hide = NO;
case UIKeyboardTypeEmailAddress:
hide = YES;
hide = NO;
_inputHidden =
//==============================================================================
Unity Interface:
extern &C& void UnityKeyboard_Create(unsigned keyboardType, int autocorrection, int multiline, int secure, int alert, const char* text, const char* placeholder)
#if UNITY_TVOS
// Not supported. The API for showing keyboard for editing multi-line text
// is not available on tvOS
multiline =
static const UIKeyboardType keyboardTypes[] =
UIKeyboardTypeDefault,
UIKeyboardTypeASCIICapable,
UIKeyboardTypeNumbersAndPunctuation,
UIKeyboardTypeURL,
UIKeyboardTypeNumberPad,
UIKeyboardTypePhonePad,
UIKeyboardTypeNamePhonePad,
UIKeyboardTypeEmailAddress,
static const UITextAutocorrectionType autocorrectionTypes[] =
UITextAutocorrectionTypeNo,
UITextAutocorrectionTypeDefault,
static const UIKeyboardAppearance keyboardAppearances[] =
UIKeyboardAppearanceDefault,
UIKeyboardAppearanceAlert,
KeyboardShowParam param =
text, placeholder,
keyboardTypes[keyboardType],
autocorrectionTypes[autocorrection],
keyboardAppearances[alert],
(BOOL)multiline, (BOOL)secure
[[KeyboardDelegate Instance] setKeyboardParams:param];
extern &C& void UnityKeyboard_Show()
// do not send hide if didnt create keyboard
// TODO: probably assert?
if(!_keyboard)
[[KeyboardDelegate Instance] show];
extern &C& void UnityKeyboard_Hide()
// do not send hide if didnt create keyboard
// TODO: probably assert?
if(!_keyboard)
[[KeyboardDelegate Instance] hide];
extern &C& void UnityKeyboard_SetText(const char* text)
[KeyboardDelegate Instance].text = [NSString stringWithUTF8String: text];
extern &C& NSString* UnityKeyboard_GetText()
return [KeyboardDelegate Instance].
extern &C& int UnityKeyboard_IsActive()
return (_keyboard && _keyboard.active) ? 1 : 0;
extern &C& int UnityKeyboard_IsDone()
return (_keyboard && _keyboard.done) ? 1 : 0;
extern &C& int UnityKeyboard_WasCanceled()
return (_keyboard && _keyboard.canceled) ? 1 : 0;
extern &C& void UnityKeyboard_SetInputHidden(int hidden)
_shouldHideInput
_shouldHideInputChanged =
// update hidden status only if keyboard is on screen to avoid showing input view out of nowhere
if(_keyboard && _keyboard.active)
[_keyboard updateInputHidden];
extern &C& int UnityKeyboard_IsInputHidden()
return _shouldHideInput ? 1 : 0;
extern &C& void UnityKeyboard_GetRect(float* x, float* y, float* w, float* h)
CGRect area = _keyboard ? _keyboard.area : CGRectMake(0,0,0,0);
// convert to unity coord system
float multX = (float)GetMainDisplaySurface()-&targetW / UnityGetGLView().bounds.size.
float multY = (float)GetMainDisplaySurface()-&targetH / UnityGetGLView().bounds.size.
*y = area.origin.y * multY;
*w = area.size.width * multX;
*h = area.size.height * multY;
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:4227次
排名:千里之外
(1)(1)(1)(1)(5)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'

我要回帖

更多关于 r in action 中文版 的文章

 

随机推荐