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 内置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 内置资源工具
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...