public Sprite oldSprite;
public Sprite newSprite;
public string path = "Assets/";
public void FindAndChangePrefab()
{
if (oldSprite == null)
{
Debug.Log("请先设置查找引用的图片");
return;
}
if (newSprite == null)
{
Debug.Log("请先设置替换的图片");
return;
}
// 搜索所有预制件,并检查它们是否引用了该Sprite
int changeCount = 0;
string[] guids = AssetDatabase.FindAssets("t:Prefab", new[] { path});
foreach (string guid in guids)
{
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
Image[] AllImage = prefab.transform.GetComponentsInChildren<Image>(true);
bool isChange = false;
foreach (Image spriteRenderer in AllImage)
{
if (spriteRenderer.sprite == oldSprite)
{
Debug.Log(string.Format("预制件:{0} 里的 {1} 查找到引用",prefab.name,spriteRenderer.transform.name));
spriteRenderer.sprite = newSprite;
isChange = true;
}
}
if (isChange)
{
EditorUtility.SetDirty(prefab);
changeCount++;
}
}
// 输出结果
if (changeCount == 0)
{
Debug.Log("没有预制件在引用此图片");
}
else
{
AssetDatabase.SaveAssets();
Debug.Log("查找并替换完成");
}
}
Unity 编辑器模式下查找引用指定图片的所有预制件,并替换成新的指定图片
最新推荐文章于 2024-06-28 17:00:31 发布