Beispiel #1
0
        //private void CreateDataList(SerializedObject serializedObject)
        //{
        //    // 第二个参数是 SerializedProperty elements 数组
        //    // 我们的数组是根据不同配置文件获取的,所以动态设置,初始化为 null
        //    m_DataList = new ReorderableList(serializedObject, null)
        //    {
        //        drawHeaderCallback = DataList_DrawHeaderCallback,
        //        elementHeightCallback = DataList_ElementHeightCallback,
        //        drawElementCallback = DataList_DrawElementCallback
        //    };
        //}

        //private void DataList_DrawHeaderCallback(Rect rect)
        //{
        //    EditorGUI.LabelField(rect, "Datas");
        //}

        //private float DataList_ElementHeightCallback(int index)
        //{
        //    SerializedProperty dataProperty = m_DataList.serializedProperty.GetArrayElementAtIndex(index);
        //    return EditorGUI.GetPropertyHeight(dataProperty);
        //}

        //private void DataList_DrawElementCallback(Rect rect, int index, bool isActive, bool isFocused)
        //{
        //    SerializedProperty dataProperty = m_DataList.serializedProperty.GetArrayElementAtIndex(index);
        //    EditorGUI.PropertyField(rect, dataProperty, true);
        //}
        #endregion

        #region Save, Load, Check and Sort
        private void SaveToFile(IEditorConfigSerializer config)
        {
            string ext  = (config is XmlConfigFile) ? "xml" : "txt";
            string path = EditorUtility.SaveFilePanel(
                "Save", Application.streamingAssetsPath, config.GetType().Name, ext);

            if (!string.IsNullOrEmpty(path))
            {
                if (!CheckDuplicateKeys(config))
                {
                    Debug.LogError("Config to save has some `Duplicate Keys`. Save Failure.");
                    return;
                }

                try
                {
                    byte[] bytes = config.EditorSerializeToBytes();
                    File.WriteAllBytes(path, bytes);
                    AssetDatabase.Refresh();
                }
                catch (Exception e)
                {
                    Debug.LogError("Save ERROR: " + e.ToString());
                    return;
                }
            }
        }
Beispiel #2
0
        private void LoadFromFile(IEditorConfigSerializer config)
        {
            string ext  = (config is XmlConfigFile) ? "xml" : "txt";
            string path = EditorUtility.OpenFilePanel(
                "Load", Application.streamingAssetsPath, ext);

            if (!string.IsNullOrEmpty(path))
            {
                try
                {
                    byte[] bytes = File.ReadAllBytes(path);
                    config.EditorDeserializeToObject(bytes);
                    EditorUtility.SetDirty(srpgData);
                    Repaint();
                }
                catch (Exception e)
                {
                    Debug.LogError("Load ERROR: " + e.ToString());
                    return;
                }

                if (!CheckDuplicateKeys(config))
                {
                    Debug.LogError("Loaded File has some `Duplicate Keys`.");
                    return;
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// 检查重复的Key
        /// </summary>
        /// <returns></returns>
        private bool CheckDuplicateKeys(IEditorConfigSerializer config)
        {
            // 获取所有key
            Array keys = config.EditorGetKeys();

            // key : index
            Dictionary <object, int> keySet = new Dictionary <object, int>();

            // dumplicate [key : indexes]
            Dictionary <object, HashSet <string> > duplicateKeys = new Dictionary <object, HashSet <string> >();

            for (int i = 0; i < keys.Length; i++)
            {
                object key = keys.GetValue(i);

                // 如果key重复了
                if (keySet.ContainsKey(key))
                {
                    // 如果重复key的set没有建立
                    if (!duplicateKeys.ContainsKey(key))
                    {
                        // 建立set,并加入最初的下标
                        duplicateKeys[key] = new HashSet <string>
                        {
                            keySet[key].ToString()
                        };
                    }

                    // 加入当前下标
                    duplicateKeys[key].Add(i.ToString());
                }
                else
                {
                    keySet.Add(key, i);
                }
            }

            if (duplicateKeys.Count != 0)
            {
                // 打印所有重复的keys
                foreach (var kvp in duplicateKeys)
                {
                    Debug.LogErrorFormat(
                        "Duplicate Keys \"{0}\": Index [{1}]",
                        kvp.Key.ToString(),
                        string.Join(", ", kvp.Value.ToArray()));
                }
                return(false);
            }

            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// 绘制按钮
        /// </summary>
        private bool DoDrawButtons()
        {
            IEditorConfigSerializer config = srpgData.GetCurConfig();

            if (config == null)
            {
                EditorGUILayout.HelpBox(
                    string.Format("{0} Config is not found.", srpgData.currentConfig.ToString()),
                    MessageType.Error);
                return(false);
            }

            EditorGUILayout.BeginHorizontal();
            {
                if (GUILayout.Button("Save To File", m_BtnWidth))
                {
                    SaveToFile(config);
                }

                if (GUILayout.Button("Load From File", m_BtnWidth))
                {
                    LoadFromFile(config);
                }

                if (GUILayout.Button("Check Keys", m_BtnWidth))
                {
                    CheckDuplicateKeys(config);
                }

                if (GUILayout.Button("Sort Datas", m_BtnWidth))
                {
                    SortWithKeys(config);
                }
            }
            EditorGUILayout.EndHorizontal();

            return(true);
        }
Beispiel #5
0
 private void SortWithKeys(IEditorConfigSerializer config)
 {
     config.EditorSortDatas();
 }