Unity相机截图

一:使用相机截图

                 public Vector2                                       popupSize;
                 public int                                                                 screenshotHeight;
                 public Menu2DController menu2DController;
                 public string                                            postText;
                 protected Texture2D _screenshot;
                 protected bool                        _cleanupScreenshot = true ;


1:使用相机正在渲染的屏幕截图
publicstaticTexture2DScreenshot{
                                                 get{
                                                                 List<Camera > cameras = new List <Camera>();
                                                                 cameras.Add (GameObject. FindGameObjectWithTag("MainCamera" ).GetComponent< Camera>());                                                                         
                                                                 foreach (GameObject go in GameObject.FindGameObjectsWithTag ("GUICamera")){
                                                                                 cameras.Add (go.GetComponent< Camera>());
                                                                }

                                                                 cameras.Sort ((x, y) => x.depth .CompareTo( y.depth ));

                                                                 int width = Screen. width;
                                                                 int height = Screen. height;
                                                                 Texture2D tex = new Texture2D( width, height , TextureFormat. RGB24, false );
                                                                 RenderTexture rt = new RenderTexture( width, height , 24);


                                                                 GameObject watermarkGO = GameObject. FindWithTag("Watermark" );
                                                                 if (watermarkGO == null){
                                                                                 Debug.LogError ("No watermark found!");
                                                                } else{
                                                                                 //watermarkGO.SetActive(true);
                                                                }

                                                                 foreach (Camera cam in cameras){
                                                                                 RenderTexture current = cam.targetTexture;
                                                                                 cam.targetTexture = rt;
                                                                                 bool enabled = cam.enabled;
                                                                                 cam.enabled = true;
                                                                                 cam.Render ();
                                                                                 cam.enabled = enabled;
                                                                                 cam.targetTexture = current;
                                                                }

                                                                 if (watermarkGO != null){
                                                                                 //watermarkGO.SetActive(false);
                                                                }
                                                                 RenderTexture.active = rt;
                                                                 // Read render texture contents into the texture
                                                                 tex.ReadPixels ( new Rect(0, 0, width, height ), 0, 0 );
                                                                 tex.Apply ();

                                                                 RenderTexture.active = null;
                                                                 Object.Destroy (rt);
                                                                 return tex ;
                                                }
                                }


2:获取已经几区的图片的Texture
protected IEnumerator ShowCorutine()
                {
                                 _cleanupScreenshot = true ;                               
                                 _screenshot = Screenshot;
                                 // add some delay, because taking a screenshot
                                 // does a 'hiccup'
                                 yield return new WaitForSeconds (0.1f);

                                
                                 menu2DController.HideButtons ();
                                 Popup.Params p                   = Popup.Params .Default;
                                 p.header                                                 = LocalizationManager.GetLocalizedText (HEADER);
                                 p.dimensions                        = popupSize;         
                                 p.image                                  = _screenshot;
                                 p.imageDimensions             = new Vector2 (Screen. width / (float )Screen. height * screenshotHeight, screenshotHeight );
                                 p.onHideEnd = HandlePopupHideEnd;
                                 p.button1text = LocalizationManager. GetLocalizedText (SHARE_IT );
                                 p.button1handler = OnNativeShareClick;
                                 p.onHideStart = menu2DController. ShowButtons;
                                
                                 Popup.Instance .Show (p);
                }
3:将已经截图的texture 图片使用字符串进行传输

public void OnNativeShareClick()
                {
        //Debug.LogError("XXXXX   ShareWebChat");
        if (_screenshot != null)
        {
            byte[] val = _screenshot. EncodeToPNG();
            string bytesString = System. Convert.ToBase64String (val);
            AriesGame.Instance .WeChatShare( bytesString);
        }
        else
        {
            Debug.LogError ("XXXXX  _screenshot = null !!!!!!!!!!!!! ");
        }
        //SPShareUtility.ShareMedia ("Kickerinho", GetDescription (), _screenshot);
                }




ps:Popup.Params  是一个弹出框 可以做一个弹出框的预制件


                                 // constructor with default values
                                
                                 public enum Layout
                                {
                                                 TEXT_ONLY,
                                                 IMAGE_TEXT,
                                                 IMAGE_STATS
                                }

                                 // constructor with default values
                                 public static Params Default
                                {
                                                 get{
                                                                 Params p ;
                                                                 p.content                                               = "";
                                                                 p.formatContent                    = true;
                                                                 p.header                                                 = "";
                                                                 p.showCloseButton             = true;
                                                                 p.position                                              = Vector3.zero ;
                                                                 p.dimensions                        = new Vector2 (400, 200);
                                                                 p.button1text                        = "";
                                                                 p.button2text                        = "";
                                                                 p.button1handler                 = null;
                                                                 p.button2handler                 = null;
                                                                 p.button1sprite                      = "BtnGreen";
                                                                 p.button2sprite                      = "BtnBlue";
                                                                 p.image                                                   = null;
                                                                 p.imageDimensions              = new Vector2 (100, 100);
                                                                 p.onHideEnd                                          = null;
                                                                 p.onHideStart                       = null;
                                                                 p.layout                                  = Layout.TEXT_ONLY ;
                                                                 p.layoutSprite                        = "";
                                                                 p.layoutImageDimensions = new Vector2(22,22);
                                                                 p.stat1text                              = "";
                                                                 p.stat2text                                               = "";
                                                                 p.statsSprite                           = "";
                                                                 p.statsImageDimensions = new Vector2(22,22);

                                                                 return p ;
                                                }
                                }
                }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值