Exemplo n.º 1
0
    /// <summary>
    /// Loads the Mod Dialog Box from the XML file.
    /// </summary>
    /// <param name="file">The FileInfo object that references to the XML file.</param>
    public void LoadFromXML(FileInfo file)
    {
        // TODO: Find a better way to do this. Not user friendly/Expansible.
        // ModDialogBox -> Dialog Background
        //                 |-> Title
        //                 |-> Content
        Content = transform.GetChild(0).GetChild(1);

        controls = new Dictionary <string, DialogControl>();

        XmlSerializer serializer = new XmlSerializer(typeof(ModDialogBoxInformation));

        try
        {
            ModDialogBoxInformation dialogBoxInfo = (ModDialogBoxInformation)serializer.Deserialize(file.OpenRead());
            Title = dialogBoxInfo.title;
            foreach (DialogComponent gameObjectInfo in dialogBoxInfo.content)
            {
                // Implement new DialogComponents in here.
                switch (gameObjectInfo.ObjectType)
                {
                case "Text":
                    GameObject textObject = (GameObject)Instantiate(Resources.Load("Prefab/DialogBoxPrefabs/DialogText"), Content);
                    textObject.GetComponent <Text>().text = (string)gameObjectInfo.data;
                    textObject.GetComponent <RectTransform>().anchoredPosition = gameObjectInfo.position;
                    break;

                case "Input":
                    GameObject inputObject = (GameObject)Instantiate(Resources.Load("Prefab/DialogBoxPrefabs/DialogInputComponent"), Content);
                    inputObject.GetComponent <RectTransform>().anchoredPosition = gameObjectInfo.position;
                    inputObject.GetComponent <RectTransform>().sizeDelta        = gameObjectInfo.size;
                    controls[gameObjectInfo.name] = inputObject.GetComponent <DialogControl>();
                    break;

                case "Image":
                    GameObject imageObject  = (GameObject)Instantiate(Resources.Load("Prefab/DialogBoxPrefabs/DialogImage"), Content);
                    Texture2D  imageTexture = new Texture2D((int)gameObjectInfo.size.x, (int)gameObjectInfo.size.y);
                    try
                    {
                        imageTexture.LoadImage(File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, (string)gameObjectInfo.data)));
                        Sprite imageSprite = Sprite.Create(imageTexture, new Rect(0, 0, gameObjectInfo.size.x, gameObjectInfo.size.y), Vector2.zero);

                        imageObject.GetComponent <Image>().sprite = imageSprite;
                        imageObject.GetComponent <RectTransform>().anchoredPosition = gameObjectInfo.position;
                        imageObject.GetComponent <RectTransform>().sizeDelta        = gameObjectInfo.size;
                        controls[gameObjectInfo.name] = imageObject.GetComponent <DialogControl>();
                    }
                    catch (System.Exception error)
                    {
                        Debug.ULogErrorChannel("ModDialogBox", "Error converting image:" + error.Message);
                        return;
                    }

                    break;

                case "Button":
                    GameObject buttonObject = (GameObject)Instantiate(Resources.Load("Prefab/DialogBoxPrefabs/DialogButton"));
                    buttonObject.GetComponent <RectTransform>().anchoredPosition = gameObjectInfo.position;
                    buttonObject.GetComponent <RectTransform>().sizeDelta        = gameObjectInfo.size;
                    buttonObject.GetComponentInChildren <Text>().text            = (string)gameObjectInfo.data;
                    buttonObject.GetComponent <DialogButton>().buttonName        = gameObjectInfo.name;
                    controls[gameObjectInfo.name] = buttonObject.GetComponent <DialogButton>();

                    break;
                }
            }

            // Enable dialog buttons from the list of buttons.
            foreach (DialogBoxResult buttons in dialogBoxInfo.buttons)
            {
                switch (buttons)
                {
                case DialogBoxResult.Yes:
                    gameObject.transform.GetChild(0).transform.Find("Buttons/btnYes").gameObject.SetActive(true);
                    break;

                case DialogBoxResult.No:
                    gameObject.transform.GetChild(0).transform.Find("Buttons/btnNo").gameObject.SetActive(true);
                    break;

                case DialogBoxResult.Cancel:
                    gameObject.transform.GetChild(0).transform.Find("Buttons/btnCancel").gameObject.SetActive(true);
                    break;

                case DialogBoxResult.Okay:
                    gameObject.transform.GetChild(0).transform.Find("Buttons/btnOK").gameObject.SetActive(true);
                    break;
                }
            }

            EventActions eventActions = new EventActions();
            foreach (KeyValuePair <string, string> pair in dialogBoxInfo.Actions)
            {
                eventActions.Register(pair.Key, pair.Value);
            }

            events = eventActions;

            FunctionsManager.Get("ModDialogBox").RegisterGlobal(typeof(ModDialogBox));
            extraData = new List <object>();
        }
        catch (System.Exception error)
        {
            Debug.ULogErrorChannel("ModDialogBox", "Error deserializing data:" + error.Message);
        }
    }