Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3D自定义编辑器焦点随EditorGUILayout.LabelField更改_Unity3d_Unity3d Editor - Fatal编程技术网

Unity3D自定义编辑器焦点随EditorGUILayout.LabelField更改

Unity3D自定义编辑器焦点随EditorGUILayout.LabelField更改,unity3d,unity3d-editor,Unity3d,Unity3d Editor,我在使用自定义编辑器时遇到了一个问题,我的编辑焦点在没有我的角色执行任何操作的情况下发生了更改。 基本上,这是一个处于中性状态的自定义编辑器屏幕(只添加了空翻译实体): 这是一个屏幕,我的焦点无缘无故地改变了。如您所见,我的编辑器检测到两个键是相同的,并通过框顶部的消息通知它,问题是我实际上在编辑第二个框的“键”字段,并且我的焦点在第一个框上发生了变化(突出显示为蓝色)。如果我没有显示警告消息,我没有任何问题,因此我猜它来自那里 这是我的自定义编辑器脚本: [CustomEditor

我在使用自定义编辑器时遇到了一个问题,我的编辑焦点在没有我的角色执行任何操作的情况下发生了更改。 基本上,这是一个处于中性状态的自定义编辑器屏幕(只添加了空翻译实体):

这是一个屏幕,我的焦点无缘无故地改变了。如您所见,我的编辑器检测到两个键是相同的,并通过框顶部的消息通知它,问题是我实际上在编辑第二个框的“键”字段,并且我的焦点在第一个框上发生了变化(突出显示为蓝色)。如果我没有显示警告消息,我没有任何问题,因此我猜它来自那里

这是我的自定义编辑器脚本:

    [CustomEditor(typeof(InternationalizationDatabaseSO))]
    public class InternationalizationDatabaseSOEditor : Editor
    {
        #region Constantes
        private const string ITEMS_PROPERTY_NAME = "_mItems";

        private const string ITEM_CATEGORY_NAME = "_mCategory";
        private const string ITEM_KEY_NAME = "_mKey";
        private const string ITEM_VALUES_NAME = "_mValues";

        private const string ITEM_VALUE_LANGUAGE = "_mLanguage";
        private const string ITEM_VALUE_VALUE = "_mValue";
        #endregion

        #region Attributs
        private InternationalizationDatabaseSO _mDatabase;
        #endregion

        #region Methods
        private void OnEnable()
        {
            Init();
        }

        private void Init()
        {
            _mDatabase = target as InternationalizationDatabaseSO;
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            SerializedProperty itemsProperty = serializedObject.FindProperty(ITEMS_PROPERTY_NAME);

            int arraySize = itemsProperty.arraySize;

            EditorGUI.BeginDisabledGroup(false);
            {
                EditorGUILayout.BeginHorizontal();
                {
                    GUILayout.FlexibleSpace();
                    if (OnInspectorGUIButton("+", 40, 25, Color.white, Color.green))
                    {
                        itemsProperty.arraySize++;
                        itemsProperty.GetArrayElementAtIndex(itemsProperty.arraySize - 1).FindPropertyRelative(ITEM_CATEGORY_NAME).stringValue = "";
                        itemsProperty.GetArrayElementAtIndex(itemsProperty.arraySize - 1).FindPropertyRelative(ITEM_KEY_NAME).stringValue = "";
                        serializedObject.ApplyModifiedProperties();
                        Init();
                        return;
                    }
                }
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.Space();

                for(int i = 0; i < arraySize; i++)
                {
                    if(OnInspectorGUIItem(i) == false)
                    {
                        serializedObject.ApplyModifiedProperties();
                        Init();
                        return;
                    }
                }
            }

            EditorGUI.EndDisabledGroup();
            serializedObject.ApplyModifiedProperties();
        }

        private bool OnInspectorGUIItem(int index)
        {
            SerializedProperty itemsProperty = serializedObject.FindProperty(ITEMS_PROPERTY_NAME);

            SerializedProperty itemCategory = itemsProperty.GetArrayElementAtIndex(index).FindPropertyRelative(ITEM_CATEGORY_NAME);
            SerializedProperty itemKey = itemsProperty.GetArrayElementAtIndex(index).FindPropertyRelative(ITEM_KEY_NAME);

            EditorGUI.indentLevel += 1;

            EditorGUILayout.BeginVertical(GUI.skin.box);
            {
                EditorGUILayout.Space();
                EditorGUILayout.BeginHorizontal();
                {
                    EditorGUILayout.BeginVertical();
                    {
                        if(KeyAlreadyExist(index, itemKey.stringValue))
                        {
                            OnInspectorGUIText("Key already exists", 12, Color.red, FontStyle.Bold, false);
                        }
                        OnInspectorGUIText("Key : " + itemKey.stringValue, 12, FontStyle.Normal, false);
                    }
                    EditorGUILayout.EndVertical();

                    GUILayout.FlexibleSpace();
                    if(OnInspectorGUIButton("-", 40, 25, Color.white, Color.red))
                    {
                        itemCategory.stringValue = "";
                        itemKey.stringValue = "";
                        itemsProperty.DeleteArrayElementAtIndex(index);
                        return false;
                    }
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.Space();

                EditorGUILayout.BeginVertical();
                {
                    GUIStyle style = EditorStyles.foldout;
                    style.fontSize = 15;
                    style.fontStyle = FontStyle.Bold;


                    itemCategory.isExpanded = EditorGUILayout.Foldout(itemCategory.isExpanded, "General informations", style);
                    if(itemCategory.isExpanded)
                    {
                        EditorGUILayout.Space();
                        EditorGUILayout.PropertyField(itemCategory, new GUIContent("Category"));
                        EditorGUILayout.PropertyField(itemKey, new GUIContent("Key"));
                    }

                    if(OnInspectorGUIItemLanguage(index, itemsProperty.GetArrayElementAtIndex(index)) == false)
                    {
                        return false;
                    }
                }
                EditorGUILayout.EndVertical();
            }
            EditorGUILayout.EndVertical();
            EditorGUI.indentLevel -= 1;
            return true;
        }

        private bool OnInspectorGUIItemLanguage(int index, SerializedProperty propertyItem)
        {
            EditorGUILayout.BeginVertical();
            {
                GUIStyle style = EditorStyles.foldout;
                style.fontSize = 15;
                style.normal.textColor = Color.black;
                style.fontStyle = FontStyle.Bold;

                SerializedProperty itemValues = propertyItem.FindPropertyRelative(ITEM_VALUES_NAME);
                itemValues.isExpanded = EditorGUILayout.Foldout(itemValues.isExpanded, "Languages informations", style);
                if(itemValues.isExpanded)
                {
                    EditorGUILayout.Space();
                    EditorGUILayout.BeginVertical();
                    {
                        EditorGUILayout.BeginHorizontal();
                        {
                            int nbTranslation = itemValues.arraySize;
                            EditorGUILayout.LabelField("Nb translation : " + nbTranslation);
                            GUILayout.FlexibleSpace();
                            if (OnInspectorGUIButton("+", 20, 20, Color.white, Color.green))
                            {
                                itemValues.arraySize++;
                                itemValues.GetArrayElementAtIndex(itemValues.arraySize - 1).FindPropertyRelative(ITEM_VALUE_LANGUAGE).intValue = 0;
                                itemValues.GetArrayElementAtIndex(itemValues.arraySize - 1).FindPropertyRelative(ITEM_VALUE_VALUE).stringValue = "";
                                return false;
                            }
                        }
                        EditorGUILayout.EndHorizontal();
                        EditorGUILayout.Space();  
                        if (itemValues.arraySize > 0)
                        {
                            string translatedLanguages = "{";
                            foreach (InternationalizationDatabaseItemLanguage item in _mDatabase.Items[index].Values)
                            {
                                translatedLanguages += item.Language.ToString() + ", ";
                            }
                            if (translatedLanguages.Length > 2)
                            {
                                translatedLanguages = translatedLanguages.Remove(translatedLanguages.Length - 2);
                            }
                            translatedLanguages += "}";

                            //EditorStyles.label.stretchHeight = true;
                            EditorStyles.label.wordWrap = true;
                            EditorGUILayout.LabelField("Translated : \n" + translatedLanguages);
                            EditorGUILayout.Space();

                            for (int i = 0; i < itemValues.arraySize; i++)
                            {
                                EditorGUILayout.BeginVertical(GUI.skin.box);
                                {
                                    EditorGUILayout.Space();
                                    InternationalizationDatabaseItemLanguage item = _mDatabase.Items[index].Values[i];
                                    SerializedProperty propLanguage = itemValues.GetArrayElementAtIndex(i).FindPropertyRelative(ITEM_VALUE_LANGUAGE);
                                    SerializedProperty propValue = itemValues.GetArrayElementAtIndex(i).FindPropertyRelative(ITEM_VALUE_VALUE);

                                    EditorGUILayout.BeginHorizontal();
                                    {
                                        if (LanguageAlreadyExist(index, i, _mDatabase.Items[index].Values[i].Language))
                                        {
                                            OnInspectorGUIText("Warning language translation already exist", 12, Color.red, FontStyle.Bold, false);
                                        }

                                        GUILayout.FlexibleSpace();
                                        if (OnInspectorGUIButton("-", 20, 20, Color.white, Color.red))
                                        {
                                            itemValues.DeleteArrayElementAtIndex(i);
                                            return false;
                                        }
                                    }
                                    EditorGUILayout.EndHorizontal();

                                    EditorGUILayout.Space();
                                    EditorGUILayout.PropertyField(propLanguage, new GUIContent("Language"));
                                    EditorGUILayout.PrefixLabel("Translation");
                                    propValue.stringValue = EditorGUILayout.TextArea(propValue.stringValue, GUILayout.Height(80));

                                }
                                EditorGUILayout.Space();
                                EditorGUILayout.EndVertical();
                                EditorGUILayout.Space();
                            }
                        }
                    }
                    EditorGUILayout.EndVertical();
                }
            }

            EditorGUILayout.EndVertical();
            EditorGUILayout.Space();
            return true;
        }

        private bool KeyAlreadyExist(int currentIndex, string key)
        {
            for(int i = 0; i < _mDatabase.Items.Count; i++)
            {
                InternationalizationDatabaseItem item = _mDatabase.Items[i];
                if(currentIndex != i && item.Key == key && string.IsNullOrEmpty(key) == false)
                {
                    return true;
                }
            }

            return false;
        }

        private bool LanguageAlreadyExist(int indexInDatabase, int currentIndex, SystemLanguage language)
        {
            for (int i = 0; i < _mDatabase.Items[indexInDatabase].Values.Count; i++)
            {
                InternationalizationDatabaseItemLanguage item = _mDatabase.Items[indexInDatabase].Values[i];
                if (currentIndex != i && item.Language == language)
                {
                    return true;
                }
            }

            return false;
        }

        private void OnInspectorGUIText(string text, FontStyle fontStyle, bool prefixLabel)
        {
            GUIStyle style = new GUIStyle();
            style.fontStyle = fontStyle;
            style.wordWrap = true;

            if (prefixLabel)
            {
                EditorGUILayout.PrefixLabel(text, GUIStyle.none, style);
            }
            else
            {
                EditorGUILayout.LabelField(text, style);
            }
        }

        private void OnInspectorGUIText(string text, int fontSize, FontStyle fontStyle, bool prefixLabel)
        {
            GUIStyle style = new GUIStyle();
            style.fontSize = fontSize;
            style.fontStyle = fontStyle;
            style.wordWrap = true;

            if (prefixLabel)
            {
                EditorGUILayout.PrefixLabel(text, GUIStyle.none, style);
            }
            else
            {
                EditorGUILayout.LabelField(text, style);
            }
        }

        private void OnInspectorGUIText(string text, int fontSize, Color textColor, FontStyle fontStyle, bool prefixLabel)
        {
            GUIStyle style = new GUIStyle();
            style.fontSize = fontSize;
            style.normal.textColor = textColor;
            style.fontStyle = fontStyle;
            style.wordWrap = true;

            if(prefixLabel)
            {
                EditorGUILayout.PrefixLabel(text, GUIStyle.none, style);
            }
            else
            {
                EditorGUILayout.LabelField(text, style);
            }
        }

        private bool OnInspectorGUIButton(string label, int width, int height, Color textColor, Color backgroundColor)
        {
            Color saveColor = GUI.backgroundColor;
            GUILayoutOption[] options = { GUILayout.Width(width), GUILayout.Height(height) };
            GUIStyle style = new GUIStyle(GUI.skin.button);
            style.normal.textColor = textColor;
            GUI.backgroundColor = backgroundColor;

            bool pressed = GUILayout.Button(label, style, options);
            GUI.backgroundColor = saveColor;

            return pressed;
        }

        #endregion
    }
[CustomEditor(typeof(InternationalizationDatabaseSO))]
公共类国际化数据库编辑器:编辑器
{
#区域常数
private const string ITEMS\u PROPERTY\u NAME=“\u mItems”;
private const string ITEM_CATEGORY_NAME=“_mCategory”;
private const string ITEM_KEY_NAME=“_mKey”;
private const string ITEM_VALUES_NAME=“_mValues”;
private const string ITEM_VALUE_LANGUAGE=“_mllanguage”;
private const string ITEM_VALUE_VALUE=“_mValue”;
#端区
#区域属性
私有国际化数据库SOmdase;
#端区
#区域方法
私有void OnEnable()
{
Init();
}
私有void Init()
{
_mDatabase=target as InternationalizationDatabaseSO;
}
公共覆盖无效OnInspectorGUI()
{
serializedObject.Update();
SerializedProperty itemsProperty=serializedObject.FindProperty(ITEMS\u PROPERTY\u NAME);
int arraySize=itemsProperty.arraySize;
EditorGUI.BeginDisabledGroup(false);
{
EditorGUILayout.BeginHorizontal();
{
GUILayout.FlexibleSpace();
if(onInspectorGUI按钮(“+”,40,25,Color.white,Color.green))
{
itemsProperty.arraySize++;
itemsProperty.GetArrayElementIndex(itemsProperty.arraySize-1)。FindPropertyRelative(ITEM_CATEGORY_NAME)。stringValue=“”;
itemsProperty.GetArrayElementIndex(itemsProperty.arraySize-1)。FindPropertyRelative(ITEM_KEY_NAME)。stringValue=“”;
serializedObject.ApplyModifiedProperties();
Init();
返回;
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
for(int i=0;i