Unity3D里截圖並保存在指定目錄下


private void OnScreenCapture ()
	{
		//yield return new WaitForEndOfFrame();
		try {
			int width = Screen.width;
			int height = Screen.height-reduceHigh;
			
			Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);
			
			tex.ReadPixels (new Rect (0, bottomStarH, width, height), 0, 0, true);
			
			byte[] imagebytes = tex.EncodeToPNG ();
			
			tex.Compress (false);
			tex.Apply();
			mScreenShotImgae = tex;
			
			if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer) 
			{
				path = Application.persistentDataPath + screenShotFileName +".png";
				string origin = path;
				
				string destination = "/mnt/sdcard/moliputao";
				
				if(!Directory.Exists(destination))
				{
					Directory.CreateDirectory(destination);
				}
				
				destination = destination + "/" + screenShotFileName +".png";
				
				if(System.IO.File.Exists(origin))
				{
					System.IO.File.Move(origin,destination);
				}
				path = destination;
				
			}
			else if (Application.platform == RuntimePlatform.WindowsEditor) {  
				path = Application.dataPath;
				path = path.Replace ("/Assets", "/"+screenShotFileName + ".png");
			}
			
			File.WriteAllBytes (path, imagebytes);
			mScreenShotImgae = tex;
			
		} catch (System.Exception e) {
			Debug.Log ("ScreenCaptrueError:" + e);
		}
	}

  只放出核心代碼!以后有時間會補全和詳細講解!

因為我是需要截圖后出現預覽效果,所以並沒有使用協程,如果用協程需要加一句代碼,等待當前那一幀結束!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM