UGUIGameObjectTool.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using UnityEngine;
  4. using UnityEngine.U2D;
  5. using UnityEngine.UI;
  6. using UnityEngine.UI.PackgTool;
  7. namespace UnityEditor.UI
  8. {
  9. public class UGUIGameObjectTool
  10. {
  11. private static List<SpriteAtlas> allSpriteAtlasList;
  12. public static void ReviewUI(List<SpriteAtlas> allSpriteAtlasList, SpriteAtlas spriteAtlas, PackInfo packInfo)
  13. {
  14. UGUIGameObjectTool.allSpriteAtlasList = allSpriteAtlasList;
  15. string[] allFinle =
  16. System.IO.Directory.GetFiles(Application.dataPath + "/Res/UI/", "*", SearchOption.AllDirectories);
  17. // string[] uiallObj = AssetDatabase.FindAssets("", new string[] {"Assets/Res/UI"});
  18. for (int i = 0; i < allFinle.Length; i++)
  19. {
  20. string path = allFinle[i].Replace(Application.dataPath, "Assets");
  21. if (path.Contains(".mate"))
  22. {
  23. continue;
  24. }
  25. GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(path);
  26. if (go != null)
  27. {
  28. bool isAlter = ReviewGameObject(go, spriteAtlas, packInfo);
  29. if (isAlter)
  30. {
  31. PrefabUtility.SavePrefabAsset(go);
  32. }
  33. }
  34. }
  35. AssetDatabase.SaveAssets();
  36. }
  37. private static SpriteAtlas GetSpriteNamePack(string spriteName)
  38. {
  39. if (allSpriteAtlasList == null || allSpriteAtlasList.Count <= 0)
  40. {
  41. return null;
  42. }
  43. for (int i = 0; i < allSpriteAtlasList.Count; i++)
  44. {
  45. if (allSpriteAtlasList[i].GetSprite(spriteName) != null)
  46. {
  47. return allSpriteAtlasList[i];
  48. }
  49. }
  50. return null;
  51. }
  52. private static bool ReviewGameObject(GameObject gameObject, SpriteAtlas spriteAtlas, PackInfo packInfo)
  53. {
  54. bool isAlter = false;
  55. MyImage[] allImage = gameObject.transform.GetComponentsInChildren<MyImage>(true);
  56. if (allImage == null)
  57. {
  58. return false;
  59. }
  60. for (int i = 0; i < allImage.Length; i++)
  61. {
  62. MyImage image = allImage[i];
  63. bool isoK = false;
  64. if (image.packInfo != null && image.CurrSpriteAtlas != null)
  65. {
  66. isoK = image.CurrSpriteAtlas.GetSprite(image.icon_name) != null;
  67. if (!isoK) //没在在图集里面找到图片了,需要去其他地方查找(删除图集)
  68. {
  69. SpriteAtlas newAtlas = GetSpriteNamePack(image.icon_name);
  70. if (newAtlas != null)
  71. {
  72. image.packInfo = UGUICacheInfo.uguiPackDB.GetPackgInfo(newAtlas.name);
  73. image.CurrSpriteAtlas = newAtlas;
  74. image.icon_name = image.icon_name;
  75. image.ReashUI();
  76. isAlter = true;
  77. }
  78. }
  79. else
  80. {
  81. continue;
  82. }
  83. }
  84. if (image.CurrSpriteAtlas == null &&
  85. image.sprite != null|| !string.IsNullOrEmpty( image.icon_name)) //没有图集 但是由功能管理器添加的UI (添加图集)
  86. {
  87. string name = image.sprite!=null? image.sprite!.name: image.icon_name;
  88. SpriteAtlas newAtlas = GetSpriteNamePack(name);
  89. // Sprite sprite = spriteAtlas.GetSprite(name);
  90. if (newAtlas != null)
  91. {
  92. image.packInfo = UGUICacheInfo.uguiPackDB.GetPackgInfo(newAtlas.name);
  93. image.CurrSpriteAtlas = newAtlas;
  94. image.icon_name = image.icon_name;
  95. image.ReashUI();
  96. isAlter = true;
  97. }
  98. }
  99. }
  100. return isAlter;
  101. }
  102. }
  103. }