Beispiel #1
0
        public static EntityData SerializeEntity(GameObject obj)
        {
            //未激活的不导出
            if ((!ExportSetting.instance.common.exportUnactivatedObject && !obj.activeInHierarchy))
            {
                MyLog.Log(obj.name + "对象未激活");
                return(null);
            }

            if (obj.GetComponent <RectTransform>() != null)
            {
                return(null);
            }
            MyLog.Log("对象:" + obj.name);
            var entityData = currentData.CreateEntity();
            var components = obj.GetComponents <Component>();

            var index = 0;//TODO

            foreach (var comp in components)
            {
                if (comp is Animator)
                {
                    components[index] = components[0];
                    components[0]     = comp;
                }

                index++;
            }

            foreach (var comp in components)
            {
                if (comp == null)
                {
                    MyLog.LogWarning("空的组件");
                    continue;
                }
                string compClass = comp.GetType().Name;
                // MyLog.Log("组件:" + compClass);
                if (!ExportSetting.instance.common.exportUnactivatedComp)
                {
                    //利用反射查看组件是否激活,某些组件的enabled不再继承链上,只能用反射,比如BoxCollider
                    var property = comp.GetType().GetProperty("enabled");
                    if (property != null && !((bool)property.GetValue(comp, null)))
                    {
                        MyLog.Log(obj.name + "组件未激活");
                        continue;
                    }
                }

                if (!SerializeObject.IsComponentSupport(compClass))
                {
                    MyLog.LogWarning("不支持的组件: " + compClass);
                    continue;
                }
                var compData = SerializeObject.SerializeComponent(obj, compClass, comp, entityData);
                if (compData != null)
                {
                    entityData.AddComponent(compData);
                    MyLog.Log("--导出组件:" + compClass);
                }
                else
                {
                    MyLog.LogWarning("组件: " + compClass + " 导出失败");
                }
            }

            return(entityData);
        }