Unity如何在任意區(qū)域截屏創(chuàng)建Sprite

這篇文章主要介紹Unity如何在任意區(qū)域截屏創(chuàng)建Sprite,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

公司專(zhuān)注于為企業(yè)提供成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、商城網(wǎng)站制作,小程序設(shè)計(jì),軟件定制網(wǎng)站開(kāi)發(fā)等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。憑借多年豐富的經(jīng)驗(yàn),我們會(huì)仔細(xì)了解各客戶(hù)的需求而做出多方面的分析、設(shè)計(jì)、整合,為客戶(hù)設(shè)計(jì)出具風(fēng)格及創(chuàng)意性的商業(yè)解決方案,創(chuàng)新互聯(lián)更提供一系列網(wǎng)站制作和網(wǎng)站推廣的服務(wù)。

Unity截取全屏靜幀的方法較為簡(jiǎn)單這里不作討論,指定區(qū)域截圖用到的最主要的方法就是讀取屏幕像素:

//        // 摘要:        //     Read pixels from screen into the saved texture data.        //        // 參數(shù):        //   source:        //     Rectangular region of the view to read from. Pixels are read from current render        //     target.        //        //   destX:        //     Horizontal pixel position in the texture to place the pixels that are read.        //        //   destY:        //     Vertical pixel position in the texture to place the pixels that are read.        //        //   recalculateMipMaps:        //     Should the texture's mipmaps be recalculated after reading?        public void ReadPixels(Rect source, int destX, int destY, [DefaultValue("true")] bool recalculateMipMaps);        [ExcludeFromDocs]        public void ReadPixels(Rect source, int destX, int destY);

為了方便調(diào)用,寫(xiě)一個(gè)擴(kuò)展協(xié)程如下:

public static IEnumerator CutSpriteFromScreen(this RectTransform boxMin, RectTransform boxMax, UnityAction<Sprite> complete)    {        var sp = new Vector2(boxMin.position.x, boxMin.position.y);        var temp = new Vector2(boxMax.position.x, boxMax.position.y);        Vector2Int size = new Vector2Int((int)(temp.x - sp.x), (int)(temp.y - sp.y));
       //判斷截圖框是否超出屏幕邊界        if (sp.x < 0 || sp.y < 0 || sp.x + size.x > Screen.width || sp.y + size.y > Screen.height)            yield break;
       //等待當(dāng)前幀渲染結(jié)束,此為必須項(xiàng)        yield return new WaitForEndOfFrame();
       Texture2D texture = new Texture2D(size.x, size.y, TextureFormat.RGB24, false);
       texture.ReadPixels(new Rect(sp.x, sp.y, size.x, size.y), 0, 0, false);        texture.Apply();
       complete(Sprite.Create(texture, new Rect(Vector2Int.zero, size), Vector2.one * .5f));    }

調(diào)用如下:

StartCoroutine(BoxMin.CutSpriteFromScreen(BoxMax, (x) => GameData.Instance.PlayerData.Bag.FragCutIcon = x));

效果展示:

Unity如何在任意區(qū)域截屏創(chuàng)建Sprite

Unity如何在任意區(qū)域截屏創(chuàng)建Sprite

可以直接將拼好的芯片圖截取后保存起來(lái)方便在其他界面展示安裝效果,省去了每一界面都劃格子重新讀取數(shù)據(jù)計(jì)算一遍;

因?yàn)槭聦?shí)上只有在設(shè)置芯片的頁(yè)面才需要單獨(dú)對(duì)每塊芯片進(jìn)行細(xì)致操作,其他位置可以簡(jiǎn)化為展示一張縮略圖;

當(dāng)芯片的安裝發(fā)生變化時(shí),同步更新該縮略圖即可。

以上是“Unity如何在任意區(qū)域截屏創(chuàng)建Sprite”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當(dāng)前題目:Unity如何在任意區(qū)域截屏創(chuàng)建Sprite
網(wǎng)頁(yè)URL:http://muchs.cn/article0/jopiio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、品牌網(wǎng)站制作、網(wǎng)站設(shè)計(jì)公司、用戶(hù)體驗(yàn)、網(wǎng)站制作做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)