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 内置资源工具
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。