欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Unity如何實(shí)現(xiàn)圖片生成灰白圖

小編這次要給大家分享的是Unity如何實(shí)現(xiàn)圖片生成灰白圖,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。

成都創(chuàng)新互聯(lián)公司從2013年創(chuàng)立,先為壽寧等服務(wù)建站,壽寧等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為壽寧企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

效果

原圖

Unity如何實(shí)現(xiàn)圖片生成灰白圖

生成出來的灰白圖

Unity如何實(shí)現(xiàn)圖片生成灰白圖

制作方法

把文章末尾的的TextureUtils.cs腳本放到工程的Assets / Editor目錄中

然后選中項(xiàng)目中的一張圖片,然后點(diǎn)擊菜單Tools / GenGrayTexture

Unity如何實(shí)現(xiàn)圖片生成灰白圖

就會在同級目錄中生成灰白圖片了

Unity如何實(shí)現(xiàn)圖片生成灰白圖

// 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<Texture2D>(SelectionMode.DeepAssets);
  foreach (var t in textures)
  {
   var path = AssetDatabase.GetAssetPath(t);

   // 如果提示圖片不可讀,需要設(shè)置一下isReadable為true, 操作完記得再設(shè)置為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];
    // 顏色值計(jì)算,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();
  }
 }
}

如果要批量修改,可以用Directory.GetFiles接口來獲取特定格式的文件

var files = Directory.GetFiles("D:\\path", "*.*", SearchOption.AllDirectories);
foreach(var f in files)
{
 if(!f.EndsWith(".png") && !f.EndsWith(".jpg")) continue;
 // TODO...
 
}

看完這篇關(guān)于Unity如何實(shí)現(xiàn)圖片生成灰白圖的文章,如果覺得文章內(nèi)容寫得不錯(cuò)的話,可以把它分享出去給更多人看到。

當(dāng)前名稱:Unity如何實(shí)現(xiàn)圖片生成灰白圖
文章源于:http://www.chinadenli.net/article44/pegphe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣網(wǎng)站制作靜態(tài)網(wǎng)站網(wǎng)站策劃自適應(yīng)網(wǎng)站響應(yīng)式網(wǎng)站

廣告

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

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)