public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
                {
                    EditorGUI.BeginProperty(position, label, property);

                    SerializedProperty localisationkeyProperty = property.FindPropertyRelative("_localisationKey");
                    string             localisationkey         = localisationkeyProperty.stringValue;

                    float yPos = position.y;

                    Rect foldoutPosition = new Rect(position.x, yPos, position.width, EditorGUIUtility.singleLineHeight);

                    property.isExpanded = !EditorGUI.Foldout(foldoutPosition, !property.isExpanded, property.displayName + " (Localised String)");
                    yPos += EditorGUIUtility.singleLineHeight;

                    if (!property.isExpanded)
                    {
                        int origIndent = EditorGUI.indentLevel;
                        EditorGUI.indentLevel++;

                        //Draw list of possible keys
                        int currentKey = 0;
                        {
                            string[] keys = Localisation.GetStringKeys();

                            for (int i = 0; i < keys.Length; i++)
                            {
                                if (keys[i] == localisationkey)
                                {
                                    currentKey = i;
                                    break;
                                }
                            }

                            Rect typePosition = new Rect(position.x, yPos, position.width, EditorGUIUtility.singleLineHeight);
                            yPos += EditorGUIUtility.singleLineHeight;

                            EditorGUI.BeginChangeCheck();
                            currentKey = EditorGUI.Popup(typePosition, "Localisation Key", currentKey, keys);

                            if (EditorGUI.EndChangeCheck())
                            {
                                if (currentKey == 0)
                                {
                                    localisationkey = UpdateNewLocalisedStringRef(property, null);
                                }
                                else
                                {
                                    localisationkey = UpdateNewLocalisedStringRef(property, keys[currentKey]);
                                }
                            }
                        }

                        //Draw button for adding new key
                        if (currentKey == 0)
                        {
                            string[] folders            = Localisation.GetStringFolders();
                            int      currentFolderIndex = 0;
                            string   keyWithoutFolder;
                            Localisation.GetFolderIndex(localisationkey, out currentFolderIndex, out keyWithoutFolder);


                            float keyTextWidth = position.width - EditorUtils.GetLabelWidth() - (folderNameWidth + buttonSpace + autoKeybuttonWidth + buttonSpace + addButtonWidth);
                            float buttonWidth  = autoKeySlashFakeWidth + keyTextWidth + buttonSpace + autoKeybuttonWidth + buttonSpace + addButtonWidth;


                            //Get list of current folder options
                            Rect folderText = new Rect(position.x, yPos, position.width - buttonWidth, EditorGUIUtility.singleLineHeight);

                            EditorGUI.BeginChangeCheck();
                            int    newFolderIndex = EditorGUI.Popup(folderText, "New Key", currentFolderIndex, folders);
                            string currentFolder  = newFolderIndex == 0 ? "" : folders[newFolderIndex];
                            if (EditorGUI.EndChangeCheck())
                            {
                                if (newFolderIndex != 0)
                                {
                                    localisationkey = UpdateNewLocalisedStringRef(property, currentFolder + "/" + keyWithoutFolder);
                                }
                            }

                            Rect addKeySlash = new Rect(position.x + (position.width - buttonWidth) - fudge, yPos, autoKeySlashWidth, EditorGUIUtility.singleLineHeight);
                            EditorGUI.LabelField(addKeySlash, new GUIContent("/"));

                            Rect addKeyText = new Rect(position.x + (position.width - buttonWidth) - fudge + autoKeySlashFakeWidth, yPos, keyTextWidth + fudge, EditorGUIUtility.singleLineHeight);
                            if (newFolderIndex != 0)
                            {
                                EditorGUI.BeginChangeCheck();
                                keyWithoutFolder = EditorGUI.TextField(addKeyText, keyWithoutFolder);

                                if (EditorGUI.EndChangeCheck())
                                {
                                    localisationkey = UpdateNewLocalisedStringRef(property, currentFolder + "/" + keyWithoutFolder);
                                }
                            }
                            else
                            {
                                EditorGUI.BeginChangeCheck();
                                localisationkey = EditorGUI.TextField(addKeyText, localisationkey);

                                if (EditorGUI.EndChangeCheck())
                                {
                                    UpdateNewLocalisedStringRef(property, localisationkey);
                                }
                            }

                            Rect autoKeyButton = new Rect(position.x + (position.width - buttonWidth) + autoKeySlashFakeWidth + buttonSpace + keyTextWidth, yPos, autoKeybuttonWidth, EditorGUIUtility.singleLineHeight);
                            Rect addKeyButton  = new Rect(position.x + (position.width - buttonWidth) + autoKeySlashFakeWidth + buttonSpace + keyTextWidth + buttonSpace + autoKeybuttonWidth, yPos, addButtonWidth, EditorGUIUtility.singleLineHeight);

                            yPos += addKeyButton.height;

                            if (GUI.Button(autoKeyButton, "Auto"))
                            {
                                LocalisedStringRef localisedStringRef = SerializedPropertyUtils.GetSerializedPropertyValue <LocalisedStringRef>(property);

                                Component component = property.serializedObject.targetObject as Component;
                                if (component != null)
                                {
                                    string parentName = component.gameObject.name;

                                    if (component.gameObject.scene.IsValid())
                                    {
                                        parentName = component.gameObject.scene.name + "_" + parentName;
                                    }

                                    localisedStringRef.SetAutoNameParentName(parentName);
                                }

                                localisationkey = UpdateNewLocalisedStringRef(property, localisedStringRef.GetAutoKey());
                            }

                            if (GUI.Button(addKeyButton, "Add") && !string.IsNullOrEmpty(localisationkey))
                            {
                                if (!Localisation.Exists(localisationkey))
                                {
                                    Localisation.Set(localisationkey, Localisation.GetCurrentLanguage(), string.Empty);
                                    LocalisationEditorWindow.EditString(localisationkey);
                                }
                            }
                        }

                        //Draw displayed text (can be edited to update localization file)
                        {
                            //Only display if have a valid key
                            if (!string.IsNullOrEmpty(localisationkey) && Localisation.Exists(localisationkey))
                            {
                                string text       = StringUtils.GetFirstLine(Localisation.GetRawString(localisationkey));
                                float  height     = EditorGUIUtility.singleLineHeight;
                                float  labelWidth = EditorUtils.GetLabelWidth();

                                Rect textPosition = new Rect(position.x + labelWidth + 2.0f, yPos, position.width - labelWidth - 2.0f - editbuttonWidth - buttonSpace, height);
                                EditorGUI.LabelField(textPosition, text, EditorUtils.ReadonlyTextBoxStyle);
                                Rect editTextPosition = new Rect(textPosition.x + textPosition.width + buttonSpace, yPos, editbuttonWidth, EditorGUIUtility.singleLineHeight);

                                if (GUI.Button(editTextPosition, "Edit"))
                                {
                                    LocalisationEditorWindow.EditString(localisationkey);
                                }

                                yPos += height;
                            }
                        }

                        EditorGUI.indentLevel = origIndent;
                    }

                    EditorGUI.EndProperty();
                }
