public static void Json2Prefab(string outPath, string dirName) { string[] jsonPaths = Directory.GetFiles(outPath, "*.json", SearchOption.AllDirectories); List <Model> modelList = new List <Model>(); foreach (string jsonPath in jsonPaths) { // if(!PSD2UI.isMD5Change(jsonPath)) // { // continue; // } StreamReader jsonReader = File.OpenText(jsonPath); if (jsonReader != null) { string jsonStr = jsonReader.ReadToEnd(); Model model = JsonUtility.FromJson <Model> (jsonStr); modelList.Add(model); } } if (modelList.Count <= 0) { return; } PSD2UI.curBaseAssetsDir = outPath; for (var i = 0; i < modelList.Count; i++) { var model = modelList[i]; for (var j = model.children.Length - 1; j >= 0; j--) { oneCopyImgToTmpPath(model.children[j], dirName); } } copyImgToTmpPathFromPNGExport(dirName); //打包临时文件的图集 Debug.Log("######开始打图集######"); string srcPath = PSD2UI.texturePackerTempPath + dirName; string tarPath = PSDConst.GUI_PATH + dirName + "/" + dirName + ".png"; AtlasManager.InitAtlasForTextureP(srcPath, tarPath); Debug.Log("######图集打完,开始加载UI######"); string path1 = PSDConst.GetPrefabPathByName("Canvas"); Canvas temp1 = AssetDatabase.LoadAssetAtPath(path1, typeof(Canvas)) as Canvas; // 实例化显示prefab(要另外用个对象保存避免被释放) Canvas canvas = GameObject.Instantiate(temp1) as Canvas; for (var i = 0; i < modelList.Count; i++) { var model = modelList[i]; string path3 = PSDConst.GetPrefabPathByName("GameObject"); GameObject temp = AssetDatabase.LoadAssetAtPath(path3, typeof(GameObject)) as GameObject; GameObject gameobj = GameObject.Instantiate(temp) as GameObject; gameobj.name = model.name + "Prefab"; gameobj.transform.SetParent(canvas.gameObject.transform, false); RectTransform rect = gameobj.GetComponent <RectTransform>(); // 总预设锚点再左上角(父锚点,锚点) Vector2 anchorP = PSDConst.PIVOTPOINT1; rect.anchorMin = anchorP; rect.anchorMax = anchorP; rect.pivot = anchorP; rect.offsetMin = new Vector2(0.0f, 0.0f); rect.offsetMax = new Vector2(0.0f, 0.0f); rect.sizeDelta = new Vector2(model.options.width, model.options.height); for (int j = model.children.Length - 1; j >= 0; j--) { initComponent(model.children[j], gameobj, dirName); } string prefabPath = PSDConst.GUI_PATH + dirName + "/" + model.name + "Prefab.prefab"; Debug.Log("######创建预设:" + prefabPath + "######"); PrefabUtility.CreatePrefab(prefabPath, gameobj, ReplacePrefabOptions.ReplaceNameBased); } SetAssetBundleName(); BuildAB.BuildAllAssetBundles(); AssetDatabase.Refresh(); }