コード例 #1
0
        protected override JSONObject ToJSON(WXHierarchyContext context)
        {
            JSONObject json = new JSONObject(JSONObject.Type.OBJECT);
            JSONObject data = new JSONObject(JSONObject.Type.OBJECT);

            json.AddField("type", getTypeName());
            json.AddField("data", data);
            data.AddField("active", true);

            data.AddField("ref", context.AddComponent(new WXUIToggle(uiToggle, go, entity), uiToggle));

            if (uiToggle.group != null)
            {
                data.AddField("group", (string)uiToggle.group);
            }


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

            data.AddField("onChange", onChange);


            foreach (var change in uiToggle.onChange)
            {
                var __onChange = new JSONObject(JSONObject.Type.OBJECT);
                __onChange.AddField("target", context.AddComponent(new WXEngineMonoBehaviour(change.target), change.target));
                __onChange.AddField("method", change.methodName);

                var _params = new JSONObject(JSONObject.Type.ARRAY);
                __onChange.AddField("params", _params);
                if (change.parameters != null)
                {
                    foreach (var p in change.parameters)
                    {
                        if (p.obj != null)
                        {
                            _params.Add(WXMonoBehaviourPropertiesHandler.InvokePropertyHandler(p.expectedType, p.obj, context));
                        }
                    }
                }

                onChange.Add(__onChange);
            }


            return(json);
        }
コード例 #2
0
        protected override JSONObject ToJSON(WXHierarchyContext context)
        {
            JSONObject json = new JSONObject(JSONObject.Type.OBJECT);
            JSONObject data = new JSONObject(JSONObject.Type.OBJECT);

            json.AddField("type", getTypeName());
            json.AddField("data", data);
            data.AddField("active", true);

            data.AddField("ref", context.AddComponent(new WXUIButton(uiButton, go, entity), uiButton));

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

            data.AddField("onClick", onClick);

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

            colorPressed.Add(255f * uiButton.pressed.r);
            colorPressed.Add(255f * uiButton.pressed.g);
            colorPressed.Add(255f * uiButton.pressed.b);
            colorPressed.Add(255f * uiButton.pressed.a);
            data.AddField("pressedColor", colorPressed);

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

            colorDisabledColor.Add(255f * uiButton.disabledColor.r);
            colorDisabledColor.Add(255f * uiButton.disabledColor.g);
            colorDisabledColor.Add(255f * uiButton.disabledColor.b);
            colorDisabledColor.Add(255f * uiButton.disabledColor.a);
            data.AddField("disabledColor", colorDisabledColor);

            if (uiButton.tweenTarget != null)
            {
                JSONObject colorNormal = new JSONObject(JSONObject.Type.ARRAY);
                var        mWidget     = uiButton.tweenTarget.GetComponent <UIWidget>();
                colorNormal.Add(255f * mWidget.color.r);
                colorNormal.Add(255f * mWidget.color.g);
                colorNormal.Add(255f * mWidget.color.b);
                colorNormal.Add(255f * mWidget.color.a);
                data.AddField("normalColor", colorNormal);
            }

            // onClick
            // onClick.AddField("");
            foreach (var click in uiButton.onClick)
            {
                var __onClick = new JSONObject(JSONObject.Type.OBJECT);
                // var target = new JSONObject(JSONObject.Type.OBJECT);
                // target.AddField(click.target.GetType().FullName.EscapeNamespaceSimple(), context.AddComponent(new WXEngineMonoBehaviour(click.target), click.target));
                // onClick.AddField("target", WXMonoBehaviourPropertiesHandler.InvokePropertyHandler(typeof(UnityEngine.MonoBehaviour), click.target, context));
                __onClick.AddField("target", context.AddComponent(new WXEngineMonoBehaviour(click.target), click.target));
                __onClick.AddField("method", click.methodName);

                var _params = new JSONObject(JSONObject.Type.ARRAY);
                __onClick.AddField("params", _params);
                if (click.parameters != null)
                {
                    foreach (var p in click.parameters)
                    {
                        if (p.obj != null)
                        {
                            _params.Add(WXMonoBehaviourPropertiesHandler.InvokePropertyHandler(p.expectedType, p.obj, context));
                        }
                    }
                }

                onClick.Add(__onClick);
            }

            return(json);
        }
