Http://blog.csdn.net/xys289187120/article/details/6993977
Unity3d game engine senses iOS device rotation and iPhone Keyboard Events
If the original Yusong Momo article is reprinted, please note: Reprinted to my independent domain name blog Yusong Momo program Research Institute, Original address: http://www.xuanyusong.com/archives/556
IPhone iPad itouch rotation devices support arbitrary rotation of the screen in four directions, so the powerful unity3d game engine is also supported. Although many games force screen rotation to avoid troubles, but as for learning, we still know it as well, because unity3d is very convenient to process screen rotation. The following Momo will introduce the unity3d screen to you in an example ~~.
Force the screen to rotate in four directions
[CSHARP]View plaincopy
- Void start (){
- // Vertical up and down
- Iphonekeyboard. autorotatetoportrait = false;
- Iphonekeyboard. autorotatetoportraitupsidedown = false;
- // Horizontal up and down
- Iphonekeyboard. autorotatetolandscapeleft = false;
- Iphonekeyboard. autorotatetolandscaperight = false;
- }
This method is applicable to unity3.3 and the following versions.
Input. deviceorientation can be used to obtain the orientation of the current iOS device screen.
Screen. Orientation
[CSHARP]View plaincopy
- Void Update (){
- // Process horizontal rotation in two directions
- If (input. deviceorientation = deviceorientation. landscapeleft)
- {
- If (screen. Orientation! = Screenorientation. landscapeleft ){
- Screen. Orientation = screenorientation. landscapeleft;
- }
- } Else if (input. deviceorientation = deviceorientation. landscaperight)
- {
- If (screen. Orientation! = Screenorientation. landscaperight ){
- Screen. Orientation = screenorientation. landscaperight;
- }
- } Else
- // Process the Vertical Rotation
- If (input. deviceorientation = deviceorientation. Portrait)
- {
- If (screen. Orientation! = Screenorientation. Portrait ){
- Screen. Orientation = screenorientation. portrait;
- }
- } Else if (input. deviceorientation = deviceorientation. portraitupsidedown)
- {
- If (screen. Orientation! = Screenorientation. portraitupsidedown ){
- Screen. Orientation = screenorientation. portraitupsidedown;
- }
- }
- }
For versions 3.4 and later, you can directly set screen rotation in setting for iOS settings.
In the following example, the screen rotation status is directly switched through the button on the left, and the button on the right opens the iPhone input status box.
[CSHARP]View plaincopy
- Using unityengine;
- Using system. collections;
- Public class main: monobehaviour {
- // Keyboard input
- Private iphonekeyboard keyboard;
- // Font skin
- Public guiskin fontskin;
- // Use this for initialization
- Void start (){
- }
- // Update is called once per frame
- Void Update (){
- }
- Void ongui (){
- // Set skin
- Gui. Skin = fontskin;
- // Force the portrait of the screen
- If (GUI. Button (New rect (10, 10,300,100), "Change landscapeleft ")){
- Screen. Orientation = screenorientation. landscapeleft;
- } Else if (GUI. Button (New rect (10,110,300,100), "Change landscaperight ")){
- Screen. Orientation = screenorientation. landscaperight;
- } Else
- // Force the screen landscape
- If (GUI. Button (New rect (10,210,300,100), "change portrait ")){
- Screen. Orientation = screenorientation. portrait;
- } Else if (GUI. Button (New rect (10,310,300,100), "Change portraitupsidedown ")){
- Screen. Orientation = screenorientation. portraitupsidedown;
- }
- If (GUI. Button (New rect (320, 10,300,100), "Open keyboard ")){
- // Open the iPhone input box
- // The first parameter is displayed as test by default.
- // Set the type of the input box for the second parameter. this parameter is set to the default value and can be entered at any time.
- Keyboard = iphonekeyboard. Open ("test", iphonekeyboardtype. Default );
- }
- If (keyboard! = NULL ){
- If (keyboard. Done ){
- // After the input is complete, Click Done to enter the input content.
- Debug. Log (keyboard. Text );
- }
- }
- }
- }
There are several important parameters for the iPhone keyboardtype keyboard type. You can try it to see the effect. I cannot ~
Iphonekeyboardtype. numbersandpunctuation: Enter punctuation marks and numbers
Iphonekeyboardtype. url: Enter the URL
Iphonekeyboardtype. Phonepad: Enter the phone number
Iphonekeyboardtype. Numberpad: enter a number
Iphonekeyboardtype. Emailaddress: enter email
The screen direction not only senses the four directions of the IOS device plane, but also the screen up and down directions. Screen face up: landscapeleft. faceup
Screen Down: landscapeleft. Facedown