unity使用代碼修改Texture屬性,一鍵產生精靈

來源:互聯網
上載者:User

http://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前面有空格)。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.