public void SetNode(Transform node) { if (ExportToJson.IsPrefabAsset(node, true)) { string path = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(node); GameObject go = AssetDatabase.LoadAssetAtPath <GameObject>(path); prefabName = go.name; //获取差异数据 Transform _node = go.transform; if (_node.childCount > 0) { differ = new Differ(); for (int i = 0; i < _node.childCount; i++) { if (differ.child == null) { differ.child = new List <Differ>(); } Transform tmp = _node.GetChild(i); Transform _tmp = node.GetChild(i); Differ _differ = Differ.getDiffer(tmp, _tmp, i); if (_differ != null) { differ.child.Add(_differ); } } if (differ.child == null || differ.child.Count == 0) { differ = null; } } return; } nodeTran = node; for (int i = 0; i < nodeTran.childCount; i++) { Transform tmp = nodeTran.GetChild(i); if (child == null) { child = new List <Child>(); } child.Add(ExportToJson.SetChild(tmp)); } }
public void SetNode(Transform node) { if (ExportToJson.IsPrefabAsset(node, true)) { string path = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(node); GameObject go = AssetDatabase.LoadAssetAtPath <GameObject>(path); prefabName = go.name; return; } nodeTran = node; for (int i = 0; i < nodeTran.childCount; i++) { Transform tmp = nodeTran.GetChild(i); if (child == null) { child = new List <Child>(); } child.Add(ExportToJson.SetChild(tmp)); } }
static void ExportPrefabsNameToTs() { string sceneName = ExportToJson.capitalizeFirstSetter(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name); //预制体名字ts文件导出目录 string filepath = @".\..\src\dMyGame\_prefabsName\_" + sceneName + "PrefabNames.ts"; // createFile(filepath); // string prefabsNames = ""; List <string> names = new List <string>(); Object[] objAll = UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects(); GameObject[] gos = (GameObject[])FindObjectsOfType(typeof(GameObject)); string classText = ""; List <string> classList = new List <string>(); Regex classReg = new Regex("(^{ *class *(?<name>[a-zA-Z0-9]*) *(?<log>.*))"); bool ifClass = false; Match classMatch = classReg.Match(""); for (int i = 0; i < objAll.Length; i++) { GameObject obj = objAll[i] as GameObject; string name = obj.name; //判断是不是预制体 if (!ExportToJson.IsPrefabAsset(obj, true)) { //检测是不是分类 if ((new Regex("^{")).IsMatch(name)) { // 获取这个分类名字 classMatch = classReg.Match(name); ifClass = true; } //分类结束 else if ((new Regex("^}")).IsMatch(name)) { ifClass = false; classText += "\n"; classText += " /**\n"; classText += " * " + classMatch.Groups["log"] + "\n"; classText += " */\n"; classText += " public static get Get" + ExportToJson.capitalizeFirstSetter(classMatch.Groups["name"].ToString()) + "Class(): string[] {\n"; classText += " //返回数据\n"; classText += " return [\n"; if (classList.Count > 0) { //添加进分组数据 for (int j = 0; j < classList.Count; j++) { classText += (" '" + classList[j] + "',\n"); } // } classText += " ];\n"; classText += " }\n"; //清空 classList = new List <string>(); } else { prefabsNames += (" //" + name + "\n"); } continue; } //判断是否检测到一个分类 if (ifClass) { classList.Add(name); } // names.Add(name); // string _name = ExportToJson.capitalizeFirstSetter(name.Replace(" ", "")); prefabsNames += (" public static readonly " + _name + ": string = '" + name + "';" + "\n"); } // string _prefabeNameTest = ""; for (int i = 0; i < names.Count; i++) { _prefabeNameTest = _prefabeNameTest + "@" + names[i] + "@"; } // Debug.Log(_prefabeNameTest); //读取本地缓存各个场景预制体列表 string _AllPrefabsNameCacheURL = @"./Assets/Cache/AllPrefabsNameCache.txt"; string _AllPrefabsNameCacheText = ""; try{ string[] _AllPrefabsNameCache = File.ReadAllLines(_AllPrefabsNameCacheURL); for (int i = 0; i < _AllPrefabsNameCache.Length; i++) { _AllPrefabsNameCacheText += _AllPrefabsNameCache[i]; } }catch {} // Debug.Log(_AllPrefabsNameCacheText); Regex _AllPrefabsNameCacheSceneRegex = new Regex("@" + sceneName + ":{.*?}"); string _cacheSceneText = "@" + sceneName + ":{" + _prefabeNameTest + "}"; //替换场景预制体列表 if (_AllPrefabsNameCacheSceneRegex.IsMatch(_AllPrefabsNameCacheText)) { _AllPrefabsNameCacheText = _AllPrefabsNameCacheSceneRegex.Replace(_AllPrefabsNameCacheText, _cacheSceneText); } else { _AllPrefabsNameCacheText += _cacheSceneText; } // Debug.Log(_AllPrefabsNameCacheText); //存储缓存 createFile(_AllPrefabsNameCacheURL); File.WriteAllText(_AllPrefabsNameCacheURL, _AllPrefabsNameCacheText); //根据缓存设置一个全局的预制体名字ts脚本 TextAsset AllPrefabsAsset = (TextAsset)Resources.Load("template/AllPrefabsNames"); string AllPrefabsTxt = AllPrefabsAsset.text; string _AllPrefabsNameSceneText = ""; //读取所有预制体名字 MatchCollection _allPrefabeMatch = (new Regex("@(?<scene>.*?):{(?<prefabs>.*?)}")).Matches(_AllPrefabsNameCacheText); for (int i = 0; i < _allPrefabeMatch.Count; i++) { _AllPrefabsNameSceneText = _AllPrefabsNameSceneText + " " + _allPrefabeMatch[i].Groups["scene"] + ": string = '" + _allPrefabeMatch[i].Groups["prefabs"] + "';\n"; } // AllPrefabsTxt = (new Regex("{{AllPrefab}}")).Replace(AllPrefabsTxt, _AllPrefabsNameSceneText); string _AllPrefabsNameSceneURL = @".\..\src\dMyGame\_prefabsName\_AllPrefabNames.ts"; //写入全部预制体名字 File.WriteAllText(_AllPrefabsNameSceneURL, AllPrefabsTxt); //读取模板 TextAsset textAsset = (TextAsset)Resources.Load("template/PrefabsName"); string template = textAsset.text; //正则替换模板内容 string text = (new Regex("{{PrefabNames}}")).Replace(template, prefabsNames); text = (new Regex("{{sceneName}}")).Replace(text, sceneName); text = (new Regex("{{className}}")).Replace(text, classText); //写入文件 File.WriteAllText(filepath, text); // AssetDatabase.Refresh(); //打印输出 Debug.Log("导出场景 " + sceneName + " 预制体名字TS文件完成,时间:" + System.DateTime.Now); }