Unity相机截图

来源:互联网 发布:山西网络电视台直播 编辑:程序博客网 时间:2024/05/18 14:24
一:使用相机截图

                publicVector2                                     popupSize;
                publicint                                                               screenshotHeight;
                publicMenu2DControllermenu2DController;
                publicstring                                          postText;
                protectedTexture2D_screenshot;
                protectedbool                      _cleanupScreenshot=true;


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

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

                                                                intwidth=Screen.width;
                                                                intheight=Screen.height;
                                                                Texture2Dtex=newTexture2D(width,height,TextureFormat.RGB24,false);
                                                                RenderTexturert=newRenderTexture(width,height, 24);


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

                                                                foreach(Cameracamincameras){
                                                                                RenderTexturecurrent=cam.targetTexture;
                                                                                cam.targetTexture=rt;
                                                                                boolenabled=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(newRect(0, 0,width,height), 0, 0 );
                                                                tex.Apply();

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


2:获取已经几区的图片的Texture
protectedIEnumeratorShowCorutine()
                {
                                _cleanupScreenshot=true;                               
                                _screenshot=Screenshot;
                                // add some delay, because taking a screenshot
                                // does a 'hiccup'
                                yieldreturnnewWaitForSeconds(0.1f);

                                
                                menu2DController.HideButtons();
                                Popup.Paramsp                  =Popup.Params.Default;
                                p.header                                                =LocalizationManager.GetLocalizedText(HEADER);
                                p.dimensions                       =popupSize;         
                                p.image                                 =_screenshot;
                                p.imageDimensions            =newVector2(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 图片使用字符串进行传输

publicvoidOnNativeShareClick()
                {
       //Debug.LogError("XXXXX   ShareWebChat");
       if(_screenshot!=null)
        {
           byte[]val=_screenshot.EncodeToPNG();
           stringbytesString=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
                                
                                publicenumLayout
                                {
                                                TEXT_ONLY,
                                                IMAGE_TEXT,
                                                IMAGE_STATS
                                }

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

                                                                returnp;
                                                }
                                }
                }
1 0
原创粉丝点击