[Unity3D] Unity3D game development screenshots save wonderful moments, unity3d highlights moments
Dear friends, I'm Qin Yuanpei. Welcome to follow my blog. My blog address is blog.csdn.net/qinyuanpei. Today, let's do something simple. What should we do? The answer is screenshots. As a single-host game player, the blogger captures the favorite pictures of the game each time while playing the game, especially after learning Unity3D, when playing games, we will look at the game from a game designer's perspective. In other words, we may focus more on the technical components. For example, when a blogger writes "role control from the free perspective of [Unity3D] Unity3D game development" and "role control of [Unity3D] Unity3D game development, when you encounter problems that cannot be grasped by bloggers, you can experience it through playing games. During the game, bloggers always capture pictures that bloggers prefer ,, the following sections are collected by bloggers:
Now, after enjoying the beautiful scenery, let's learn how to implement screenshots in Unity3D. We will first provide three ways to implement screenshots:
/// <Summary> /// use the CaptureScreenshot () method in the Application class to implement this method. /// advantages: simple, can quickly capture a frame of the screen, full screen // disadvantages: not for the camera, unable to perform local // </summary> // <param name = "mFileName"> M file name. </param> private void CaptureByUnity (string mFileName) {Application. captureScreenshot (mFileName, 0) ;}/// <summary> /// capture the screen in the specified range based on a Rect type. // the lower left corner is (0, 0) /// </summary> /// <param name = "mRect"> M rect. </param> /// <param name = "mFileName"> M file name. </param> private IEnumerator CaptureByRect (Rect mRect, string mFileName) {// wait until the rendering thread ends yield return new WaitForEndOfFrame (); // initialize javasmtexture = new Texture2D (int) mRect. width, (int) mRect. height, TextureFormat. RGB24, false); // read the pixel information on the screen and store it as the texture data mTexture. readPixels (mRect, 0, 0); // apply mTexture. apply (); // encode the image information as byte [] bytes = mTexture. encodeToPNG (); // save System. IO. file. writeAllBytes (mFileName, bytes); // if needed, return // return mTexture;} private IEnumerator CaptureByCamera (Camera mCamera, Rect mRect, string mFileName) {// wait until the rendering thread ends. yield return new WaitForEndOfFrame (); // initialize RenderTextureRenderTexture mRender = new RenderTexture (int) mRect. width, (int) mRect. height, 0); // sets the camera's objective mcamera.tar getTexture = mRender; // starts rendering mCamera. render (); // activate the rendering texture to read information RenderTexture. active = mRender; Texture2D mTexture = new Texture2D (int) mRect. width, (int) mRect. height, TextureFormat. RGB24, false); // read the pixel information on the screen and store it as the texture data mTexture. readPixels (mRect, 0, 0); // apply mTexture. apply (); // deploy the camera, and deploy the image mcamera.tar getTexture = null; RenderTexture. active = null; GameObject. destroy (mRender); // encode the image information as byte information. byte [] bytes = mTexture. encodeToPNG (); // save System. IO. file. writeAllBytes (mFileName, bytes); // if needed, return // return mTexture ;}}
Next, let's call these three methods to implement a simple example:
// Define the image storage path private string mPath1; private string mPath2; private string mPath3; // camera public Transform CameraTrans; void Start () {// initialization path mPath1 = Application. dataPath + "\ ScreenShot \ ScreenShot1.png"; mPath2 = Application. dataPath + "\ ScreenShot \ ScreenShot2.png"; mPath3 = Application. dataPath + "\ ScreenShot \ ScreenShot3.png";} // main method. Use UGUI to implement void OnGUI () {if (GUILayout. button ("method 1", GUILayout. height (30) {CaptureByUnity (mPath1);} if (GUILayout. button ("method 2", GUILayout. height (30) {StartCoroutine (CaptureByRect (new Rect (1024,768,), mPath2);} if (GUILayout. button ("method 3", GUILayout. height (30) {// enable CameraTrans. camera. enabled = true; // disable the Camera. main. enabled = false; StartCoroutine (CaptureByCamera (CameraTrans. camera, new Rect (1024,768,), mPath3 ));}}
In the third method, the blogger places a camera named TopCamera in the scene, which is vertically projected down to the game scene, so that players can see the top view of the scene. Here we use this camera to test the third method. Activate the camera first. Scenario settings
Let's take a look at the effects of the three methods:
From the perspective of the effect, the first method is the best, but customization is a problem. The second method works normally. I feel that TextureFormat is not selected here. The third effect basically meets the requirements of bloggers, but the camera's projection range does not seem to be well designed. Here we find that the second one will render the editor window into it. The blogger thinks that even if the Game window is placed to the maximum when the program is running, it will still be affected by the window, then, the blogger compiles the program into an executable file. However, after the program is run, the blogger does not find the corresponding file. Later, I found the official API to find out the reason:
Description
Contains the path to the game data folder (Read Only ).
The value depends on which platform you are running on:
Unity Editor:<
Path to project folder>/Assets
Mac player:<
Path to player app bundle>/Contents
IPhone player:<
Path to player app bundle>/<
AppName. app>/Data
Win player:<
Path to executablename_Data folder>
Web player:The absolute url to the player data file folder (without the actual data file name)
Flash:The absolute url to the player data file folder (without the actual data file name)
Note that the string returned on a PC will use a forward slash as a folder separator
Obviously, we can know the location of Application. datapath on different platforms. For executable files (.exe, Windows Platform), it corresponds to a folder corresponding to the application. For example, if the executable file is named UnityGame, the corresponding location is the UnityGame_Data file. Therefore, the problem with the blogger should be that the ScreenShot folder is not created here. I hope you will pay attention to it when you are working on related projects. Well, this is what we are talking about today. I hope you will like it.
Daily proverbs: It doesn't matter whether it is good or bad. Life is a void dream. Only Tian daoheng
In the reciprocating cycle, never changed-Murong Ziying.
If you like my blog, please remember my name: Qin Yuanpei. My blog address is blog.csdn.net/qinyuanpei.
Reprinted please indicate the source, Author: Qin Yuanpei, the source of this article: http://blog.csdn.net/qinyuanpei/article/details/39185195
[Unity3D] mobile 3D Game Development: how to use gravity sensing in Unity3D
Wang wanghai's laboratory, Shanghai laboratory-various graphics experiments, data structure experiments, and other trivial little notes are all gathered here for 0-various graphics experiments and data structures the lab and all other trivial notes are gathered here for 0 error (s ), 0 warning (s) the arrival of this magic moment error (s), 0 warning (s) the arrival of this magic moment learning Unity script is recommended: unity3D indexing gravity sensing is very common in mobile game development. Unity3D is a collection of gravity sensing related content. A simple JS script demonstrates the use of gravity sensing. CSDNGravity. js: // object texture var round: Texture2D; // The x y coordinate var x = 0 displayed on the screen; var y = 0; // maximum x y range displayed on the Object Screen var cross_x = 0; var cross_y = 0; function Start () {// initialization value cross_x = Screen. width-round. width; cross_y = Screen. height-round. height;} function OnGUI () {// The overall x y z gravity sensing gravity component GUI is displayed. label (Rect (0,0, 480,100), "position is" + Input. acceleration); // draw the object GUI. drawTexture (Rect (x, y, 256,256), round);} function Update ( ) {// Modify the object position based on the gravity component. Multiply the value by 30 here to make the object move faster x ++ = Input. acceleration. x * 30; y + =-Input. acceleration. y * 30; // avoid an object exceeding the screen if (x <0) {x = 0;} else if (x> cross_x) {x = cross_x ;} if (y <0) {y = 0;} else if (y> cross_y) {y = cross_y ;}} the Input here refers to the Input in Unity, acceleration is its gravity, and x and y represent its gravity. After creating the image, you only need to add the texture image: 12
Unity3D game development instances, source code recommendation books, or download Source Code addresses
Unity 3D Game Development