Unity 带 Alpha 图片手机包失真

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://linsh-tech.blog.csdn.net/article/details/79264124

iOS 图集压缩失真

在 Android 平台下 UI 图片一切正常,但打包到 iOS 上之后,带透明度的区域会出现明显的失真,排查之后才发现,对于设置了 Package Tag 参数,即打包到图集中的图片,使用 Unity 默认的压缩设置的话,会使用 PVRTC 4 bits 进行压缩,此时:

  • 不打图集的散图,带透明度,不会有明显失真;
  • 打图集的图片,带透明度,透明区域失真会比较明显:

1. 最终策略:

对于比较小的图片,需要打图集的,可以考虑不进行压缩,设置为 RGBA 16 的真彩色格式;
对于不打图集的散图,使用 Unity 默认的压缩的压缩方式进行打包,即 PVRTC 4 bits

2. 修改操作:

Project 窗口中选中图片,在 Inspector 窗口中修改设置如下:

由于 Unity 分不同版本, Free 版本默认是不进行压缩的 RGBA 32 格式,而 Pro 版本默认是 Compressed 的格式,所以 Pro 必须勾选 Override for iOS 设置才能生效,直接修改 .meta 文件可能不生效。

修改后的效果:


图片黑边:

.png 图片导入 Unity 并将其 Texture Type 改为 Sprite(2D and UI) ,然后在 UI 中使用一个 Image 组件去展示此图片时,出现黑边,例如:

解决办法其实不难,直接将图片的 Filter Mode 属性从默认的 Bilinear 修改为 Points(no filter) 即可,修改后效果:

展开阅读全文

TCP 传输图片失真

06-07

正在做一个项目,目前卡住了,主要是传输图片,总是失真。不知道大家有没有遇到过类似问题。rn客户端(linux系统)传输图片到服务器(windows操作系统)。在服务器上建立一个MFC程序,显示接收到的图片。如果单步调试客户端的程序,服务器可以接收到图片,并且图片是完整的,不失真的。但是直接运行客户端的程序,服务器接收到的图片却是失真的,每次执行各种失真情况。rn同时,我在vc下写了一个WIN32控制台应用程序的服务器,接收到的图片是完整的,但是在MFC下图片就不行了,纠结ING......rnrn客户端程序(linux)rn[code=C/C++][/code]rn#include rn#include rn#include rn#include rn#include rn#include rn#include rn#include rn#includernrn#define PIC_LEN 800rn#define PORTNUM 6000rn#define FALSE 0rn#define TRUE 1rntypedef int BOOL;rnrnenumrnrn //client namern USER_CLIENT1 = 1,rn USER_CLIENT2,rn USER_CLIENT3,rn USER_CLIENT4,rn USER_CLIENT5,rn USER_CLIENT6,rnrn //client message typern USER_DATA,rn USER_PICTURE,rn USER_REGISTER,rn;rnrnstruct SendMsgrnrn int cliNum;rn int transType;rn int tempVa;rn int humVal;rn int somkVal;rn char picData[PIC_LEN];rn int picDataLen;rn BOOL picEnd;rn;rnrnrnvoid error(const char *msg)rnrn perror(msg);rn exit(0);rnrnrnrnint main(int argc, char *argv[])rnrn int sockfd;rn sockfd = socket(AF_INET, SOCK_STREAM, 0);rn if (sockfd < 0)rn error("ERROR opening socket");rnrn int len;rn const char *cp = "10.21.11.146";rnrn struct sockaddr_in serv_addr;rn bzero((char *) &serv_addr, sizeof(serv_addr));rn serv_addr.sin_family = AF_INET;rn serv_addr.sin_port = htons(PORTNUM);rn if ( 0 == inet_aton(cp, &serv_addr.sin_addr))rn error("ERROR not valid address!");rnrnrn FILE *fq = NULL;rn SendMsg CliMsg;rn CliMsg.cliNum = USER_CLIENT5;rn CliMsg.transType = USER_REGISTER;rn CliMsg.picEnd = FALSE;rnrn if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)rn error("ERROR connecting");rnrn if ( NULL == (fq = fopen("/home/yuxiangyang/xiaoyaxuan.jpg", "rb")) ) rn error("ERROR:file open error!");rnrn write(sockfd,(char*)&CliMsg,sizeof(SendMsg));rnrn bzero(CliMsg.picData,PIC_LEN);rnrn CliMsg.transType = USER_PICTURE;rnrn while(!feof(fq))rn rn len = fread(CliMsg.picData, 1, PIC_LEN, fq);rn CliMsg.picDataLen = len;rn write(sockfd, (char*)&CliMsg, sizeof(SendMsg));rn printf("send data ing ing ......\n");rn usleep(6000);rn rnrn CliMsg.picEnd = TRUE;rnrn write(sockfd,(char*)&CliMsg,sizeof(SendMsg));rn printf("transmit data over!\n");rnrn while(1)rn rn sleep(4000);rn rnrn close(sockfd);rn fclose(fq);rn return 0;rnrnrn服务器(win32控制台应用程序,图片接收是图片的)rn[code=C/C++][/code]rn#include "stdafx.h"rn#include rn#include rnrnenumrnrn USER_CLIENT1 = 1,rn USER_CLIENT2,rn USER_CLIENT3,rn USER_CLIENT4,rn USER_CLIENT5,rn USER_CLIENT6,rnrn USER_DATA,rn USER_PICTURE,rn USER_REGISTER,rn;rnstruct recvMsgrnrn int cliNum;rn int transType;rn int tempVa;rn int humVal;rn int somkVal;rn char picData[800];rn int picDataLen;rn BOOL picEnd;rn;rnint _tmain(int argc, _TCHAR* argv[])rnrn WORD wVersionRequested;rn WSADATA wsaData;rn int err;rn rn wVersionRequested = MAKEWORD( 2, 2 );rn rn err = WSAStartup( wVersionRequested, &wsaData );rn if ( err != 0 ) rn rn return 1;rn rn rn rn if ( LOBYTE( wsaData.wVersion ) != 2 ||rn HIBYTE( wsaData.wVersion ) != 2 )rn rn WSACleanup( );rn return 1; rn rnrn SOCKET listfd = socket(AF_INET, SOCK_STREAM, 0);rn if( INVALID_SOCKET == listfd )rn rn std::cout<<"sock initial error!"< 论坛

