コード例 #1
0
        private void ToBundle(string path, EVariantType variantType)
        {
            var files = Directory.GetFiles(path + "/" + variantType.ToString());

            for (int i = 0; i < files.Length; i++)
            {
                string bundlePath = files[i];
                if (bundlePath.Contains(".meta"))
                {
                    continue;
                }
                AssetImporter importer = AssetImporter.GetAtPath(bundlePath);
                if (importer != null)
                {
                    importer.assetBundleName    = path.Substring(path.LastIndexOf('/') + 1);
                    importer.assetBundleVariant = variantType.ToString();
                }
            }
            string bundleFolder = m_bundlePath + "/Scene";

            if (!Directory.Exists(bundleFolder))
            {
                Directory.CreateDirectory(bundleFolder);
            }
            BuildPipeline.BuildAssetBundles(bundleFolder, BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget);
        }
コード例 #2
0
        private void ToGenerateVariant(GameObject root, EVariantType variantType)
        {
            m_config = new SceneConfig();
            //var root = GameObject.Find(m_rootName);
            string folderName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;

            if (System.IO.Directory.Exists(m_packageFolder + folderName + "/" + variantType.ToString()))
            {
                DelectDir(m_packageFolder + folderName + "/" + variantType.ToString());
            }
            Directory.CreateDirectory(m_packageFolder + folderName + "/" + variantType.ToString());
            MeshRenderer[]      meshRenders = root.GetComponentsInChildren <MeshRenderer>();
            List <MeshRenderer> renlist     = new List <MeshRenderer>();
            List <MeshFilter>   filList     = new List <MeshFilter>();

            for (int i = 0; i < meshRenders.Length; i++)
            {
                var meshRender = meshRenders[i];
                var meshFilter = meshRender.GetComponent <MeshFilter>();
                if (meshFilter == null)
                {
                    continue;
                }
                if (meshRender.sharedMaterials.Length <= 0 || meshRender.sharedMaterial == null)
                {
                    continue;
                }
                bool has = false;
                for (int j = 0; j < renlist.Count; j++)
                {
                    if (filList[j].mesh == meshFilter.sharedMesh && renlist[j].sharedMaterials.Length == meshRender.sharedMaterials.Length && renlist[i].sharedMaterial == meshRender.sharedMaterial)
                    {
                        has = true;
                        break;
                    }
                }
                if (has)
                {
                    continue;
                }
                var f = meshRender.GetComponent <MeshFilter>();
                if (f == null)
                {
                    continue;
                }
                renlist.Add(meshRender);
                filList.Add(f);;
                var obj = GameObject.Instantiate(meshRender.gameObject);
                var ani = obj.GetComponent <Animator>();
                if (ani != null && ani.runtimeAnimatorController == null)
                {
                    UnityEngine.Object.DestroyImmediate(ani);
                }
                var a = obj.GetComponent <Animation>();
                if (a != null && a.GetClipCount() <= 0)
                {
                    UnityEngine.Object.DestroyImmediate(a);
                }
                var prefab = PrefabUtility.CreateEmptyPrefab(m_packageFolder + folderName + "/" + variantType.ToString() + "/" + meshRender.gameObject.name + ".prefab");
                PrefabUtility.ReplacePrefab(obj, prefab);
                UnityEngine.Object.DestroyImmediate(obj);
            }
            var cloneRoot = Object.Instantiate(root);

            m_config.objs = new List <SceneObj>();
            TraverseChildNode(cloneRoot.transform);

            var rootPrefab = PrefabUtility.CreateEmptyPrefab(m_packageFolder + folderName + "/" + variantType.ToString() + "/root.prefab");

            PrefabUtility.ReplacePrefab(cloneRoot, rootPrefab);
            UnityEngine.Object.DestroyImmediate(cloneRoot);

            if (RenderSettings.fog)
            {
                m_config.fog                  = new SceneFog();
                m_config.fog.colorR           = RenderSettings.fogColor.r;
                m_config.fog.colorG           = RenderSettings.fogColor.g;
                m_config.fog.colorB           = RenderSettings.fogColor.b;
                m_config.fog.colorA           = RenderSettings.fogColor.a;
                m_config.fog.fogMode          = (int)RenderSettings.fogMode;
                m_config.fog.fogDensity       = RenderSettings.fogDensity;
                m_config.fog.fogStartDistance = RenderSettings.fogStartDistance;
                m_config.fog.fogEndDistance   = RenderSettings.fogEndDistance;
            }
            if (RenderSettings.skybox != null)
            {
                if (!RenderSettings.skybox.name.Contains("Default-"))
                {
                    AssetDatabase.CopyAsset(UnityEditor.AssetDatabase.GetAssetPath(RenderSettings.skybox), m_packageFolder + folderName + "/" + variantType.ToString() + "/" + m_config.skybox + ".mat");
                    m_config.skybox = RenderSettings.skybox.name;
                }
            }
            if (LightmapSettings.lightmaps != null)
            {
                m_config.lightmap       = new SceneLightmap();
                m_config.lightmap.mode  = (int)LightmapSettings.lightmapsMode;
                m_config.lightmap.datas = new List <SceneLightmapData>();
                for (int i = 0; i < LightmapSettings.lightmaps.Length; i++)
                {
                    Texture2D color     = LightmapSettings.lightmaps[i].lightmapColor;
                    Texture2D dir       = LightmapSettings.lightmaps[i].lightmapDir;
                    string    colorName = color != null?color.name.Trim() : null;

                    if (!string.IsNullOrEmpty(colorName))
                    {
                        AssetDatabase.CopyAsset(UnityEditor.AssetDatabase.GetAssetPath(color), m_packageFolder + folderName + "/" + variantType.ToString() + "/" + colorName + ".exr");
                    }
                    string dirName = dir != null?dir.name.Trim() : null;

                    if (string.IsNullOrEmpty(dirName))
                    {
                        AssetDatabase.CopyAsset(UnityEditor.AssetDatabase.GetAssetPath(dir), m_packageFolder + folderName + "/" + variantType.ToString() + "/" + dirName + ".exr");
                    }
                    m_config.lightmap.datas.Add(new SceneLightmapData()
                    {
                        color = colorName,
                        dir   = dirName
                    });
                }
            }
            var lightObj = GameObject.Find(m_lightName);

            if (lightObj != null)
            {
                m_config.lights = new List <SceneLight>();
                var lights = lightObj.GetComponentsInChildren <UnityEngine.Light>();
                for (int i = 0; i < lights.Length; i++)
                {
                    var light       = lights[i];
                    var lightConfig = new SceneLight();
                    lightConfig.colorR      = light.color.r;
                    lightConfig.colorG      = light.color.g;
                    lightConfig.colorB      = light.color.b;
                    lightConfig.colorA      = light.color.a;
                    lightConfig.eulerAngleX = light.transform.eulerAngles.x;
                    lightConfig.eulerAngleY = light.transform.eulerAngles.y;
                    lightConfig.eulerAngleZ = light.transform.eulerAngles.z;
                    lightConfig.positionX   = light.transform.position.x;
                    lightConfig.positionY   = light.transform.position.y;
                    lightConfig.positionZ   = light.transform.position.z;
                    lightConfig.intensity   = light.intensity;
                    lightConfig.type        = (int)light.type;
                    lightConfig.range       = light.range;
                    lightConfig.spotAngle   = light.spotAngle;
                    lightConfig.width       = light.areaSize.x;
                    lightConfig.height      = light.areaSize.y;
                    m_config.lights.Add(lightConfig);
                }
            }
            byte[] bytes = Serialize <SceneConfig>(m_config);
            using (FileStream stream = new FileStream(m_packageFolder + folderName + "/" + variantType.ToString() + "/config.bytes", FileMode.Create, FileAccess.Write))
            {
                stream.Write(bytes, 0, bytes.Length);
            }
            AssetDatabase.Refresh();
            ToBundle(m_packageFolder + folderName, variantType);
        }