/// <summary> /// 修复路径发生错误的数据, 名字改了, 父节点改了的情况 /// </summary> public void RepairPathErrorUIElementData() { List <string> tOldNameList = new List <string>(); List <UIElementData> tNewNameUIElementDataList = new List <UIElementData>(); //更新数据 foreach (var tKv in mUIElementDic) { UIElementData tUiElementData = tKv.Value; if (tUiElementData.mState != UIElementData.eState.Go_Type_PathError) { continue; } string tOldName = tUiElementData.mVarName; tUiElementData.UpdateDataByGo(); if (tOldName == tUiElementData.mVarName) { continue; } tOldNameList.Add(tOldName); tNewNameUIElementDataList.Add(tUiElementData); } //处理用户 修改了prefab 节点名字的情况 for (int i = 0; i < tNewNameUIElementDataList.Count; ++i) { string tOldName = tOldNameList[i]; UIElementData tNewNameUIElementData = tNewNameUIElementDataList[i]; mUIElementDic.Remove(tOldName); UIElementData tSameNameUIElement = null; if (mUIElementDic.TryGetValue(tNewNameUIElementData.mVarName, out tSameNameUIElement) == false) { //没有同名的情况 mUIElementDic.Add(tNewNameUIElementData.mVarName, tNewNameUIElementData); } else { if (tSameNameUIElement.mGo == tNewNameUIElementData.mGo) { //同名且是同一个Go, 直接替换原有的 mUIElementDic.Remove(tNewNameUIElementData.mVarName); mUIElementDic.Add(tNewNameUIElementData.mVarName, tNewNameUIElementData); } else { //同名但不同Go, 出错了 Debug.LogError("冲突了:相同变量名, 但 GameObject 不是同一个, " + "\nGameObject 路径分别为:" + "\n" + tSameNameUIElement.mPath + "\n" + tNewNameUIElementData.mPath + "\n已删除 " + tNewNameUIElementData.mPath + " 上的冲突,如需要这个对象, 请处理名字冲突后, 重新绑定"); } } } }
/// <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); }
public void UpdateGoReference() { foreach (var tKv in mUIElementDic) { UIElementData tUIElementData = tKv.Value; tUIElementData.UpdateGo(); } }
public void RefreshUIElementState() { foreach (var tKv in mUIElementDic) { UIElementData tUIElementData = tKv.Value; tUIElementData.RefreshState(); } }
/// <summary> /// 获取元素 /// </summary> public UIElementData GetUIElementData(GameObject pGo, string pTypeName) { string tName = GetVariableName(pGo, pTypeName); UIElementData tUIElementData = null; mUIElementDic.TryGetValue(tName, out tUIElementData); return(tUIElementData); }
/// <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); } }
/// <summary> /// 是否已有同名的Go 被绑定了 /// </summary> /// <param name="pGoName"></param> /// <returns></returns> public bool HasSameNameGoBind(GameObject pGo) { string tGoName = pGo.name; foreach (var tKv in mUIElementDic) { UIElementData tElement = tKv.Value; if (tElement.mGo.name == tGoName && tElement.mGo != pGo) { return(true); } } return(false); }
/// <summary> /// 是否已添加 /// </summary> public bool IsAdd(GameObject pGo, string pType) { UIElementData tElement = GetUIElementData(pGo, pType); if (tElement == null) { return(false); } if (tElement.mGo == pGo) { return(true); } return(false); }
/// <summary> /// 获取筛选的元素数据 /// </summary> /// <param name="pFilterStr"></param> /// <returns></returns> public List <UIElementData> GetElementListByFilterStr(string pFilterStr) { string tLowFilterStr = pFilterStr == null ? "" : pFilterStr.ToLower(); List <UIElementData> tElementList = new List <UIElementData>(); foreach (var tKv in mUIElementDic) { UIElementData tElement = tKv.Value; string tLowName = tElement.mVarName.ToLower(); if (tLowName.Contains(tLowFilterStr) == false) { continue; } tElementList.Add(tKv.Value); } return(tElementList); }
public object mTypeInstance; //要绑定的类型实例 public static UIElementData Create(GameObject pRootGo, string pName, string pPath, string pTypeName) { if (pRootGo == null) { return(null); } UIElementData tData = new UIElementData(); tData.mRootGo = pRootGo; tData.mVarName = pName; tData.mPath = pPath; tData.mTypeName = pTypeName; tData.mGo = tData.GetGo(); tData.mTypeInstance = tData.GetTypeInstance(); return(tData); }
/// <summary> /// 替换掉View模板代码的特定部分 /// </summary> /// <returns></returns> public string ReplaceViewTag(string pContent) { string pPrefabPath = mPrefabRelativePath.Replace("Assets/Res/", ""); pContent = pContent.Replace("PathTag", pPrefabPath); StringBuilder tMemberTagSb = new StringBuilder(); StringBuilder tCheckNullTabSb = new StringBuilder(); StringBuilder tBindElementTagTag = new StringBuilder(); foreach (var tKv in mUIElementDic) { UIElementData tUIElementData = tKv.Value; tMemberTagSb.AppendLine(string.Format("{0,4}{1} {2};", " ", tUIElementData.mTypeName, tUIElementData.mVarName)); tCheckNullTabSb.AppendLine(string.Format( "{0,13}tElementDic.Add(new KeyValuePair<string, string>(\"{1}\",\"{2}\"));", " ", tUIElementData.mPath, tUIElementData.mTypeName)); if (tUIElementData.mTypeName == mGoTypeName) { tBindElementTagTag.AppendLine(string.Format("{0,8}{1} = mRootTransform.Find(\"{2}\").gameObject;", " ", tUIElementData.mVarName, tUIElementData.mPath)); } else { tBindElementTagTag.AppendLine(string.Format( "{0,8}{1} = mRootTransform.Find(\"{2}\").GetComponent<{3}>();", " ", tUIElementData.mVarName, tUIElementData.mPath, tUIElementData.mTypeName)); } } pContent = pContent.Replace("//MemberTag", tMemberTagSb.ToString()); pContent = pContent.Replace("//BindElementTag", tBindElementTagTag.ToString()); pContent = pContent.Replace("//CheckNullTag", tCheckNullTabSb.ToString()); return(pContent); }
public bool TryAddElement(string name, SkiaElement element) { int rank; if (!_orderedViewLayerDefinitions.TryGetValue(name, out rank)) { return(false); } UIElementData data = new UIElementData(element, name, rank); int position = 0; while (position < _elements.Count) { UIElementData existing = _elements[position]; if (existing.Rank > rank) { break; } ++position; } Children.Insert(position, element); //var framework = element as FrameworkElement; //if (framework != null) //{ // framework.Width = this.ActualWidth; // framework.Height = this.ActualHeight; //} _elements.Insert(position, data); return(true); }
/// <summary> /// 绘制已添加的元素 /// </summary> private void DrawBRElementInfo(Rect pRect) { mBRScrollPos = GUILayout.BeginScrollView(mBRScrollPos, "GroupBox"); { //调用GUILayoutUtility.GetRect 之后, 就相当于有这个 rect 的占用了 Rect tRect = GUILayoutUtility.GetRect(new GUIContent(""), "OL box NoExpand", GUILayout.Width(pRect.width - 45)); if (Event.current.type == EventType.Repaint) { mRightComponentSlectInfoRect = tRect; mRightComponentSlectInfoRect.width += 5; mRightComponentSlectInfoRect.height += 6; } if (mSearchFieldStr != mElementListFilterStr) { mElementListFilterStr = mSearchFieldStr; UpdateFilterElementList(); } mHasPathError = false; mHasReferenceNull = false; for (int i = 0; i < mFilterElementList.Count; ++i) { UIElementData tUIElementData = mFilterElementList[i]; //背景Rect Rect tBgRect = new Rect(mRightComponentSlectInfoRect); tBgRect.height -= 2; tBgRect.y += tBgRect.height * i; bool tIsSelect = mBrSelectUiElementData == tUIElementData; //空的高度占位符 GUILayout.Label("", GUILayout.Height(tBgRect.height)); //修改背景颜色 Color tColor = Color.white; if (tIsSelect) { tColor = mSelectColor; } else if (tUIElementData != null) { switch (tUIElementData.mState) { case UIElementData.eState.GoNull: case UIElementData.eState.Go_TypeNull: tColor = mErrorColor; mHasReferenceNull = true; break; case UIElementData.eState.Go_Type_PathError: tColor = mWarnColor; mHasPathError = true; break; } } //绘制空label 充当背景颜色 Color tTempColor = GUI.backgroundColor; GUI.backgroundColor = tColor; { GUI.Label(tBgRect, "", "OL box NoExpand"); } GUI.backgroundColor = tTempColor; //内容用的Rect Rect tContentRect = new Rect(tBgRect); tContentRect.x += 3; tContentRect.y += 2; tContentRect.width -= 5; GUILayout.BeginArea(tContentRect); GUILayout.BeginHorizontal(); { GUILayout.Label(tUIElementData.mVarName); //选中按钮 if (GUILayout.Button("选中", GUILayout.Width(35f))) { Selection.activeObject = tUIElementData.mGo; EditorGUIUtility.PingObject(tUIElementData.mGo); } //删除按钮 float tBtnMaxWidth = 85f; bool tWaitToDelect = mManager.IsWaitToDelete(tUIElementData.mVarName); string tBtnName = tWaitToDelect ? "取消待删" : "待删"; if (GUILayout.Button(tBtnName, GUILayout.MaxWidth(tBtnMaxWidth))) { mManager.ToggleWaitToDelete(tUIElementData.mVarName); } } GUILayout.EndHorizontal(); GUILayout.EndArea(); //背景点击监听 if (Event.current.type == EventType.MouseDown && tBgRect.Contains(Event.current.mousePosition)) { if (mBrSelectUiElementData == tUIElementData) { mBrSelectUiElementData = null; } else { mBrSelectUiElementData = tUIElementData; } } } } GUILayout.EndScrollView(); }