Unity如何实现图片生成灰白图-创新互联
                                            小编这次要给大家分享的是Unity如何实现图片生成灰白图,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

效果
原图

生成出来的灰白图

制作方法
把文章末尾的的TextureUtils.cs脚本放到工程的Assets / Editor目录中
然后选中项目中的一张图片,然后点击菜单Tools / GenGrayTexture

就会在同级目录中生成灰白图片了

// TextureUtils.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class TextureUtils : MonoBehaviour
{
 [MenuItem("Tools/GenGrayTexture")]
 public static void GenGrayTexture()
 {
  // 获取选中的图片
  var textures = Selection.GetFiltered(SelectionMode.DeepAssets);
  foreach (var t in textures)
  {
   var path = AssetDatabase.GetAssetPath(t);
   // 如果提示图片不可读,需要设置一下isReadable为true, 操作完记得再设置为false
   var imp = AssetImporter.GetAtPath(path) as TextureImporter;
   imp.isReadable = true;
   AssetDatabase.ImportAsset(path);
   var newTexture = new Texture2D(t.width, t.height, TextureFormat.RGBA32, false);
   var colors = t.GetPixels();
   var targetColors = newTexture.GetPixels();
   for (int i = 0, len = colors.Length; i < len; ++i)
   {
    var c = colors[i];
    // 颜色值计算,rgb去平均值
    var v = (c.r + c.g + c.b) / 3f;
    targetColors[i] = new Color(v, v, v, c.a);
   }
   newTexture.SetPixels(targetColors);
   string fname = path.Split('.')[0] + "_gray.png";
   File.WriteAllBytes(fname, newTexture.EncodeToPNG());
   imp.isReadable = false;
   AssetDatabase.Refresh();
  }
 }
} 另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站名称:Unity如何实现图片生成灰白图-创新互联
网页URL:http://www.scyingshan.cn/article/joicp.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 