예제 #1
0
    private void CreateComponents(GameObject go, JSON.Object obj)
    {
        //
        // This is the 'stupid' but 'safe & predictable way of doing this.
        //
        switch (obj.GetString("type", "UnityEngine.UI.Text"))
        {
        case "UnityEngine.UI.Text":
        {
            var c = go.AddComponent <UnityEngine.UI.Text>();
            c.text      = obj.GetString("text", "Text");
            c.fontSize  = obj.GetInt("fontSize", 14);
            c.font      = FileSystem.Load <Font>("Assets/Content/UI/Fonts/" + obj.GetString("font", "RobotoCondensed-Bold.ttf"));
            c.alignment = (TextAnchor)System.Enum.Parse(typeof(TextAnchor), obj.GetString("align", "UpperLeft"));
            c.color     = ColorEx.Parse(obj.GetString("color", "1.0 1.0 1.0 1.0"));
            GraphicComponentCreated(c, obj);
            break;
        }

        case "UnityEngine.UI.Image":
        {
            var c = go.AddComponent <UnityEngine.UI.Image>();
            c.sprite   = FileSystem.Load <Sprite>(obj.GetString("sprite", "Assets/Content/UI/UI.Background.Tile.psd"));
            c.material = FileSystem.Load <Material>(obj.GetString("material", "Assets/Icons/IconMaterial.mat"));
            c.color    = ColorEx.Parse(obj.GetString("color", "1.0 1.0 1.0 1.0"));
            c.type     = (UnityEngine.UI.Image.Type)System.Enum.Parse(typeof(UnityEngine.UI.Image.Type), obj.GetString("imagetype", "Simple"));

            if (obj.ContainsKey("png"))
            {
                SetImageFromServer(c, uint.Parse(obj.GetString("png")));
            }

            GraphicComponentCreated(c, obj);

            break;
        }

        case "UnityEngine.UI.RawImage":
        {
            var c = go.AddComponent <UnityEngine.UI.RawImage>();
            c.texture = FileSystem.Load <Texture>(obj.GetString("sprite", "Assets/Icons/rust.png"));
            c.color   = ColorEx.Parse(obj.GetString("color", "1.0 1.0 1.0 1.0"));

            if (obj.ContainsKey("material"))
            {
                c.material = FileSystem.Load <Material>(obj.GetString("material"));
            }

            if (obj.ContainsKey("url"))
            {
                Rust.Global.Runner.StartCoroutine(LoadTextureFromWWW(c, obj.GetString("url")));
            }

            if (obj.ContainsKey("png"))
            {
                SetImageFromServer(c, uint.Parse(obj.GetString("png")));
            }

            GraphicComponentCreated(c, obj);

            break;
        }

        case "UnityEngine.UI.Button":
        {
            var c = go.AddComponent <UnityEngine.UI.Button>();

            if (obj.ContainsKey("command"))
            {
                var cmd = obj.GetString("command");
                c.onClick.AddListener(() => { ConsoleNetwork.ClientRunOnServer(cmd); });
            }

            if (obj.ContainsKey("close"))
            {
                var pnlName = obj.GetString("close");
                c.onClick.AddListener(() => { DestroyPanel(pnlName); });
            }

            // bg image
            var img = go.AddComponent <UnityEngine.UI.Image>();
            img.sprite   = FileSystem.Load <Sprite>(obj.GetString("sprite", "Assets/Content/UI/UI.Background.Tile.psd"));
            img.material = FileSystem.Load <Material>(obj.GetString("material", "Assets/Icons/IconMaterial.mat"));
            img.color    = ColorEx.Parse(obj.GetString("color", "1.0 1.0 1.0 1.0"));
            img.type     = (UnityEngine.UI.Image.Type)System.Enum.Parse(typeof(UnityEngine.UI.Image.Type), obj.GetString("imagetype", "Simple"));

            c.image = img;

            GraphicComponentCreated(img, obj);

            break;
        }

        case "UnityEngine.UI.Outline":
        {
            var c = go.AddComponent <UnityEngine.UI.Outline>();
            c.effectColor     = ColorEx.Parse(obj.GetString("color", "1.0 1.0 1.0 1.0"));
            c.effectDistance  = Vector2Ex.Parse(obj.GetString("distance", "1.0 -1.0"));
            c.useGraphicAlpha = obj.ContainsKey("useGraphicAlpha");
            break;
        }

        case "UnityEngine.UI.InputField":
        {
            var t = go.AddComponent <UnityEngine.UI.Text>();
            t.text      = obj.GetString("text", "Text");
            t.fontSize  = obj.GetInt("fontSize", 14);
            t.font      = FileSystem.Load <Font>("Assets/Content/UI/Fonts/" + obj.GetString("font", "RobotoCondensed-Bold.ttf"));
            t.alignment = (TextAnchor)System.Enum.Parse(typeof(TextAnchor), obj.GetString("align", "UpperLeft"));
            t.color     = ColorEx.Parse(obj.GetString("color", "1.0 1.0 1.0 1.0"));

            var c = go.AddComponent <UnityEngine.UI.InputField>();
            c.textComponent  = t;
            c.characterLimit = obj.GetInt("characterLimit", 0);

            if (obj.ContainsKey("command"))
            {
                var cmd = obj.GetString("command");
                c.onEndEdit.AddListener((value) => { ConsoleNetwork.ClientRunOnServer(cmd + " " + value); });
            }

            if (obj.ContainsKey("password"))
            {
                c.inputType = UnityEngine.UI.InputField.InputType.Password;
            }

            GraphicComponentCreated(t, obj);

            break;
        }

        case "NeedsCursor":
        {
            go.AddComponent <NeedsCursor>();
            break;
        }

        case "RectTransform":
        {
            var rt = go.GetComponent <RectTransform>();
            if (rt)
            {
                rt.anchorMin = Vector2Ex.Parse(obj.GetString("anchormin", "0.0 0.0"));
                rt.anchorMax = Vector2Ex.Parse(obj.GetString("anchormax", "1.0 1.0"));
                rt.offsetMin = Vector2Ex.Parse(obj.GetString("offsetmin", "0.0 0.0"));
                rt.offsetMax = Vector2Ex.Parse(obj.GetString("offsetmax", "1.0 1.0"));
            }
            break;
        }

        case "Countdown":
        {
            var c = go.AddComponent <Countdown>();
            c.endTime   = obj.GetInt("endTime", 0);
            c.startTime = obj.GetInt("startTime", 0);
            c.step      = obj.GetInt("step", 1);

            if (obj.ContainsKey("command"))
            {
                c.command = obj.GetString("command");
            }

            break;
        }
        }
    }
    private void CreateComponents(GameObject go, JSON.Object obj)
    {
        //
        // This is the 'stupid' but 'safe & predictable way of doing this.
        //
        switch (obj.GetString("type", "UnityEngine.UI.Text"))
        {
        case "UnityEngine.UI.Text":
        {
            var c = go.AddComponent <UnityEngine.UI.Text>();
            c.text      = obj.GetString("text", "Text");
            c.fontSize  = obj.GetInt("fontSize", 14);
            c.font      = FileSystem.Load <Font>("Assets/Content/UI/Fonts/" + obj.GetString("font", "RobotoCondensed-Bold.ttf"));
            c.alignment = ParseEnum(obj.GetString("align"), TextAnchor.UpperLeft);
            c.color     = ColorEx.Parse(obj.GetString("color", "1.0 1.0 1.0 1.0"));
            GraphicComponentCreated(c, obj);
            break;
        }

        case "UnityEngine.UI.Image":
        {
            var c = go.AddComponent <UnityEngine.UI.Image>();
            c.sprite   = FileSystem.Load <Sprite>(obj.GetString("sprite", "Assets/Content/UI/UI.Background.Tile.psd"));
            c.material = FileSystem.Load <Material>(obj.GetString("material", "Assets/Icons/IconMaterial.mat"));
            c.color    = ColorEx.Parse(obj.GetString("color", "1.0 1.0 1.0 1.0"));
            c.type     = ParseEnum(obj.GetString("imagetype", "Simple"), UnityEngine.UI.Image.Type.Simple);

            if (obj.ContainsKey("png") && uint.TryParse(obj.GetString("png"), out var id))
            {
                SetImageFromServer(c, id);
            }

            if (obj.ContainsKey("itemid"))
            {
                var itemdef = ItemManager.FindItemDefinition(obj.GetInt("itemid"));
                if (itemdef != null)
                {
                    c.material = null;
                    c.sprite   = itemdef.iconSprite;

                    if (obj.ContainsKey("skinid"))
                    {
                        var requestedSkin = obj.GetInt("skinid");
                        var skin          = itemdef.skins.FirstOrDefault(x => x.id == requestedSkin);
                        if (skin.id == requestedSkin)
                        {
                            c.sprite = skin.invItem.icon;
                        }
                        else
                        {
                            var workshopSprite = WorkshopIconLoader.Find((ulong)requestedSkin);
                            if (workshopSprite != null)
                            {
                                c.sprite = workshopSprite;
                            }
                        }
                    }
                }
            }

            GraphicComponentCreated(c, obj);

            break;
        }

        case "UnityEngine.UI.RawImage":
        {
            var c = go.AddComponent <UnityEngine.UI.RawImage>();
            c.texture = FileSystem.Load <Texture>(obj.GetString("sprite", "Assets/Icons/rust.png"));
            c.color   = ColorEx.Parse(obj.GetString("color", "1.0 1.0 1.0 1.0"));

            if (obj.ContainsKey("material"))
            {
                c.material = FileSystem.Load <Material>(obj.GetString("material"));
            }

            if (obj.ContainsKey("url"))
            {
                Rust.Global.Runner.StartCoroutine(LoadTextureFromWWW(c, obj.GetString("url")));
            }

            if (obj.ContainsKey("png") && uint.TryParse(obj.GetString("png"), out var id))
            {
                SetImageFromServer(c, id);
            }

            GraphicComponentCreated(c, obj);

            break;
        }

        case "UnityEngine.UI.Button":
        {
            var c = go.AddComponent <UnityEngine.UI.Button>();

            if (obj.ContainsKey("command"))
            {
                var cmd = obj.GetString("command");
                c.onClick.AddListener(() => { ConsoleNetwork.ClientRunOnServer(cmd); });
            }

            if (obj.ContainsKey("close"))
            {
                var pnlName = obj.GetString("close");
                c.onClick.AddListener(() => { DestroyPanel(pnlName); });
            }

            // bg image
            var img = go.AddComponent <UnityEngine.UI.Image>();
            img.sprite   = FileSystem.Load <Sprite>(obj.GetString("sprite", "Assets/Content/UI/UI.Background.Tile.psd"));
            img.material = FileSystem.Load <Material>(obj.GetString("material", "Assets/Icons/IconMaterial.mat"));
            img.color    = ColorEx.Parse(obj.GetString("color", "1.0 1.0 1.0 1.0"));
            img.type     = ParseEnum(obj.GetString("imagetype", "Simple"), UnityEngine.UI.Image.Type.Simple);

            c.image = img;

            GraphicComponentCreated(img, obj);

            break;
        }

        case "UnityEngine.UI.Outline":
        {
            var c = go.AddComponent <UnityEngine.UI.Outline>();
            c.effectColor     = ColorEx.Parse(obj.GetString("color", "1.0 1.0 1.0 1.0"));
            c.effectDistance  = Vector2Ex.Parse(obj.GetString("distance", "1.0 -1.0"));
            c.useGraphicAlpha = obj.ContainsKey("useGraphicAlpha");
            break;
        }

        case "UnityEngine.UI.InputField":
        {
            var t = go.AddComponent <UnityEngine.UI.Text>();
            t.fontSize  = obj.GetInt("fontSize", 14);
            t.font      = FileSystem.Load <Font>("Assets/Content/UI/Fonts/" + obj.GetString("font", "RobotoCondensed-Bold.ttf"));
            t.alignment = ParseEnum(obj.GetString("align"), TextAnchor.UpperLeft);
            t.color     = ColorEx.Parse(obj.GetString("color", "1.0 1.0 1.0 1.0"));

            var c = go.AddComponent <UnityEngine.UI.InputField>();
            c.textComponent  = t;
            c.characterLimit = obj.GetInt("characterLimit", 0);

            if (obj.ContainsKey("command"))
            {
                var cmd = obj.GetString("command");
                c.onEndEdit.AddListener((value) => { ConsoleNetwork.ClientRunOnServer(cmd + " " + value); });
            }
            c.text     = obj.GetString("text", "Text");
            c.readOnly = obj.GetBoolean("readOnly", false);
            c.lineType = ParseEnum(obj.GetString("lineType", "SingleLine"), InputField.LineType.SingleLine);
            if (obj.ContainsKey("password"))
            {
                c.inputType = UnityEngine.UI.InputField.InputType.Password;
            }

            GraphicComponentCreated(t, obj);

            break;
        }

        case "NeedsCursor":
        {
            go.AddComponent <NeedsCursor>();
            break;
        }

        case "RectTransform":
        {
            var rt = go.GetComponent <RectTransform>();
            if (rt)
            {
                rt.anchorMin = Vector2Ex.Parse(obj.GetString("anchormin", "0.0 0.0"));
                rt.anchorMax = Vector2Ex.Parse(obj.GetString("anchormax", "1.0 1.0"));
                rt.offsetMin = Vector2Ex.Parse(obj.GetString("offsetmin", "0.0 0.0"));
                rt.offsetMax = Vector2Ex.Parse(obj.GetString("offsetmax", "1.0 1.0"));
            }
            break;
        }

        case "Countdown":
        {
            var c = go.AddComponent <Countdown>();
            c.endTime   = obj.GetInt("endTime", 0);
            c.startTime = obj.GetInt("startTime", 0);
            c.step      = obj.GetInt("step", 1);

            if (obj.ContainsKey("command"))
            {
                c.command = obj.GetString("command");
            }

            break;
        }

        case "UnityEngine.UI.VerticalLayoutGroup":
        {
            var        vlg          = go.AddComponent <VerticalLayoutGroup>();
            var        paddingArray = obj.GetString("padding", "0").Split(' ');
            RectOffset padding      = null;
            if (paddingArray.Length > 0)
            {
                padding      = new RectOffset();
                padding.left = int.Parse(paddingArray[0]);

                if (paddingArray.Length > 1)
                {
                    padding.right = int.Parse(paddingArray[1]);

                    if (paddingArray.Length > 2)
                    {
                        padding.top = int.Parse(paddingArray[2]);

                        if (paddingArray.Length > 3)
                        {
                            padding.bottom = int.Parse(paddingArray[3]);
                        }
                    }
                }
            }

            if (padding != null)
            {
                vlg.padding = padding;
            }

            vlg.spacing        = obj.GetInt("spacing", 0);
            vlg.childAlignment = (TextAnchor)System.Enum.Parse(typeof(TextAnchor), obj.GetString("childAlignment", "UpperLeft"));

            if (obj.ContainsKey("childControlWidth"))
            {
                vlg.childControlWidth = true;
            }

            if (obj.ContainsKey("childControlHeight"))
            {
                vlg.childControlHeight = true;
            }

            if (obj.ContainsKey("childForceExpandWidth"))
            {
                vlg.childForceExpandWidth = true;
            }

            if (obj.ContainsKey("childForceExpandHeight"))
            {
                vlg.childForceExpandHeight = true;
            }
            break;
        }

        case "UnityEngine.UI.HorizontalLayoutGroup":
        {
            var        hlg          = go.AddComponent <HorizontalLayoutGroup>();
            var        paddingArray = obj.GetString("padding", "0").Split(' ');
            RectOffset padding      = null;
            if (paddingArray.Length > 0)
            {
                padding      = new RectOffset();
                padding.left = int.Parse(paddingArray[0]);

                if (paddingArray.Length > 1)
                {
                    padding.right = int.Parse(paddingArray[1]);

                    if (paddingArray.Length > 2)
                    {
                        padding.top = int.Parse(paddingArray[2]);

                        if (paddingArray.Length > 3)
                        {
                            padding.bottom = int.Parse(paddingArray[3]);
                        }
                    }
                }
            }

            if (padding != null)
            {
                hlg.padding = padding;
            }

            hlg.spacing        = obj.GetInt("spacing", 0);
            hlg.childAlignment = (TextAnchor)System.Enum.Parse(typeof(TextAnchor), obj.GetString("childAlignment", "UpperLeft"));

            if (obj.ContainsKey("childControlWidth"))
            {
                hlg.childControlWidth = true;
            }

            if (obj.ContainsKey("childControlHeight"))
            {
                hlg.childControlHeight = true;
            }

            if (obj.ContainsKey("childForceExpandWidth"))
            {
                hlg.childForceExpandWidth = true;
            }

            if (obj.ContainsKey("childForceExpandHeight"))
            {
                hlg.childForceExpandHeight = true;
            }
            break;
        }

        case "UnityEngine.UI.LayoutElement":
        {
            var le = go.AddComponent <LayoutElement>();

            if (obj.ContainsKey("ignoreLayout"))
            {
                le.ignoreLayout = true;
                break;
            }

            if (obj.ContainsKey("minWidth"))
            {
                le.minWidth = obj.GetFloat("minWidth", 0);
            }

            if (obj.ContainsKey("minHeight"))
            {
                le.minHeight = obj.GetFloat("minHeight", 0);
            }

            if (obj.ContainsKey("preferredWidth"))
            {
                le.preferredWidth = obj.GetFloat("preferredWidth", 0);
            }

            if (obj.ContainsKey("preferredHeight"))
            {
                le.preferredHeight = obj.GetFloat("preferredHeight", 0);
            }

            if (obj.ContainsKey("flexibleWidth"))
            {
                le.flexibleWidth = obj.GetFloat("flexibleWidth", 0);
            }

            if (obj.ContainsKey("flexibleHeight"))
            {
                le.flexibleHeight = obj.GetFloat("flexibleHeight", 0);
            }

            if (obj.ContainsKey("layoutPriority"))
            {
                le.layoutPriority = obj.GetInt("layoutPriority", 1);
            }

            break;
        }

        case "NeedsKeyboard":
        {
            go.AddComponent <NeedsKeyboard>();
            break;
        }
        }
    }