コード例 #1
0
    /// <summary>
    /// Parses the dialog localization XML file.
    /// </summary>
    static IDictionary <string, LocalizedDialogue> ParseDialogLocalizationXMLFile(XmlDocument xmlDoc)
    {
        IDictionary <string, LocalizedDialogue> ret = new Dictionary <string, LocalizedDialogue>();
        XmlElement  root        = xmlDoc.DocumentElement;
        XmlNodeList xmlNodeList = root.GetElementsByTagName("dialog");

        foreach (XmlNode dialogNode in xmlNodeList)
        {
            XmlElement        dialogElement     = (XmlElement)dialogNode;
            LocalizedDialogue localizedDialogue = new LocalizedDialogue();
            localizedDialogue.DialogID = dialogElement.GetAttribute("id");
            foreach (XmlNode dialogItemNode in dialogElement.ChildNodes)
            {
                XmlElement            dialogItemElement     = (XmlElement)dialogItemNode;
                LocalizedDialogueItem localizedDialogueItem = new LocalizedDialogueItem();
                localizedDialogueItem.DialogCharacterID = dialogItemElement.GetAttribute("character");
                localizedDialogueItem.DialogImageName   = dialogItemElement.GetAttribute("image");
                if (dialogItemElement.HasAttribute("pend"))
                {
                    localizedDialogueItem.pendAfterFinished = float.Parse(dialogItemElement.GetAttribute("pend"));
                }

                localizedDialogue.dialogueItem.Add(localizedDialogueItem);
            }
            ret.Add(localizedDialogue.DialogID, localizedDialogue);
        }
        return(ret);
    }
コード例 #2
0
ファイル: GameDialogue.cs プロジェクト: Hengle/colonist-1
    /// <summary>
    /// Displaies the dialogue.
    /// if AnimatedTypingLetter = true, the letters will be typed to screen one by one
    /// if AnimatedTypingLetter = false. the dialog text will be flashed out to screen in one time.
    /// </summary>
    IEnumerator DisplayDialogue(string DialogueID)
    {
        LocalizedDialogue dialog = Localization.GetDialogue(DialogueID);

        HasDialog = true;

        foreach (LocalizedDialogueItem dialogItem in dialog.dialogueItem)
        {
            LocalizeCharacter speaker = Localization.GetCharacter(dialogItem.DialogCharacterID);
            this.Portrait     = speaker.PortraitIconTexture;
            this.SpearkerName = speaker.CharacterName;
            float delay = dialogItem.pendAfterFinished.HasValue ? dialogItem.pendAfterFinished.Value : 2;
            if (!string.IsNullOrEmpty(dialogItem.DialogImageName))
            {
                displayedImage = (Texture)Resources.Load(Localization.DialogAssetRootFolder + "/" +
                                                         LevelName + "/" + this.targetLanguage + "/" + dialogItem.DialogImageName,
                                                         typeof(Texture));
            }
            else
            {
                displayedImage = null;
            }
            yield return(new WaitForSeconds(delay));
        }
        HasDialog = false;
    }
コード例 #3
0
        public LocalizedDialogue GetLocalizedDialogue(Dialogue dia)
        {
            LocalizedDialogue found = null;

            if (localizedDialoguesMap == null || localizedDialogueLinesMap == null)
            {
                if (!activeLocale.Equals(settings.defaultLocale) || !settings.isDefaultEmbeded)
                {
                    Debug.LogError("The dialogue was not loaded for the current locale: " + activeLocale.langName + "(" + activeLocale.langCode + ")");
                }
            }
            else if (!string.IsNullOrEmpty(dia.translationKey) && localizedDialoguesMap.ContainsKey(dia.translationKey))
            {
                found = localizedDialoguesMap[dia.translationKey];
            }
            return(found);
        }
