2015-12-08 20:34:54 u012322710 阅读数 2065

 自学Unity3d其实已经有1年多了,一直在练习,没有一个完整的游戏,最近花了4天来完成一个小游戏,九宫拼图,游戏很简单,代码其实1天半就写完了,主要是美工,不想搞的太粗糙,从网上下了的素材PS了一下。

  总结一下,格子是3*3的,但是要考虑要二维数组循环的方便不越界,用5*5的,两边去掉墙壁来判断。空格子的ID是-1,小拼图弄个从1---8的ID,墙壁是0  。UI用的UGUI,屏幕适应比NGUI感觉要方便点。格子的移动要用GetComponent<RectTransform>().anchoredPosition3D 来移动。其他都很好做。


2017-09-14 16:13:32 jianda244 阅读数 1178

九宫格的两个条件

1.转成Sprite格式


2.编辑九宫格



自定义九宫格完成后需要保存


3.Image组件设置为sliced模式


根据绿色顶点拉伸自定义九宫格



下面是效果图



2016-09-12 15:35:44 HeBiReChenAi 阅读数 4691

鉴于之前写的关于这篇博客,大家提出了一些问题,当时楼主自己电脑上看的是没问题,没大在意,最近一些朋友再次提出了这个问题,楼主这里集中修正了下:


修正时Unity的版本:

Unity 2017.2.0f3


问题:

刚开始画的时候会多出一条线?


这里楼主找了下原因,刚开始的脚本这个数值测试的时候设置的2,实际上应该设为0;楼主改了下不会再出现多一条线的问题,大家也试试,



后面楼主也发现了一些其他问题:

1.llineRender的脚本更新:


更正后的脚本如图:



2.点击一个按钮后停止后再次按下时会重复记录,楼主在按下时,加了句密码清空:



目前想到的就这些了,大家还有什么疑问尽管提出来,虚心求教,旨在更好的技术交流。

另外附上更新后的脚本包:

工程源码2.0

=========================分割线下面为2016.9.12日编写=======================================================

好久不更博客了,最近闲的无聊,做了个九宫格手机手势解锁密码的demo.

先上图:


虽然有点丑,但是功能还是到了哈哈偷笑,制作起来很简单,两个脚本就够了,目录结构如图



说一下思路哈,首先界面上用UGUI做成九个按钮,用它的排序列表排成3*3的,用lineRender画线。然后逻辑上图案密码实际上保存的就是数字,每个按钮对应一个数字,保存的密码实际上是数字的顺序。

下面详细说明

1.首先新建的按钮下的text删除,换成image放进去,然后把按钮图片和image的图片都换成knob,系统自带的图片,调整按钮的alpha.

效果如下:



2.将这个按钮复制8个,名字改为1-9.

效果如图:



3.新建一个空物体,叫Buttons,将9个按钮拖到它下面,然后再Buttons上添加GridLayoutGroup脚本,设置行列和间距

效果如图:





这样基本的显示建立好了


4.建立一个空物体命名为line,然后添加lineRender组件,并新建材质添加到lineRender上,线的大小和宽度或者图片好看可以自己设置,楼主懒得弄,就设了个颜色。


5.现在开始写代码了,新建一个脚本BtnsControl,脚本内容如下,具体的看注释,有疑问或者可以优化的可以评论中给我建议。