显示JPG图片图片失真严重

01-31

直接上代码,使用Load将JPG转成BMP句柄后,缩放到控件上,但是感觉图片颜色失真严重。rn[code=c]//读取jpg格式的图片rnBOOL CPicStatic::Load(LPCTSTR lpszPathName)rnrn BOOL bSuccess=FALSE;rn CFile file;rn //打开文件rn if(!file.Open(lpszPathName,CFile::modeRead))rn rn TRACE(_T("Failed to open file %s,Error %x"),lpszPathName,::GetLastError());rn return FALSE;rn rn //获取文件大小rn DWORD dwFileSize=file.GetLength();rn //以文件大小为基础分配内存rn LPVOID pvData=NULL;rn HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);rn if(hGlobal==NULL)rn rn TRACE(_T("Failed to allocate memory for file %s,Error %x"),lpszPathName,::GetLastError());rn return FALSE;rn rn pvData=GlobalLock(hGlobal);rn if(pvData==NULL)rn rn TRACE(_T("Failed to lock memory"));rn GlobalFree(hGlobal);rn return FALSE;rn rn //读取文件,存储到全局内存rn if(file.Read(pvData,dwFileSize)!=dwFileSize)rn rn TRACE(_T("Failed to read image data from %s,Error:%x"),lpszPathName,::GetLastError());rn GlobalUnlock(hGlobal);rn GlobalFree(hGlobal);rn return FALSE;rn rn //整理内存,关闭文件句柄rn GlobalUnlock(hGlobal);rn //在全局内存中创建IStream*rn LPSTREAM pStream=NULL;rn if(FAILED(CreateStreamOnHGlobal(hGlobal,TRUE,&pStream)))rn rn TRACE(_T("Failed to create IStream interface from %s,Error %x"),lpszPathName,::GetLastError());rn GlobalFree(hGlobal);rn return FALSE;rn rn //从图片文件上创建IPicturern if (SUCCEEDED(::OleLoadPicture(pStream,dwFileSize,FALSE,IID_IPicture,(LPVOID*)&pPic)))rn rn short nType=PICTYPE_UNINITIALIZED;rn if(SUCCEEDED(pPic->get_Type(&nType))&&(nType==PICTYPE_BITMAP))rn rn OLE_HANDLE hBitmap;rn OLE_HANDLE hPalette;rn if(SUCCEEDED(pPic->get_Handle(&hBitmap))&&SUCCEEDED(pPic->get_hPal(&hPalette)))rn rn m_Bitmap.Attach((HWND)hBitmap);rn m_Palette.Attach((HPALETTE)hPalette);rn bSuccess=TRUE;rn rn rn rn file.Close();rn pStream->Release();rn GlobalFree(hGlobal);rn return bSuccess;rnrnrnvoid CPicStatic::ZoomPic()rnrn BITMAP bm; //定义一个位图结构rn m_Bitmap.GetBitmap(&bm);rn CDC *pDC=GetDC();rn CDC dcMem;rn dcMem.CreateCompatibleDC(pDC); //创建一个兼容的DCrn CBitmap* pOldBitmap=(CBitmap*)dcMem.SelectObject(m_Bitmap); //将位图选入设备环境类rn CRect lRect;rn this->GetClientRect(&lRect);rn lRect.NormalizeRect();rn SetStretchBltMode(this->GetDC()->GetSafeHdc(), COLORONCOLOR); rn this->GetDC()->StretchBlt(lRect.left ,lRect.top ,lRect.Width(),lRect.Height(), rn &dcMem,0 ,0,bm.bmWidth,bm.bmHeight,SRCCOPY);//显示位图rn dcMem.SelectObject(&pOldBitmap); //将原有的句柄选入设备环境rn ReleaseDC(pDC);rn dcMem.DeleteDC();rn Unload();rn[/code] 论坛

没有更多推荐了,返回首页