protected override JSONObject ToJSON(WXHierarchyContext context)
        {
            if (uiSprite != null)
            {
                //if (uiSprite.isAnchored)
                //{
                //    JSONObject anchorJson = new JSONObject(JSONObject.Type.OBJECT);
                //    anchorJson.AddField("type", "UIWidget");
                //    JSONObject anchorSubJSON = new JSONObject(JSONObject.Type.OBJECT);
                //    anchorSubJSON.AddField("leftMargin", uiSprite.leftAnchor.absolute);
                //    anchorSubJSON.AddField("rightMargin", uiSprite.rightAnchor.absolute);
                //    anchorSubJSON.AddField("topMargin", uiSprite.topAnchor.absolute);
                //    anchorSubJSON.AddField("bottomMargin", uiSprite.bottomAnchor.absolute);
                //    anchorJson.AddField("data", anchorSubJSON);

                //}

                JSONObject json = new JSONObject(JSONObject.Type.OBJECT);
                json.AddField("type", "UISprite");

                JSONObject subJSON = new JSONObject(JSONObject.Type.OBJECT);

                JSONObject color = new JSONObject(JSONObject.Type.ARRAY);
                color.Add(255f * uiSprite.color.r);
                color.Add(255f * uiSprite.color.g);
                color.Add(255f * uiSprite.color.b);
                color.Add(255f * uiSprite.color.a);
                subJSON.AddField("color", color);
                subJSON.AddField("colorBlendType", 0);

                //if (uiSprite.name == "PageBagChecked")
                //{
                //    Debug.Log("++==++==++");

                //    Debug.Log(uiSprite.isAnchored);
                //    Debug.Log(uiSprite.bottomAnchor.absolute);
                //    Debug.Log(uiSprite.bottomAnchor.target.name);
                //}

                UI2DSprite.Type type = uiSprite.type;
                subJSON.AddField("type", (int)type);

                UI2DSprite.Flip flipType = uiSprite.flip;
                subJSON.AddField("flip", (int)flipType);
                subJSON.AddField("fillCenter", uiSprite.centerType == UI2DSprite.AdvancedType.Sliced);
                subJSON.AddField("fillDir", (int)uiSprite.fillDirection);
                subJSON.AddField("fillAmount", uiSprite.fillAmount);
                subJSON.AddField("invertFill", uiSprite.invert);

                if (uiSprite.atlas != null)
                {
                    string uuid = WXSpriteFrame.getSprite(uiSprite.atlas, uiSprite.spriteName, context.preset);
                    if (uuid == null)
                    {
                        Debug.LogWarning("获取sprite失败: " + uiSprite.spriteName);
                    }
                    else
                    {
                        context.AddResource(uuid);
                    }

                    if (uuid != null)
                    {
                        subJSON.AddField("spriteFrame", uuid);
                    }
                    else
                    {
                        JSONObject nullJSON = new JSONObject(JSONObject.Type.NULL);
                        subJSON.AddField("spriteFrame", nullJSON);
                    }
                }
                else
                {
                    JSONObject nullJSON = new JSONObject(JSONObject.Type.NULL);
                    subJSON.AddField("spriteFrame", nullJSON);
                }

                subJSON.AddField("active", uiSprite.enabled);
                json.AddField("data", subJSON);

                return(json);
            }
            else
            {
                JSONObject json = new JSONObject(JSONObject.Type.OBJECT);
                json.AddField("type", "UISprite");

                JSONObject subJSON = new JSONObject(JSONObject.Type.OBJECT);

                JSONObject color = new JSONObject(JSONObject.Type.ARRAY);
                color.Add(255f);
                color.Add(255f);
                color.Add(255f);
                color.Add(255f);
                subJSON.AddField("color", color);
                subJSON.AddField("colorBlendType", 0);
                subJSON.AddField("type", (int)UI2DSprite.Type.Simple);
                subJSON.AddField("flip", (int)UI2DSprite.Flip.Nothing);
                subJSON.AddField("fillCenter", false);
                subJSON.AddField("fillDir", (int)UI2DSprite.FillDirection.Horizontal);
                subJSON.AddField("fillAmount", 0);
                subJSON.AddField("invertFill", false);

                JSONObject nullJSON = new JSONObject(JSONObject.Type.NULL);
                subJSON.AddField("spriteFrame", nullJSON);

                subJSON.AddField("active", true);
                json.AddField("data", subJSON);

                return(json);
            }
        }
Example #2
0
        protected override JSONObject ToJSON(WXHierarchyContext context)
        {
            JSONObject json = new JSONObject(JSONObject.Type.OBJECT);

            json.AddField("type", "UISprite");

            JSONObject subJSON = new JSONObject(JSONObject.Type.OBJECT);

            JSONObject color = new JSONObject(JSONObject.Type.ARRAY);

            color.Add(255f * uiTexture.color.r);
            color.Add(255f * uiTexture.color.g);
            color.Add(255f * uiTexture.color.b);
            color.Add(255f * uiTexture.color.a);
            subJSON.AddField("color", color);
            subJSON.AddField("colorBlendType", 0);

            UI2DSprite.Type type = uiTexture.type;
            subJSON.AddField("type", (int)type);

            UI2DSprite.Flip flipType = uiTexture.flip;
            subJSON.AddField("flip", (int)flipType);
            subJSON.AddField("fillCenter", uiTexture.centerType == UI2DSprite.AdvancedType.Sliced);
            subJSON.AddField("fillDir", (int)uiTexture.fillDirection);
            subJSON.AddField("fillAmount", uiTexture.fillAmount);
            subJSON.AddField("invertFill", uiTexture.invert);

            string texturePath = "";

            if (uiTexture.mainTexture != null)
            {
                Texture2D texture2D = (Texture2D)uiTexture.mainTexture;
                if (texture2D != null)
                {
                    string path = AssetDatabase.GetAssetPath(texture2D.GetInstanceID());

                    texturePath = new WXTexture(texture2D).Export(context.preset);// MaterialUtil.SaveTextureFile(texture2D);
                    context.AddResource(texturePath);

                    UISpriteData data = new UISpriteData();
                    data.width  = uiTexture.mainTexture.width;
                    data.height = uiTexture.mainTexture.height;

                    WXSpriteFrame spriteFrame = new WXSpriteFrame(data, texturePath, path);
                    string        key         = spriteFrame.Export(context.preset);

                    subJSON.AddField("spriteFrame", key);
                    context.AddResource(key);
                }
            }
            else
            {
                JSONObject nullJSON = new JSONObject(JSONObject.Type.NULL);
                subJSON.AddField("spriteFrame", nullJSON);
            }

            subJSON.AddField("active", uiTexture.enabled);

            json.AddField("data", subJSON);

            return(json);
        }