示例#2
0
				public static object PropertyField(object obj, GUIContent label, ref bool dataChanged, GUIStyle style, params GUILayoutOption[] options)
				{
					LocalisedStringRef localisedString = (LocalisedStringRef)obj;
					
					bool editorCollapsed = !EditorGUILayout.Foldout(!localisedString._editorCollapsed, label);

					if (editorCollapsed != localisedString._editorCollapsed)
					{
						localisedString._editorCollapsed = editorCollapsed;
						dataChanged = true;
					}

					if (!editorCollapsed)
					{
						int origIndent = EditorGUI.indentLevel;
						EditorGUI.indentLevel++;
						
						//Draw list of possible keys
						int currentKeyIndex = 0;
						{
							string[] keys = Localisation.GetStringKeys();
							string currentKey = localisedString.GetLocalisationKey();

							for (int i = 0; i < keys.Length; i++)
							{
								if (keys[i] == currentKey)
								{
									currentKeyIndex = i;
									break;
								}
							}

							EditorGUI.BeginChangeCheck();
							currentKeyIndex = EditorGUILayout.Popup("Localisation Key", currentKeyIndex, keys);

							//If key has changed
							if (EditorGUI.EndChangeCheck())
							{
								if (currentKeyIndex == 0)
								{
									localisedString = new LocalisedStringRef();
								}
								else
								{
									localisedString = new LocalisedStringRef(keys[currentKeyIndex]);
								}

								dataChanged = true;
							}
						}

						//Draw buttons for adding new key
						if (currentKeyIndex == 0)
						{
							string[] folders = Localisation.GetStringFolders();
							int currentFolderIndex = 0;
							string keyWithoutFolder;
							Localisation.GetFolderIndex(localisedString.GetLocalisationKey(), out currentFolderIndex, out keyWithoutFolder);

							EditorGUILayout.BeginHorizontal();
							{
								EditorGUILayout.LabelField(new GUIContent("New Key"), GUILayout.Width(EditorUtils.GetLabelWidth()));

								string editorParentName = localisedString.GetAutoNameParentName();

								EditorGUI.BeginChangeCheck();
								int newFolderIndex = EditorGUILayout.Popup(currentFolderIndex, folders);
								string currentFolder = newFolderIndex == 0 ? "" : folders[newFolderIndex];
								if (EditorGUI.EndChangeCheck())
								{
									if (newFolderIndex != 0)
									{
										localisedString = new LocalisedStringRef(currentFolder + "/" + keyWithoutFolder);
										localisedString.SetAutoNameParentName(editorParentName);
										dataChanged = true;
									}
								}

								EditorGUILayout.LabelField(new GUIContent("/"), GUILayout.Width(44));

								EditorGUI.BeginChangeCheck();
								keyWithoutFolder = EditorGUILayout.TextField(keyWithoutFolder);
								if (EditorGUI.EndChangeCheck())
								{
									localisedString = new LocalisedStringRef(currentFolder + "/" + keyWithoutFolder);
									localisedString.SetAutoNameParentName(editorParentName);
									dataChanged = true;
								}							

								if (GUILayout.Button("Auto", GUILayout.Width(36)))
								{
									string newKey = localisedString.GetAutoKey();
									localisedString = new LocalisedStringRef(newKey);
									localisedString.SetAutoNameParentName(editorParentName);
									dataChanged = true;
								}

								if (GUILayout.Button("Add", GUILayout.Width(32)) && !string.IsNullOrEmpty(localisedString.GetLocalisationKey()))
								{
									if (!Localisation.IsKeyInTable(localisedString.GetLocalisationKey()))
									{
										Localisation.UpdateString(localisedString.GetLocalisationKey(), Localisation.GetCurrentLanguage(), string.Empty);
										LocalisationEditorWindow.EditString(localisedString.GetLocalisationKey());
									}
									dataChanged = true;
								}
							}
							EditorGUILayout.EndHorizontal();
						}

						//Draw actual localised text (can be edited to update localisation file)
						{
							string currentKey = localisedString.GetLocalisationKey();

							//Only display if have a valid key
							if (!string.IsNullOrEmpty(currentKey) && Localisation.IsKeyInTable(currentKey))
							{
								EditorGUI.BeginChangeCheck();
								string text;
								if (style != null)
									text = EditorGUILayout.TextArea(Localisation.GetRawString(currentKey), style);
								else
									text = EditorGUILayout.TextArea(Localisation.GetRawString(currentKey));
								if (EditorGUI.EndChangeCheck())
								{
									Localisation.UpdateString(currentKey, Localisation.GetCurrentLanguage(), text);
								}
							}
						}

						EditorGUI.indentLevel = origIndent;
					}

					return localisedString;
				}
