Beispiel #1
0
        public static UnityScene ExportScene(Scene scene)
        {
            UnityScene unitScene = new UnityScene();
            var        list      = scene.GetRootGameObjects();

            foreach (var obj in list)
            {
                UnityPerfabData data = Serialize(obj, unitScene);
                if (null != data)
                {
                    unitScene.perfabDatas.Add(data);
                }
            }
            return(unitScene);
        }
Beispiel #2
0
        public static UnityPerfabData Serialize(GameObject obj, UnityScene unitScene = null)
        {
            if (null == unitScene)
            {
                unitScene = new UnityScene();
            }

            unitScene.current = null;

            if (!obj.activeInHierarchy)
            {
                //对象未激活 不参与导出
                return(null);
            }

            UnityPerfabData data    = null;
            string          refName = null;

            if (obj.GetComponent <RectTransform>() != null)
            {
                return(null);
            }

            Debug.Log("导出对象:" + obj.name);

            //判断是否是Camera
            var camera = obj.GetComponent <Camera>();

            if (null != camera)
            {
                ExecComponentParser(obj, camera, unitScene);
                return(null);
            }

            var light = obj.GetComponent <Light>();

            if (null != light)
            {
                ExecComponentParser(obj, light, unitScene);
                return(null);
            }

            //prefab
            var prefabType = PrefabUtility.GetPrefabAssetType(obj);

            if (prefabType != PrefabAssetType.NotAPrefab)
            {
                var parentObj = PrefabUtility.GetCorrespondingObjectFromSource(obj);
                refName = AssetDatabase.GetAssetPath(parentObj);
                if (unitScene.refPerfabDatas.ContainsKey(refName))
                {
                    return(unitScene.refPerfabDatas[refName]);
                }
            }



            var animator = obj.GetComponent <Animator>();

            if (null != animator)
            {
                data = ExecComponentParser(obj, animator, unitScene) as UnityPerfabData;
                if (null != data)
                {
                    return(data);
                }
            }


            var meshRenderer = obj.GetComponent <MeshRenderer>();

            if (null != meshRenderer)
            {
                data = ExecComponentParser(obj, animator, unitScene) as UnityPerfabData;
            }
            else
            {
                var skinnedMeshRenderer = obj.GetComponent <SkinnedMeshRenderer>();
                if (null != skinnedMeshRenderer)
                {
                    data = ExecComponentParser(obj, animator, unitScene) as UnityPerfabData;
                }
                else
                {
                    data = new UnityPerfabData(obj);
                }
            }

            // var particle = obj.GetComponent<ParticleSystem>();
            //todo particle


            UnityRefPerfabData tempData = unitScene.current;

            Transform transform = obj.transform;

            int count = transform.childCount;

            for (int i = 0; i < count; i++)
            {
                var temp = Serialize(transform.GetChild(i).gameObject, unitScene);
                if (null != temp)
                {
                    data.childrens.Add(temp);
                }
            }

            unitScene.current = tempData;


            return(data);

            // MyJson_Object item = new MyJson_Object();
            // item.SetUUID(obj.GetInstanceID().ToString());
            // item.SetUnityID(obj.GetInstanceID());
            // item.SetClass("paper.GameObject");
            // item.SetString("name", obj.name);
            // item.SetString("tag", obj.tag);
            // var layerMask = 1 << obj.layer;
            // item.SetInt("layer", layerMask);
            // // item.SetInt("layer", LAYER[obj.layer >= LAYER.Length ? 0 : obj.layer]);;
            // item.SetBool("isStatic", obj.isStatic);

            // var componentsItem = new MyJson_Array();
            // item["components"] = componentsItem;
            // ResourceManager.instance.AddObjectJson(item);

            // 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 (!ExportToolsSetting.instance.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;
            //     }
            //     if (SerializeObject.SerializedComponent(obj, compClass, comp))
            //     {
            //         MyLog.Log("--导出组件:" + compClass);
            //         componentsItem.AddHashCode(comp);
            //     }
            //     else
            //     {
            //         MyLog.LogWarning("组件: " + compClass + " 导出失败");
            //     }
            // }
            // //遍历子对象
            // if (obj.transform.childCount > 0)
            // {
            //     for (int i = 0; i < obj.transform.childCount; i++)
            //     {
            //         var child = obj.transform.GetChild(i).gameObject;
            //         Serialize(child);
            //     }
            // }
        }