示例#1
0
        public static void ExportScene(List <GameObject> roots, string exportPath = "")
        {
            string sceneName = PathHelper.CurSceneName;

            //导出场景
            try
            {
                ExportImageTools.instance.Clear();
                ResourceManager.instance.Clean();
                //路径
                string scenePath = sceneName + ".scene.json";
                PathHelper.SetSceneOrPrefabPath(scenePath);
                //Scene
                var           scene     = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene();
                MyJson_Object sceneJson = new MyJson_Object();
                sceneJson.SetUUID(scene.GetHashCode().ToString());//用场景名称的hashCode
                sceneJson.SetUnityID(scene.GetHashCode());
                sceneJson.SetClass("paper.Scene");
                sceneJson.SetString("name", sceneName.Substring(sceneName.LastIndexOf('/') + 1));

                sceneJson.SetColor("ambientColor", RenderSettings.ambientLight);
                sceneJson.SetNumber("lightmapIntensity", UnityEditor.Lightmapping.indirectOutputScale);
                //allGameObjects
                var gameObjectsJson = new MyJson_Array();
                sceneJson["gameObjects"] = gameObjectsJson;
                GameObject[] allObjs = GameObject.FindObjectsOfType <GameObject>();
                for (int i = 0; i < allObjs.Length; i++)
                {
                    gameObjectsJson.AddHashCode(allObjs[i]);
                }
                //lightmaps
                sceneJson["lightmaps"] = ExportSceneTools.AddLightmaps(exportPath);
                ResourceManager.instance.AddObjectJson(sceneJson);
                //序列化
                foreach (var root in roots)
                {
                    SerializeObject.Serialize(root);
                }
                ResourceManager.instance.ExportFiles(scenePath, exportPath);
                MyLog.Log("----场景导出成功----");
            }
            catch (System.Exception e)
            {
                MyLog.LogError(sceneName + "  : 导出失败-----------" + e.StackTrace);
            }
        }
示例#2
0
        public override bool WriteToJson(GameObject obj, Component component, MyJson_Object compJson)
        {
            Transform comp = component as Transform;

            Vector3    localPosition = comp.localPosition;
            Quaternion localRotation = comp.localRotation;
            Vector3    localScale    = comp.localScale;

            //这里特殊处理一下,拥有SkinnedMeshRenderer组件的Transform属性清零,因为Egret3D实现不同,如果不清零,会影响动画

            /*if (obj.GetComponent<SkinnedMeshRenderer>() != null)
             * {
             *  localPosition = Vector3.zero;
             *  localRotation = Quaternion.identity;
             *  localScale = Vector3.one;
             * }*/
            //localPosition
            compJson.SetVector3("localPosition", localPosition);
            //localRotation
            compJson.SetQuaternion("localRotation", localRotation);
            //localScale
            compJson.SetVector3("localScale", localScale);
            if ((component as Transform).parent)
            {
                compJson.SetHashCode("_parent", comp.parent);
            }
            var childrenItem = new MyJson_Array();

            compJson["children"] = childrenItem;
            for (int i = 0; i < comp.childCount; i++)
            {
                var child = comp.GetChild(i);
                if (child.gameObject.activeInHierarchy)
                {
                    childrenItem.AddHashCode(child);
                }
            }

            return(true);
        }
        public static void Serialize(GameObject obj)
        {
            //未激活的不导出
            if ((!ExportToolsSetting.instance.exportUnactivatedObject && !obj.activeInHierarchy))
            {
                MyLog.Log(obj.name + "对象未激活");
                return;
            }

            if (obj.GetComponent <RectTransform>() != null)
            {
                return;
            }
            MyLog.Log("导出对象:" + obj.name);
            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);
                }
            }
        }