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)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。