Unity3d game engine senses iOS device rotation and iPhone Keyboard Events
Original by Yu Song MomoArticleIf you reprint it, please note: Reprinted to my independent domain name blogYusong 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
Void start () {// portrait up and down two directions iphonekeyboard. autorotatetoportrait = false; iphonekeyboard. autorotatetoportraitupsidedown = false; // The iphonekeyboard is displayed horizontally in the upper and lower directions. 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
Void Update () {// process horizontal rotation of 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.
Using unityengine; using system. collections; public class main: monobehaviour {// enter private iphonekeyboard keyboard on the keyboard; // font skin public guiskin fontskin; // use this for initializationvoid start () {} // update is called once per framevoid Update () {} void ongui () {// set the skin GUI. skin = fontskin; // force the portrait 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 // forces 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. po Rtraitupsidedown;} If (GUI. button (New rect (320, 10,300,100), "Open keyboard") {// open the iPhone input box // The first parameter displays test // The second parameter sets the input box type by default, the default value is here. You can enter keyboard = iphonekeyboard for everything. 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 );}}}}
IphonekeyboardtypeThere are several important parameters for the keyboard type. You can try it to see the effect. I cannot ~
Iphonekeyboardtype. numbersandpunctuation:Enter punctuation marks and numbers
Iphonekeyboardtype. Url:Enter URL
Iphonekeyboardtype. Phonepad:Enter 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
Finally, you are welcome to discuss unity3d game development with Momo. In general, this chapter is relatively simple,CodeI will not upload it. Wow ~ I strongly appreciate the technical support from the four-core technical support team ~ I would like to learn from you !!!