コード例 #4
0
 public void LoadDialogues(params Dialogue[] dialogues)
 {
     if (activeLocale.Equals(settings.defaultLocale) && settings.isDefaultEmbeded)
     {
         // If it's embeded there is no need to load nothing
     }
     else
     {
         // Otherwise we will need to load them
         // First clear up the current maps
         localizedDialoguesMap     = null;
         localizedDialogueLinesMap = null;
         if (dialogues != null && dialogues.Length > 0)
         {
             localizedDialoguesMap     = new Dictionary <string, LocalizedDialogue>();
             localizedDialogueLinesMap = new Dictionary <string, LocalizedDialogueLine>();
             foreach (Dialogue dia in dialogues)
             {
                 LocalizedDialogue ld = Resources.Load <LocalizedDialogue>(settings.baseFolder + "/" + activeLocale.langCode + "/Dialogue/" + dia.translationKey);
                 if (ld != null)
                 {
                     localizedDialoguesMap.Add(dia.translationKey, ld);
                     if (ld.Lines != null && ld.Lines.Count > 0)
                     {
                         foreach (LocalizedDialogueLine line in ld.Lines)
                         {
                             localizedDialogueLinesMap.Add(line.translationKey, line);
                         }
                     }
                 }
                 else
                 {
                     Debug.LogWarningFormat("Localized Dialogue {0} not found for locale {1}({2})", dia.name, activeLocale.langName, activeLocale.langCode);
                 }
             }
         }
     }
 }
        private void OnWizardCreate()
        {
            // Validate the original dialogues
            if (originalDialogues == null || originalDialogues.Count == 0)
            {
                EditorUtility.DisplayDialog("Error", "You need to specify at least one original dialogue", "OK");
            }
            foreach (Dialogue dia in originalDialogues)
            {
                if (dia == null || dia.Lines == null || dia.Lines.Count == 0)
                {
                    EditorUtility.DisplayDialog("Error", "An empty dialogue was specified", "OK");
                    return;
                }
            }
            List <LocalizedDialogue> localizedDialogues = new List <LocalizedDialogue>();

            string path = EditorUtility.OpenFilePanelWithFilters("Open dialogue translations file", "", new string[] { "Text file", "txt" });

            if (path.Length != 0)
            {
                using (StreamReader reader = new StreamReader(path))
                {
                    LocalizedDialogue currentDialogue = CreateInstance <LocalizedDialogue>();
                    currentDialogue.Lines = new List <LocalizedDialogueLine>();
                    localizedDialogues.Add(currentDialogue);
                    Regex regex = new Regex(@"^(.+?)(?: *):(?: *)(.*)$");
                    LocalizedDialogueLine lastLine = null;
                    int emptyLineCounter           = 0;
                    while (!reader.EndOfStream)
                    {
                        string line = reader.ReadLine();
                        if (line.StartsWith("//"))
                        {
                            emptyLineCounter = 0;
                            continue;
                        }

                        if (line.Trim().Length == 0)
                        {
                            emptyLineCounter++;
                        }
                        else
                        {
                            if (emptyLineCounter >= 2)
                            {
                                currentDialogue       = CreateInstance <LocalizedDialogue>();
                                currentDialogue.Lines = new List <LocalizedDialogueLine>();
                                localizedDialogues.Add(currentDialogue);
                                lastLine = null;
                            }

                            if (regex.IsMatch(line))
                            {
                                Match m = regex.Match(line);
                                // We don't care about the character name because it will be taken from the original
                                string lineContent = m.Groups[2].Value;
                                LocalizedDialogueLine localizedLine = new LocalizedDialogueLine();
                                localizedLine.Text = lineContent;
                                lastLine           = localizedLine;
                                currentDialogue.Lines.Add(localizedLine);
                            }
                            else
                            {
                                if (lastLine == null)
                                {
                                    EditorUtility.DisplayDialog("Format incorrect", "The file didn't comply with the format", "OK");
                                    return;
                                }
                                // Trim whitespaces to ensure that there are none before or after the line break character
                                string textContent = lastLine.Text.Trim();
                                textContent  += "\n";
                                textContent  += line.Trim();
                                lastLine.Text = textContent;
                            }

                            emptyLineCounter = 0;
                        }
                    }
                }
            }

            // Validate the number of dialogues and lines
            if (originalDialogues.Count != localizedDialogues.Count)
            {
                EditorUtility.DisplayDialog("Error", "The number of dialogues in the input file mismatch with the original dialogues specified", "OK");
                return;
            }
            for (int i = 0; i < originalDialogues.Count; i++)
            {
                if (originalDialogues[i].Lines.Count != localizedDialogues[i].Lines.Count)
                {
                    EditorUtility.DisplayDialog("Error", "The number of lines in the input file mismatch with the original dialogues specified", "OK");
                    return;
                }
            }
            // If the code reaches here it means that we can save the localizations now.
            string baseFolder      = "Resources/" + localeSettings.baseFolder + "/" + locale.langCode + "/Dialogue";
            string targetDirectory = CreateIntermediateFolders(baseFolder);

            AssetDatabase.StartAssetEditing();
            for (int i = 0; i < originalDialogues.Count; i++)
            {
                Dialogue          dia       = originalDialogues[i];
                LocalizedDialogue localized = localizedDialogues[i];
                if (string.IsNullOrEmpty(dia.translationKey))
                {
                    dia.translationKey = "dialogue-" + ShortGuid.NewGuid();
                }
                localized.translationKey = dia.translationKey;
                for (int x = 0; x < dia.Lines.Count; x++)
                {
                    if (string.IsNullOrEmpty(dia.Lines[x].translationKey))
                    {
                        dia.Lines[x].translationKey = "line-" + ShortGuid.NewGuid();
                    }
                    localized.Lines[x].original       = dia.Lines[x];
                    localized.Lines[x].translationKey = dia.Lines[x].translationKey;
                }
                AssetDatabase.CreateAsset(localized, targetDirectory + "/" + localized.translationKey + ".asset");
            }
            AssetDatabase.StopAssetEditing();
            AssetDatabase.SaveAssets();
        }
コード例 #6
0
ファイル: Localization.cs プロジェクト: hiyouke/colonist
    /// <summary>
    /// Parses the dialog localization XML file.
    /// </summary>
    static IDictionary<string, LocalizedDialogue> ParseDialogLocalizationXMLFile(XmlDocument xmlDoc)
    {
        IDictionary<string, LocalizedDialogue> ret = new Dictionary<string, LocalizedDialogue>();
        XmlElement root = xmlDoc.DocumentElement;
        XmlNodeList xmlNodeList = root.GetElementsByTagName("dialog");
        foreach (XmlNode dialogNode in xmlNodeList)
        {
            XmlElement dialogElement = (XmlElement)dialogNode;
            LocalizedDialogue localizedDialogue = new LocalizedDialogue();
            localizedDialogue.DialogID = dialogElement.GetAttribute("id");
            foreach (XmlNode dialogItemNode in dialogElement.ChildNodes)
            {
                XmlElement dialogItemElement = (XmlElement)dialogItemNode;
                LocalizedDialogueItem localizedDialogueItem = new LocalizedDialogueItem();
                localizedDialogueItem.DialogCharacterID = dialogItemElement.GetAttribute("character");
                localizedDialogueItem.DialogImageName = dialogItemElement.GetAttribute("image");
                if (dialogItemElement.HasAttribute("pend"))
                {
                    localizedDialogueItem.pendAfterFinished = float.Parse(dialogItemElement.GetAttribute("pend"));
                }

                localizedDialogue.dialogueItem.Add(localizedDialogueItem);
            }
            ret.Add(localizedDialogue.DialogID, localizedDialogue);
        }
        return ret;
    }