zoukankan      html  css  js  c++  java
  • 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);
    		}
    	}
    

      只放出核心代码!以后有时间会补全和详细讲解!

    因为我是需要截图后出现预览效果,所以并没有使用协程,如果用协程需要加一句代码,等待当前那一帧结束!

  • 相关阅读:
    RDS 工作笔记
    网站测试需要提供的参数和结果分析
    php 安全编程
    留住青春的格子
    保持工作精力旺盛的方法
    百万格子的标签认领可以提高你在alexa的排名的格子
    老电影,似水流年的记忆
    五行 八字 计算
    iis6.0 的 性能比较
    各种情绪和调节方法
  • 原文地址:https://www.cnblogs.com/vital/p/3564206.html
Copyright © 2011-2022 走看看