Unity3d game engine senses iOS device rotation and iPhone Keyboard Events (16)

Source: Internet
Author: User
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

  1. Void start (){
  2. // Vertical up and down
  3. Iphonekeyboard. autorotatetoportrait = false;
  4. Iphonekeyboard. autorotatetoportraitupsidedown = false;
  5. // Horizontal up and down
  6. Iphonekeyboard. autorotatetolandscapeleft = false;
  7. Iphonekeyboard. autorotatetolandscaperight = false;
  8. }

 

 

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

  1. Void Update (){
  2. // Process horizontal rotation in two directions
  3. If (input. deviceorientation = deviceorientation. landscapeleft)
  4. {
  5. If (screen. Orientation! = Screenorientation. landscapeleft ){
  6. Screen. Orientation = screenorientation. landscapeleft;
  7. }
  8. } Else if (input. deviceorientation = deviceorientation. landscaperight)
  9. {
  10. If (screen. Orientation! = Screenorientation. landscaperight ){
  11. Screen. Orientation = screenorientation. landscaperight;
  12. }
  13. } Else
  14. // Process the Vertical Rotation
  15. If (input. deviceorientation = deviceorientation. Portrait)
  16. {
  17. If (screen. Orientation! = Screenorientation. Portrait ){
  18. Screen. Orientation = screenorientation. portrait;
  19. }
  20. } Else if (input. deviceorientation = deviceorientation. portraitupsidedown)
  21. {
  22. If (screen. Orientation! = Screenorientation. portraitupsidedown ){
  23. Screen. Orientation = screenorientation. portraitupsidedown;
  24. }
  25. }
  26. }

 

 

 

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

  1. Using unityengine;
  2. Using system. collections;
  3. Public class main: monobehaviour {
  4. // Keyboard input
  5. Private iphonekeyboard keyboard;
  6. // Font skin
  7. Public guiskin fontskin;
  8. // Use this for initialization
  9. Void start (){
  10. }
  11. // Update is called once per frame
  12. Void Update (){
  13. }
  14. Void ongui (){
  15. // Set skin
  16. Gui. Skin = fontskin;
  17. // Force the portrait of the screen
  18. If (GUI. Button (New rect (10, 10,300,100), "Change landscapeleft ")){
  19. Screen. Orientation = screenorientation. landscapeleft;
  20. } Else if (GUI. Button (New rect (10,110,300,100), "Change landscaperight ")){
  21. Screen. Orientation = screenorientation. landscaperight;
  22. } Else
  23. // Force the screen landscape
  24. If (GUI. Button (New rect (10,210,300,100), "change portrait ")){
  25. Screen. Orientation = screenorientation. portrait;
  26. } Else if (GUI. Button (New rect (10,310,300,100), "Change portraitupsidedown ")){
  27. Screen. Orientation = screenorientation. portraitupsidedown;
  28. }
  29. If (GUI. Button (New rect (320, 10,300,100), "Open keyboard ")){
  30. // Open the iPhone input box
  31. // The first parameter is displayed as test by default.
  32. // 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.
  33. Keyboard = iphonekeyboard. Open ("test", iphonekeyboardtype. Default );
  34. }
  35. If (keyboard! = NULL ){
  36. If (keyboard. Done ){
  37. // After the input is complete, Click Done to enter the input content.
  38. Debug. Log (keyboard. Text );
  39. }
  40. }
  41. }
  42. }
 

 

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.