using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class ReplaceUiSprite : MonoBehaviour
{
private const string kStandardSpritePath = "UI/Skin/UISprite.psd";
private const string kBackgroundSpritePath = "UI/Skin/Background.psd";
private const string kInputFieldBackgroundPath = "UI/Skin/InputFieldBackground.psd";
private const string kKnobPath = "UI/Skin/Knob.psd";
private const string kCheckmarkPath = "UI/Skin/Checkmark.psd";
private const string kDropdownArrowPath = "UI/Skin/DropdownArrow.psd";
private const string kMaskPath = "UI/Skin/UIMask.psd";
private static DefaultControls.Resources s_StandardResources;
private static Dictionary<Sprite, Sprite> _dictionary = new Dictionary<Sprite, Sprite>();
private static Sprite GetReplaceSprite(List<Sprite> list, string name)
{
return list.Find(p => p.name == name);
}
[MenuItem("Jayden/ReplaceStandardUiSprite")]
public static void ReplaceStandardUiSprite()
{
// 加載Unity 內(nèi)置UI資源
if (s_StandardResources.standard == null)
{
s_StandardResources.standard = AssetDatabase.GetBuiltinExtraResource<Sprite>(kStandardSpritePath);
s_StandardResources.background = AssetDatabase.GetBuiltinExtraResource<Sprite>(kBackgroundSpritePath);
s_StandardResources.inputField = AssetDatabase.GetBuiltinExtraResource<Sprite>(kInputFieldBackgroundPath);
s_StandardResources.knob = AssetDatabase.GetBuiltinExtraResource<Sprite>(kKnobPath);
s_StandardResources.checkmark = AssetDatabase.GetBuiltinExtraResource<Sprite>(kCheckmarkPath);
s_StandardResources.dropdown = AssetDatabase.GetBuiltinExtraResource<Sprite>(kDropdownArrowPath);
s_StandardResources.mask = AssetDatabase.GetBuiltinExtraResource<Sprite>(kMaskPath);
}
// 加載自定義的替換UI資源
GameObject replaceGo = null;
string customPath = Application.dataPath + "/Resources/a_test.prefab";
if (File.Exists(customPath))
{
replaceGo = PrefabUtility.LoadPrefabContents(customPath);
if (replaceGo == null)
{
return;
}
}
else
{
return;
}
var atlas = replaceGo.GetComponent<UGUIAtlas>();
if (atlas == null)
{
return;
}
_dictionary.Add(s_StandardResources.standard, GetReplaceSprite(atlas.spriteList, "UISprite"));
_dictionary.Add(s_StandardResources.background, GetReplaceSprite(atlas.spriteList, "Background"));
_dictionary.Add(s_StandardResources.inputField, GetReplaceSprite(atlas.spriteList, "InputFieldBackground"));
_dictionary.Add(s_StandardResources.knob, GetReplaceSprite(atlas.spriteList, "Knob"));
_dictionary.Add(s_StandardResources.checkmark, GetReplaceSprite(atlas.spriteList, "Checkmark"));
_dictionary.Add(s_StandardResources.dropdown, GetReplaceSprite(atlas.spriteList, "DropdownArrow"));
_dictionary.Add(s_StandardResources.mask, GetReplaceSprite(atlas.spriteList, "UIMask"));
// 篩選選中的目錄下所有的prefab文件
Object[] objects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
foreach (var o in objects)
{
string path = AssetDatabase.GetAssetPath(o);
if (path.EndsWith(".prefab"))
{
GameObject go = PrefabUtility.LoadPrefabContents(path);
Image[] arr = go.GetComponentsInChildren<Image>(true);
if (arr != null)
{
foreach (var image in arr)
{
if (_dictionary.ContainsKey(image.sprite))
{
image.sprite = _dictionary[image.sprite];
}
}
}
PrefabUtility.SaveAsPrefabAsset(go, path);
}
}
}
}
替換UGUI 內(nèi)置資源工具
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來断盛,“玉大人罗洗,你說我怎么就攤上這事「置停” “怎么了伙菜?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長命迈。 經(jīng)常有香客問我贩绕,道長火的,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任淑倾,我火速辦了婚禮馏鹤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘娇哆。我一直安慰自己湃累,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布碍讨。 她就那樣靜靜地躺著治力,像睡著了一般。 火紅的嫁衣襯著肌膚如雪勃黍。 梳的紋絲不亂的頭發(fā)上宵统,一...
- 文/蒼蘭香墨 我猛地睜開眼疑枯,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蛔六?” 一聲冷哼從身側(cè)響起荆永,我...
- 正文 年R本政府宣布匙赞,位于F島的核電站佛掖,受9級特大地震影響妖碉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜芥被,卻給世界環(huán)境...
- 文/蒙蒙 一欧宜、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧拴魄,春花似錦冗茸、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至职员,卻和暖如春麻蹋,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背焊切。 一陣腳步聲響...