using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class BtnsControl : MonoBehaviour
{
    //line render 相关
    [Tooltip("Line renderer used for the line drawing.")]
    public LineRenderer line;
    private int lineVertexIndex = 2;

    //密码锁的按钮组
    public Button[] btns;

    public Button btnOK;

    //密码保存
    public List<string> password = new List<string>();

    // Use this for initialization
    void Start()
    {
        //绑定按钮事件,这里随便试了一个
        for (int i = 0; i < btns.Length; i++)
        {
            UIEventListener btnListenser = btns[i].GetComponent<UIEventListener>();
            btnListenser.OnMouseEnter += whenMouseEnter;
            btnListenser.OnMouseDown += whenMouseDown;
            //btnListenser.OnMouseExit += whenMouseExit;
        }

        UIEventListener btnokListenser = btnOK.GetComponent<UIEventListener>();
        btnokListenser.OnClick += OnOKBtnClick;
    }

    #region 处理开始和结束
    bool IsStart;
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            IsStart = true;
        }
        else
        {
            IsStart = false;
        }
    }
    #endregion

    void whenMouseEnter(GameObject go)
    {
        if (IsStart == false)
            return;

        Debug.Log("enter button  " + go.name);
        go.GetComponent<Image>().color = go.GetComponent<Button>().colors.highlightedColor;

        int i = int.Parse(go.name);

        password.Add(go.name);

        DrawLines(i - 1);
    }

    #region 处理意外
    //处理鼠标按下时,刚好在按钮上的意外
    void whenMouseDown(GameObject go)
    {
        IsStart = true;
        whenMouseEnter(go);
    }
    #endregion

    // 画按钮之间的连线
    void DrawLines(int btnIndex)
    {
        if (line != null)
        {
            lineVertexIndex++;
            line.SetVertexCount(lineVertexIndex);

            Vector3 cursorPos = btns[btnIndex].gameObject.transform.position;
            cursorPos.z = 0f;

            Vector3 cursorSpacePos = Camera.main.ScreenToWorldPoint(cursorPos);
            cursorSpacePos.z = 0f;
            line.SetPosition(lineVertexIndex - 1, cursorSpacePos);
        }
    }

    //输出设置的密码,可做后续处理,保存校对的啥的,可以继续延伸
    void OnOKBtnClick(GameObject go)
    {
        string pass = "";
        foreach (var item in password)
        {
            pass += item;
        }

        Debug.Log("您设置的密码是:" + pass);
    }
}


其中涉及到了按钮绑定的脚本,脚本叫UIEventListener,内容如下:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;

public class UIEventListener : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler,IPointerDownHandler
{

    /// <summary>
    /// 定义事件代理
    /// </summary>
    /// <param name="gb"></param>
    public delegate void UIEventProxy(GameObject gb);

    

    /// <summary>
    /// 鼠标点击事件
    /// </summary>
    public event UIEventProxy OnClick;

    /// <summary>
    /// 鼠标进入事件
    /// </summary>
    public event UIEventProxy OnMouseEnter;

    /// <summary>
    /// 鼠标滑出事件
    /// </summary>
    public event UIEventProxy OnMouseExit;

    /// <summary>
    /// 鼠标按下事件
    /// </summary>
    public event UIEventProxy OnMouseDown;

    public void OnPointerClick(PointerEventData eventData)
    {
        if (OnClick != null)
            OnClick(this.gameObject);
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if (OnMouseEnter != null)
            OnMouseEnter(this.gameObject);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if (OnMouseExit != null)
            OnMouseExit(this.gameObject);
    }


    public void OnPointerDown(PointerEventData eventData)
    {
        if (OnMouseDown != null)
            OnMouseDown(this.gameObject);
    }
}
这个要自己写,总觉得ugui用的不太习惯,NGUI这部分是插件里写好了的。


6.给自己的9个button添加UIEventListener,然后Buttons控件添加BtnsControl脚本,并拖入相应参数,其中的okbutton顺手在场景里建立一个放好就好了。


这样就可以了,运行起来就能设置密码了,点击ok按钮可以看到自己设置的密码实际上就是变相的数字密码。

最终效果如图:


至于,创建密码,保存密码,校对密码这里我就不说了,实现起来很简单。


谢谢支持。


工程源码

2016-07-30 22:47:55 xieyanbill 阅读数 2014

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
#endif

static KeyboardDelegate*	_keyboard = nil;

static bool					_shouldHideInput = false;
static bool					_shouldHideInputChanged = false;
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*		textView;

	UIToolbar*		viewToolbar;
	NSArray*		viewToolbarItems;
#endif

	UITextField*	textField;

	// keep toolbar items for both single- and multi- line edit in NSArray to make sure they are kept around
