/// <summary> /// 添加新的元素 /// </summary> public void AddNewUIElement(GameObject pGo, string pTypeName) { GameObject tGo = pGo as GameObject; if (tGo == null) { return; } string tName = GetVariableName(pGo, pTypeName); if (string.IsNullOrEmpty(tName)) { return; } GameObject tRootGo = mPrefabGo as GameObject; UIElementData tData = UIElementData.Create(tRootGo, tName, tGo.GetParenRelativePath(tRootGo, false), pTypeName); UIElementData tAddedData = null; if (mUIElementDic.TryGetValue(tName, out tAddedData)) { if (tData.mPath != tAddedData.mPath) { string tErrorInfo = string.Format(@"添加失败: 存在变量名相同, 但路径不同的其他对象, 冲突的另一个对象路径 = {0}", tAddedData.mPath); Debug.LogError(tErrorInfo); UIScriptCreatorEW.mInstance.ShowNotification(new GUIContent("添加失败")); return; } } mUIElementDic.Add(tData.mVarName, tData); }
/// <summary> /// 解析View脚本中的元素 /// </summary> private void ParseViewScriptData() { if (File.Exists(mViewPath) == false) { return; } string tViewContent = File.ReadAllText(mViewPath); string tStartTag = @"{//CheckNullElementStartTag"; string tEndTag = @"};//CheckNullElementEndTag"; int tStartIndex = tViewContent.IndexOf(tStartTag) + tStartTag.Length; int tCount = tViewContent.IndexOf(tEndTag) - tStartIndex; string tContent = tViewContent.Substring(tStartIndex, tCount); string[] tLineSplitStrArr = tContent.Split('\n'); for (int i = 0; i < tLineSplitStrArr.Length; ++i) { string tLineStr = tLineSplitStrArr[i]; if (tLineStr.Contains(',') == false) { continue; } string[] tDotSplitStrArr = tLineStr.Split(','); string tPattern = "tElementDic.Add\\(new KeyValuePair<string, string>\\(\"([\\s\\S]+)\",\"([\\s\\S]+)\"\\)\\);"; Match tMatch = Regex.Match(tLineStr, tPattern); string tPathStr = tMatch.Groups[1].Value; string tTypeName = tMatch.Groups[2].Value; int tGoNameStartIndex = tPathStr.LastIndexOf("/") + 1; string tGoName = tPathStr.Substring(tGoNameStartIndex); if (string.IsNullOrEmpty(tGoName)) { tGoName = mPrefabGo.name; } string tVarName = GetVariableNameByStr(tGoName, tTypeName); UIElementData tElement = UIElementData.Create(mPrefabGo, tVarName, tPathStr, tTypeName); tElement.RefreshState(); mUIElementDic.Add(tElement.mVarName, tElement); } }