1. 程式人生 > >unity圖片外掛精靈

unity圖片外掛精靈

轉自https://www.cnblogs.com/jqg-aliang/p/4873148.html

 

因為公司的UI比較豐富,各種底圖什麼的都非常多。為了不讓圖片資源重複等原因,要求匯入圖片

時需要一張一張的從美工資源那邊拿,一張一張的比對後才能新增到專案中。然後按照慣例,對圖片進行修改,做成精靈。

圖片新增到專案資源預設是這樣的,

要求的屬性設定是這樣的:

如果手動調的話,太麻煩了,點選選擇,還要應用選項,然後我馬上寫了個小工具。用C#寫程式碼真好啊,可惜

我現在只能用Lua寫。

現在把這個小工具分享給大家。

今天心情So Down,不想註釋。程式碼很簡單,就是設定一些屬性的。大家可以嘗試修改達到自己想要的要求。

複製程式碼

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
/// <summary>
/// 這個函式是自動對圖片資源自動處理,設定型別,格式等
/// </summary>
public class TextureEditor  {
    
    static Object targetObj;
    [MenuItem("TextureEdit/Edit")]
    static void EditTexture() {
        targetObj = Selection.activeObject;//這個函式可以得到你選中的物件
        if (targetObj && targetObj is Texture)
        {
            string path = AssetDatabase.GetAssetPath(targetObj);
            TextureImporter texture = AssetImporter.GetAtPath(path) as TextureImporter;
            texture.textureType = TextureImporterType.Sprite;
            texture.spritePixelsPerUnit = 1;
            texture.filterMode = FilterMode.Trilinear;
            texture.mipmapEnabled = false;
            texture.textureFormat = TextureImporterFormat.AutomaticTruecolor;
            AssetDatabase.ImportAsset(path);
            

        }
    }
    
}

複製程式碼

寫完後,可能需要重新開啟專案,然後你就會看到這個東東:

選中圖片然後點選Edit,自己看效果吧!

10月22日更新:自定義編輯器快捷鍵.

首先感謝阿升哥哥。他的部落格在這裡:http://home.cnblogs.com/u/shenggege/

OK,用滑鼠點選選單完成編輯功能還是有點麻煩,自定義一個快捷鍵吧:

[MenuItem("TextureEdit/Edit &C")]
然後你就會在編輯器中看到這個

選中圖片然後按下Alt + C,圖片秒設……

以下內容來自API:

可以使用一下指定字元建立熱鍵:% (Windows上為ctrl, OS X上為cmd), # (shift), & (alt), _ (無修改鍵)。例如建立一個選單熱鍵為shift-alt-g使用GameObject/Do Something #&g。建立一個選單熱鍵g並沒有修改鍵(組合鍵),使用GameObject/Do Something _g。熱鍵文字必須在前面加一個空格字元(GameObject/Do_g不會被解釋為熱鍵,而是GameObject/Do _g這樣,注意_g前面有空格)。