#if UNITY_IOS
	UIToolbar*		fieldToolbar;
	NSArray*		fieldToolbarItems;
#endif

	// 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]
	UIView*			inputView;
	UIView*			editView;


	CGRect			_area;
	NSString*		initialText;

	UIKeyboardType	keyboardType;

	BOOL			_multiline;
	BOOL			_inputHidden;
	BOOL			_active;
	BOOL			_done;
	BOOL			_canceled;

	BOOL			_rotating;
}

@synthesize area;
@synthesize active		= _active;
@synthesize done		= _done;
@synthesize canceled	= _canceled;
@synthesize text;

// 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;
				}
			}
			else
			{
				// 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 returnValue;
}

// 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 = true;
	[self hide];
}

- (BOOL)textViewShouldBeginEditing:(UITextView*)view
{
#if !UNITY_TVOS
	view.inputAccessoryView = viewToolbar;
#endif
	return YES;
}

#if UNITY_IOS
- (void)keyboardDidShow:(NSNotification*)notification;
{
	if (notification.userInfo == nil || inputView == nil)
		return;

	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*)notification;
{
	[self systemHideKeyboard];
}
- (void)keyboardDidChangeFrame:(NSNotification*)notification;
{
	_active = true;

	CGRect srcRect	= [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
	CGRect rect		= [UnityGetGLView() convertRect:srcRect fromView: nil];

	if(rect.origin.y >= [UnityGetGLView() bounds].size.height)
		[self systemHideKeyboard];
	else
		[self positionInput:rect x:rect.origin.x y:rect.origin.y];
}
#endif

+ (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];

	return _keyboard;
}

#if UNITY_IOS
struct CreateToolbarResult
{
	UIToolbar*	toolbar;
	NSArray*	items;
};
- (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] : nil;
	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 = items;

	inputItem = nil;
	doneItem = nil;
	cancelItem = nil;

	CreateToolbarResult ret = {toolbar, items};
	return ret;
}
#endif

- (id)init
{
	NSAssert(_keyboard == nil, @"You can have only one instance of KeyboardDelegate");
	self = [super init];
	if(self)
	{
#if UNITY_IOS
		textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 480, 480, 30)];
		textView.delegate = self;
		textView.font = [UIFont systemFontOfSize:18.0];
		textView.hidden = YES;
        textView.returnKeyType = UIReturnKeyDone;

#endif

		textField = [[UITextField alloc] initWithFrame:CGRectMake(0,0,120,30)];
		textField.delegate = self;
		textField.borderStyle = UITextBorderStyleRoundedRect;
		textField.font = [UIFont systemFontOfSize:20.0];
		textField.clearButtonMode = UITextFieldViewModeWhileEditing;

		#define CREATE_TOOLBAR(t, i, v)									\
		do {															\
			CreateToolbarResult res = [self createToolbarWithView:v];	\
			t = res.toolbar;											\
			i = res.items;												\
		} while(0)

#if UNITY_IOS
		//CREATE_TOOLBAR(viewToolbar, viewToolbarItems, nil);
		CREATE_TOOLBAR(fieldToolbar, fieldToolbarItems, textField);
#endif

		#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];
#endif

		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textInputDone:) name:UITextFieldTextDidEndEditingNotification object:nil];
	}

	return self;
}

- (void) setTextInputTraits: (id<UITextInputTraits>) traits
				  withParam: (KeyboardShowParam) param
					withCap: (UITextAutocapitalizationType) capitalization
{
	traits.keyboardType	= param.keyboardType;
	traits.autocorrectionType = param.autocorrectionType;
	traits.secureTextEntry = param.secure;
	traits.keyboardAppearance = param.appearance;
	traits.autocapitalizationType = capitalization;
}


