ImportMaterial.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using LitJson;
  6. using System.IO;
  7. public class ImportMaterial : MonoBehaviour
  8. {
  9. static int id = 0;
  10. static int tempId = 0;
  11. static string notImportMat;
  12. static string savePath = "Assets/Bundles/Materials/";
  13. static string loadPath = "Assets/GameResources/d3Assets/cloth/texture/";
  14. static string json;
  15. [MenuItem("Tools/导入材质")]
  16. public static void Import()
  17. {
  18. json = File.ReadAllText("Assets/GameResources/d3Assets/cloth/DressItemConfig.txt");
  19. Data data = JsonMapper.ToObject<Data>(json);
  20. for (int i = 0; i < data.list.Count; i++)
  21. {
  22. if (data.list[i].dressType != 1)
  23. {
  24. continue;
  25. }
  26. if (data.list[i]._id / 10000 == 8)
  27. {
  28. id = data.list[i]._id;
  29. }
  30. else if (data.list[i]._id / 100000 == 1)
  31. {
  32. if (id != data.list[i]._id / 100 * 100 + 1)
  33. {
  34. id = data.list[i]._id / 100 * 100 + 1;
  35. }
  36. }
  37. else if (data.list[i]._id / 10000 == 31)
  38. {
  39. id = data.list[i]._id/10*10+1;
  40. }
  41. else
  42. {
  43. continue;
  44. }
  45. if (id == 0) continue;
  46. if (!File.Exists("Assets/GameResources/d3Assets/cloth/texture/" + id + "_" + data.list[i]._id + "_BaseMap.jpg"))
  47. {
  48. string fileName = id + "_" + data.list[i]._id;
  49. notImportMat += fileName + "_BaseMap\n" + fileName + "_MaskMap\n" + fileName + "_Normal\n";
  50. continue;
  51. }
  52. Texture2D baseMap = AssetDatabase.LoadAssetAtPath(loadPath + id + "_" + data.list[i]._id + "_BaseMap.jpg", typeof(Texture2D)) as Texture2D;
  53. Texture2D maskMap = AssetDatabase.LoadAssetAtPath(loadPath + id + "_" + data.list[i]._id + "_MaskMap.png", typeof(Texture2D)) as Texture2D;
  54. Texture2D normalMap = AssetDatabase.LoadAssetAtPath(loadPath + id + "_" + data.list[i]._id + "_Normal.png", typeof(Texture2D)) as Texture2D;
  55. Material material = new Material(Shader.Find("Universal Render Pipeline/Lit"));
  56. material.SetTexture("_BaseMap", baseMap);
  57. material.SetTexture("_DetailMask", maskMap);
  58. material.SetTexture("_DetailNormalMap", normalMap);
  59. AssetDatabase.CreateAsset(material, savePath + data.list[i]._id +"_mat"+ ".mat");
  60. }
  61. WriteTextFile("Assets/GameResources/d3Assets/cloth/NotImportMat.txt", notImportMat);
  62. // 刷新资源数据库
  63. AssetDatabase.Refresh();
  64. }
  65. //public static void LoadTexture(string path,out Texture2D texture)
  66. //{
  67. // Texture2D texture = new Texture2D(100,100);
  68. // byte[] imageData = File.ReadAllBytes(path);
  69. // texture.LoadImage(imageData);
  70. // Debug.LogWarning(texture.LoadImage(imageData));
  71. // //Debug.Log(texture.ToString());
  72. //}
  73. public static void WriteTextFile(string path, string text)
  74. {
  75. StreamWriter writer = new StreamWriter(path, false);
  76. writer.Write(text);
  77. writer.Close();
  78. Debug.Log("Text file written and saved at: " + path);
  79. }
  80. [MenuItem("Tools/丢失")]
  81. static void SaveName()
  82. {
  83. json = File.ReadAllText("Assets/GameResources/d3Assets/cloth/DressItemConfig.txt");
  84. Data data = JsonMapper.ToObject<Data>(json);
  85. for (int i = 0; i < data.list.Count; i++)
  86. {
  87. //if (data.list[i].dressType != 1)
  88. //{
  89. // continue;
  90. //}
  91. if (data.list[i]._id / 10000 == 8)
  92. {
  93. id = data.list[i]._id;
  94. }
  95. else if (data.list[i]._id / 100000 == 1)
  96. {
  97. if (id != data.list[i]._id / 100 * 100 + 1)
  98. {
  99. id = data.list[i]._id / 100 * 100 + 1;
  100. }
  101. }
  102. else if(data.list[i]._id / 10000 == 4)
  103. {
  104. id = data.list[i]._id;
  105. }
  106. else if (data.list[i].dressType == 3)
  107. {
  108. id = data.list[i]._id;
  109. }
  110. else if(data.list[i]._id / 10000 == 2)
  111. {
  112. continue;
  113. }
  114. else
  115. {
  116. id = data.list[i]._id;
  117. }
  118. switch (data.list[i].dressType)
  119. {
  120. case 1:
  121. if (!File.Exists("Assets/GameResources/d3Assets/cloth/texture/" + id + "_" + data.list[i]._id + "_BaseMap.jpg"))
  122. {
  123. notImportMat += $"{id}\n";
  124. }
  125. break;
  126. case 2:
  127. break;
  128. case 3:
  129. if (!File.Exists("Assets/GameResources/d3Assets/cloth/texture/" + id + ".jpg"))
  130. {
  131. notImportMat += $"{id}\n";
  132. }
  133. break;
  134. case 4:
  135. if (!File.Exists("Assets/GameResources/d3Assets/cloth/texture/" + id +"L.jpg"))
  136. {
  137. notImportMat += $"{id}\n";
  138. }
  139. break;
  140. case 5:
  141. if (!File.Exists("Assets/GameResources/d3Assets/cloth/texture/" + id + ".jpg"))
  142. {
  143. notImportMat += $"{id}\n";
  144. }
  145. break;
  146. }
  147. }
  148. WriteTextFile("Assets/GameResources/d3Assets/cloth/NotImportMat.txt", notImportMat);
  149. // 刷新资源数据库
  150. AssetDatabase.Refresh();
  151. }
  152. }
  153. public class Data
  154. {
  155. public List<DressItem> list;
  156. }
  157. public class DressItem
  158. {
  159. public int _id { get; set; }
  160. public int dressType { get; set; }
  161. }