示例#3
0
                public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
                {
                    EditorGUI.BeginProperty(position, label, property);

                    bool hasChanges = false;

                    SerializedProperty localisationkeyProperty = property.FindPropertyRelative("_localisationKey");
                    string             localisationkey         = localisationkeyProperty.stringValue;

                    float yPos = position.y;

                    //Draw list of possible keys
                    int currentKey = 0;

                    {
                        string[] keys = Localisation.GetStringKeys();

                        for (int i = 0; i < keys.Length; i++)
                        {
                            if (keys[i] == localisationkey)
                            {
                                currentKey = i;
                                break;
                            }
                        }

                        Rect typePosition = new Rect(position.x, yPos, position.width, EditorGUIUtility.singleLineHeight);
                        yPos += EditorGUIUtility.singleLineHeight;

                        EditorUtils.SetBoldDefaultFont(localisationkeyProperty.prefabOverride);

                        EditorGUI.BeginChangeCheck();
                        currentKey = EditorGUI.Popup(typePosition, property.displayName + " (Localised String)", currentKey, keys);
                        if (EditorGUI.EndChangeCheck())
                        {
                            if (currentKey == 0)
                            {
                                localisationkey = UpdateNewLocalisedStringRef(property, null);
                                hasChanges      = true;
                            }
                            else
                            {
                                localisationkey = UpdateNewLocalisedStringRef(property, keys[currentKey]);
                                hasChanges      = true;
                            }
                        }

                        EditorUtils.SetBoldDefaultFont(false);
                    }

                    //Draw text preview (can be edited to update localization file)
                    if (!string.IsNullOrEmpty(localisationkey) && Localisation.Exists(localisationkey) && !localisationkeyProperty.hasMultipleDifferentValues)
                    {
                        string text       = StringUtils.GetFirstLine(Localisation.GetRawString(localisationkey, Localisation.GetCurrentLanguage()));
                        float  height     = EditorGUIUtility.singleLineHeight;
                        float  labelWidth = EditorUtils.GetLabelWidth();

                        Rect textPosition = new Rect(position.x + labelWidth + 2.0f, yPos, position.width - labelWidth - 2.0f - editbuttonWidth - buttonSpace, height);
                        EditorGUI.LabelField(textPosition, text, EditorUtils.ReadOnlyTextBoxStyle);
                        Rect editTextPosition = new Rect(textPosition.x + textPosition.width + buttonSpace, yPos, editbuttonWidth, EditorGUIUtility.singleLineHeight);

                        if (GUI.Button(editTextPosition, "Edit"))
                        {
                            LocalisationEditorWindow.EditString(localisationkey);
                        }
                    }

                    EditorGUI.EndProperty();

                    if (hasChanges)
                    {
                        property.serializedObject.ApplyModifiedProperties();
                        property.serializedObject.Update();
                    }
                }