- (void)setKeyboardParams:(KeyboardShowParam)param
{
	if(_active)
		[self hide];

	initialText = param.text ? [[NSString alloc] initWithUTF8String: param.text] : @"";

	UITextAutocapitalizationType capitalization = UITextAutocapitalizationTypeSentences;
	if(param.keyboardType == UIKeyboardTypeURL || param.keyboardType == UIKeyboardTypeEmailAddress)
		capitalization = UITextAutocapitalizationTypeNone;

#if UNITY_IOS
//	_multiline = param.multiline;
    _multiline = true;

	if (_multiline)
	{
		textView.text = initialText;
		[self setTextInputTraits:textView withParam:param withCap:capitalization];
	}
	else
	{
		textField.text = initialText;
		[self setTextInputTraits:textField withParam:param withCap:capitalization];
		textField.placeholder = [NSString stringWithUTF8String:param.placeholder];
	}
	inputView = _multiline ? textView : textField;
	editView = _multiline ? textView : fieldToolbar;

#else // UNITY_TVOS
	textField.text = initialText;
	[self setTextInputTraits:textField withParam:param withCap:capitalization];
	textField.placeholder = [NSString stringWithUTF8String:param.placeholder];
	inputView = textField;
	editView = textField;
#endif

    [self shouldHideInput:_shouldHideInput];

	_done		= NO;
	_canceled	= NO;
	_active		= YES;
}

// 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.isFirstResponder;
	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 = false;
	}

	textField.returnKeyType = _inputHidden ? UIReturnKeyDone : UIReturnKeyDefault;

	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);
	}
	else
	{
		CGRect   statusFrame	= [UIApplication sharedApplication].statusBarFrame;
		unsigned statusHeight	= statusFrame.size.height;

		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];
}
#endif

- (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 initialText;
	else
	{
#if UNITY_TVOS
		return [textField text];
#else
		return _multiline ? [textView text] : [textField text];
#endif
	}
}

- (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 = newText;
	else
		textField.text = newText;
#else
	textField.text = newText;
#endif
}

- (void)shouldHideInput:(BOOL)hide
{
	if(hide)
	{
		switch(keyboardType)
		{
			case UIKeyboardTypeDefault:                 hide = YES;	break;
			case UIKeyboardTypeASCIICapable:            hide = YES;	break;
			case UIKeyboardTypeNumbersAndPunctuation:   hide = YES;	break;
			case UIKeyboardTypeURL:                     hide = YES;	break;
			case UIKeyboardTypeNumberPad:               hide = NO;	break;
			case UIKeyboardTypePhonePad:                hide = NO;	break;
			case UIKeyboardTypeNamePhonePad:            hide = NO;	break;
			case UIKeyboardTypeEmailAddress:            hide = YES;	break;
			default:                                    hide = NO;	break;
		}
	}

	_inputHidden = hide;
}

@end



//==============================================================================
//
//  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 = false;
#endif

	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)
		return;

	[[KeyboardDelegate Instance] show];
}
extern "C" void UnityKeyboard_Hide()
{
	// do not send hide if didnt create keyboard
	// TODO: probably assert?
	if(!_keyboard)
		return;

	[[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].text;
}

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		= hidden;
	_shouldHideInputChanged	= true;

	// 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.width;
	float	multY	= (float)GetMainDisplaySurface()->targetH / UnityGetGLView().bounds.size.height;

	*x = 0;
	*y = area.origin.y * multY;
	*w = area.size.width * multX;
	*h = area.size.height * multY;
}


2020-01-05 11:03:11 weixin_43333566 阅读数 10

九宫格
中心的区域会被拉伸的(四方拉伸) 四角的区域会保持不变的 中间的区域分别做上下和左右的拉伸
在这里插入图片描述
下图就是设置完成后的效果
在这里插入图片描述
图片没有变得模糊不清楚,不损失像素,打开九宫格编辑的正确姿势,首先在Project面板选中需要进行修改的图片,然后在Inspect面板选中Sprite Editor就出现了界面,在完成后点击Apply就可以了。
这个时候图片变成了九宫格的模式,在进行复制一个当前的图片,可以把Image Type设置成为Simple就变成了原始的图片,把图片的Scale设置成为-1就出现了对称的效果。

unity3d NGUI 九宫格

阅读数 1259

没有更多推荐了,返回首页