Beispiel #1
0
        // handle all Serializable class recursively
        private static void SerializableHandler(Type _type, JSONObject _data, object value, WXHierarchyContext context, List <string> requireList)
        {
            if (value == null || _type == null)
            {
                return;
            }
            // get [SerializeField] && Public properties
            var fields = _type.GetFields(BindingFlags.NonPublic |
                                         BindingFlags.Instance |
                                         BindingFlags.Public).Where(f =>
                                                                    !f.IsDefined(typeof(NonSerializedAttribute), true) &&
                                                                    !f.IsDefined(typeof(HideInInspector), true) &&
                                                                    (f.IsDefined(typeof(SerializeField), true) || f.IsPublic));

            Dictionary <string, Type> fieldsDict = new Dictionary <string, Type>();

            foreach (var f in fields)
            {
                object itemValue = f.GetValue(value);
                // get non-permitive Serializable object && non-IEnumerable object
                Type fType = f.FieldType;
                if (fType == null)
                {
                    continue;
                }

                _data.AddField(f.Name, innerHandleField(fType, itemValue, context, requireList));
                fieldsDict.Add(f.Name, fType);
            }

            // 为serializable生成一个script
            PuertsSerializableScriptFile scriptFile = new PuertsSerializableScriptFile(
                _type.FullName,
                fieldsDict,
                new List <string>()
                );
            string scriptPath = new WXEngineScriptResource(scriptFile).Export(context.preset);

            context.AddResource(scriptPath);
            requireList.Add(scriptPath);
        }
        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);
        }