コード例 #3
0
        protected override JSONObject ToJSON(WXHierarchyContext context)
        {
            JSONObject json = new JSONObject(JSONObject.Type.OBJECT);
            JSONObject data = new JSONObject(JSONObject.Type.OBJECT);

            json.AddField("type", getTypeName());
            json.AddField("data", data);

            data.AddField("active", true);


            // ngui prefab don't need binding script
            if (this.behaviour != null /* && !WXBridge.isNGUIPreset */)
            {
                var    script = MonoScript.FromMonoBehaviour(this.behaviour);
                string path   = new WXEngineScript(script).Export(context.preset);

                context.AddResource(path);
                data.AddField("__uuid", path);
            }



            if (this.behaviour != null)
            {
                Type        myObjectType = behaviour.GetType();
                FieldInfo[] fields       = myObjectType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);

                foreach (FieldInfo field in fields)
                {
                    if (!field.IsDefined(typeof(SerializeField)) && !field.IsPublic)
                    {
                        continue;
                    }
                    WXMonoBehaviourPropertiesHandler.HandleProperty(field, behaviour, data, context);
                }
            }


            // clear {}
            json.GetField("data", (JSONObject _data) => {
                var shouldBeRemoved = new List <string>();
                foreach (var _key in _data.keys)
                {
                    // Debug.Log(_type);
                    // Debug.Log(_data[_type] + " == " + _data[_type].GetType().ToString());
                    var _value = _data[_key].ToString();
                    // Debug.Log("key:"+_key+" value:"+_value);

                    if (_data[_key].IsNull || _value == "{}")
                    {
                        shouldBeRemoved.Add(_key);
                        // _data.RemoveField(_key);
                        // Debug.Log("remove: " + _key);
                    }
                }

                foreach (var k in shouldBeRemoved)
                {
                    _data.RemoveField(k);
                }
            });
            return(json);
        }
        protected override JSONObject ToJSON(WXHierarchyContext context)
        {
            JSONObject json = new JSONObject(JSONObject.Type.OBJECT);
            JSONObject data = new JSONObject(JSONObject.Type.OBJECT);

            json.AddField("type", behaviour.GetType().FullName);
            json.AddField("data", data);

            Dictionary <string, Type> bindingFields  = new Dictionary <string, Type>();
            List <string>             bindingMethods = new List <string>();
            List <string>             requireList    = new List <string>();

            if (this.behaviour != null)
            {
                BindingFlags flags        = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public;
                Type         myObjectType = behaviour.GetType();
                FieldInfo[]  fields       = myObjectType.GetFields(flags);

                // if (behaviour.GetType().Name == typeof(PuertsTest.TankMovement).Name) {
                //     var fieldss = behaviour.GetType().GetFields(BindingFlags.Static | BindingFlags.Public);
                //     Debug.Log(fieldss.Length);
                //     foreach (FieldInfo item in fieldss) {
                //         Debug.Log(item.Name);
                //     }
                // }
                foreach (FieldInfo field in fields)
                {
                    if (
                        // 排除Action
                        field.FieldType.BaseType != typeof(System.MulticastDelegate) &&

                        !field.IsDefined(typeof(NonSerializedAttribute), true) &&

                        // 排除hideInInspector
                        !field.IsDefined(typeof(HideInInspector), true)
                        )
                    {
                        JSONObject result = WXMonoBehaviourPropertiesHandler.HandleField(field, behaviour, context, requireList);

                        if (result != null)
                        {
                            data.AddField(field.Name, result);
                            bindingFields.Add(field.Name, field.FieldType);
                        }
                    }
                }
                MethodInfo[] methods = myObjectType.GetMethods(flags);
                foreach (MethodInfo method in methods)
                {
                    bindingMethods.Add(method.Name);
                }
            }

            string script = new WXEngineScriptResource(
                new PuertsSerializableScriptFile(
                    behaviour.GetType().FullName,
                    bindingFields,
                    bindingMethods,
                    behaviour.JSClassName,
                    requireList
                    )
                ).Export(context.preset);

            context.AddResource(script);
            data.AddField("__uuid", script);
            data.AddField("active", true);

            return(json);
        }