UGUIGameObjectTool.cs 4.2 KB

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