http://www.tasharen.com/forum/index.php?topic=4018.msg19784#msg19784

I have tons of large sprites, I need to reduce the build size. The ideal is to have the color image in a jpg and the alpha information in a separate 8-bit png.

I am aware, this is not how Unity works (you import the texture into a format, Unity compresses the asset package at build time etc.) Still, I found out I can load jpg and png trough Texture2D.LoadImage, but I have no idea how to put them together. I guess a shader could do that, but I have no clue how to set them up in Unity and even less how to use them in code to call something like Finale = Merge(jpg, png).

void Start () { //load the prepared image data TextAsset colortxt; TextAsset alphatxt; Texture2D colorjpg = new Texture2D(4, 4); Texture2D alphapng = new Texture2D(4, 4); colortxt = Resources.Load("1024cloak01") as TextAsset; alphatxt = Resources.Load ("1024cloak01A") as TextAsset; colorjpg.LoadImage(colortxt.bytes); alphapng.LoadImage(alphatxt.bytes); //load the prepared Shader asset Shader rgbplusa; rgbplusa = Shader.Find("RGBplusA"); //use NGUI to display the image pixel-perfect, make use of the camera setup created with NGUI > Open the UI Wizard UITexture perfect = NGUITools.AddWidget<UITexture>(GameObject.Find ("Panel")); perfect.material =  new Material (rgbplusa); perfect.material.SetTexture("_MainTex", colorjpg); perfect.material.SetTexture("_Mask", alphapng); perfect.MakePixelPerfect(); }

so I figured out a shader that does what I need, see below. If there are some errors or unnecessary stuff, please let me know. It takes one RGB 24 bit Unity texture and one Alpha 8 Unity Texture (with Alpha from Greyscale ON). I tested it in the editor with a simple scene. I created a material, changed the shader to the custom one, added the textures. Then I added the material onto a plane and it works.

I also figured out how to avoid Unity importing my .jpg and .png as textures once I add them in the Resource folder, as I want them added to the build in their compressed form. I change the extension to .bytes and then use Resource.Load("name") as TextAsset and then create a texture and add the image data from the TextAsset.bytes with Texture2D.LoadImage.

So I have a shader and the two textures loaded from the image files in code. I can create a plane, but I am not sure how to scale and position it so that the image is displayed pixel perfect. Is there an easy way how to do it? Is there a way how to use the shader without a plane/mesh?

  1. Shader "RGBplusA" {
  2. Properties {
  3. _MainTex ("Main Texture", 2D) = "white" {}
  4. _Mask ("Mask Texture", 2D) = "white" {}
  5. }
  6. SubShader {
  7. Blend SrcAlpha OneMinusSrcAlpha
  8. Pass
  9. {
  10. SetTexture [_Mask] {combine texture}
  11. SetTexture [_MainTex] {combine texture, previous}
  12. }
  13. }
  14. }

最新文章

  1. Android Handler机制
  2. 在laravel下關於blade模板的嘗試
  3. Request.UrlReferrer
  4. web测试
  5. jquery的隐藏与显示
  6. cxf的soap风格+spirng4+maven 客户端
  7. 从今天起,记录CEF使用开发心得经验
  8. 针对安卓java入门:方法的使用
  9. 史上最全的 UIWebview 的 JS 与 OC 交互
  10. STM32 驱动12864液晶显示汉字、图片、画点、横线、竖线、斜线
  11. 清理SQL多余登录信息
  12. Linux学习笔记25——命名管道(FIFO)
  13. 领域模型(Domain Model)
  14. 二、Tomcat配置以及IDEA运行第一个Jsp项目——JavaWeb点滴
  15. 关于最小生成树(并查集)prime和kruskal
  16. 质心坐标(barycentric coordinates)及其应用
  17. Mysql权限操作、用户管理、密码操作
  18. 关于ashrpt中行源的CPU + Wait for CPU事件深入解读
  19. 基于DobboX的SOA服务集群搭建
  20. Redis的优势和特点

热门文章

  1. 二叉树的遍历(递归,迭代,Morris遍历)
  2. [Android] 【视频】黑马安卓62、66期等教程+源码
  3. matlab矩阵合并及相关运算
  4. 解决VC几个编译问题的方法——好用
  5. 如何下载struts 2及其各个包的作用
  6. double相加(減)结果会有些误差
  7. 【WEB前端经验之谈】没有速成,只有不断积累。
  8. .net,微软,薪资及其他
  9. linux下定时任务的使用
  10. JavaScript事件---事件对象