static void AddChidGameObjectData() { for (int i = 0; i < m_transList.Count; i++) { Renderer render = m_transList[i].GetComponent <Renderer>(); if (render != null) { string goPath = CalcGoChildName(m_transList[i].name, m_transList[i]); string goName = goPath.Substring(goPath.IndexOf("/") + 1); //string goName = selectionGameObjects[i].name; Vector3 position = m_transList[i].position; Vector3 rotation = m_transList[i].rotation.eulerAngles; Vector3 scale = m_transList[i].lossyScale; int lightmapIndex = render.lightmapIndex; Vector4 lightmapScaleOffset = render.lightmapScaleOffset; int realtimeLightmapIndex = render.realtimeLightmapIndex; Vector4 realtimeLightmapScaleOffset = render.realtimeLightmapScaleOffset; RenderersLightmapData goRenderersLightmapData = new RenderersLightmapData(); goRenderersLightmapData.m_name = goPath; goRenderersLightmapData.m_position = position; goRenderersLightmapData.m_rotation = rotation; goRenderersLightmapData.m_scale = scale; goRenderersLightmapData.m_lightmapIndex = lightmapIndex; goRenderersLightmapData.m_lightmapScaleOffset = lightmapScaleOffset; goRenderersLightmapData.m_realtimeLightmapIndex = realtimeLightmapIndex; goRenderersLightmapData.m_realtimeLightmapScaleOffset = realtimeLightmapScaleOffset; goRenderersLightmapData.ToString(); //goRenderersLightmapData.lightMapName = sceneLightMapData.m_lightMapFarName[lightmapIndex]; goRenderersLightmapData.lightMapName = sceneLightMapData.m_assetBundleName[lightmapIndex]; if (PrefabUtility.GetPrefabType(render.gameObject) == PrefabType.PrefabInstance) { UnityEngine.Object parentObject = EditorUtility.GetPrefabParent(render.gameObject); string assetPath = AssetDatabase.GetAssetPath(parentObject); //直接设置数据会更新到 Assets 文件 goRenderersLightmapData.perfabName = assetPath; } sceneLightMapData.m_gameObjectList.Add(goRenderersLightmapData); } } }
static void ExportLightMapMappingDataByPrefab() { #region 获取场景名 //获取当前打开场景(path) string currSceneName = EditorApplication.currentScene; //获取当前打开场景名称 currSceneName = currSceneName.Substring(currSceneName.LastIndexOf("/") + 1); currSceneName = currSceneName.Replace(".unity", ""); #endregion #region 数据获取 //RenderersLightmapDataAsset 存放目录 string resourcesPath = string.Format("Assets/Resources/PrefabLightmapData_{0}", currSceneName); if (!Directory.Exists(Application.dataPath.Replace("/Assets", "/") + resourcesPath)) { System.IO.Directory.CreateDirectory(resourcesPath); } Dictionary <string, GameObjectLightmapData> prefabPath2GoLmData = new Dictionary <string, GameObjectLightmapData>(); //获取当前场景中所有 MeshRenderer MeshRenderer[] meshRenderers = FindObjectsOfType <MeshRenderer>(); for (int i = 0; i < meshRenderers.Length; i++) { MeshRenderer meshRenderer = meshRenderers[i]; //游戏对象场景路径 string goPath = CalcGoChildName(meshRenderer.name, meshRenderer.transform); RenderersLightmapData goRenderersLightmapData = new RenderersLightmapData(); //RenderersLightmapData goRenderersLightmapData = ScriptableObject.CreateInstance<RenderersLightmapData>(); //场景路径信息 goRenderersLightmapData.m_name = goPath; string rldAssetBundleName = string.Format("RenderersLightmapDataAssetBundle_{0}_{1}_{2}.rld", currSceneName, lmType, goPath.Replace("/", "_")); //transform 信息 goRenderersLightmapData.m_position = meshRenderer.transform.position; goRenderersLightmapData.m_rotation = meshRenderer.transform.eulerAngles; goRenderersLightmapData.m_scale = meshRenderer.transform.lossyScale; //光照信息 goRenderersLightmapData.m_lightmapIndex = meshRenderer.lightmapIndex; goRenderersLightmapData.m_lightmapScaleOffset = meshRenderer.lightmapScaleOffset; //AssetBundle 信息 if (meshRenderer.lightmapIndex >= 0) { goRenderersLightmapData.lmAssetBundleName = lmAssetBundleNameArr[meshRenderer.lightmapIndex]; goRenderersLightmapData.lightMapName = assetNameLMColorArr[meshRenderer.lightmapIndex]; goRenderersLightmapData.dirLightMapName = assetNameLMDirArr[meshRenderer.lightmapIndex]; } //是否是预制体 if (PrefabUtility.GetPrefabType(meshRenderer.gameObject) == PrefabType.PrefabInstance) { UnityEngine.Object parentObject = EditorUtility.GetPrefabParent(meshRenderer.gameObject); string prefabAssetPath = AssetDatabase.GetAssetPath(parentObject); //直接设置数据会更新到 Assets 文件 goRenderersLightmapData.perfabName = prefabAssetPath; if (prefabPath2GoLmData.ContainsKey(prefabAssetPath)) { prefabPath2GoLmData[prefabAssetPath].renderersLightmapDataList.Add(goRenderersLightmapData); } else { GameObjectLightmapData gameObjectLightmapData = CreateInstance <GameObjectLightmapData>(); gameObjectLightmapData.lightType = lmType; gameObjectLightmapData.prefabName = prefabAssetPath; gameObjectLightmapData.renderersLightmapDataList = new List <RenderersLightmapData>(); gameObjectLightmapData.renderersLightmapDataList.Add(goRenderersLightmapData); prefabPath2GoLmData.Add(prefabAssetPath, gameObjectLightmapData); } } else { Debug.LogWarning(meshRenderer.gameObject + " 不是 Prefab! "); continue; } /* * //场景 路径 默认 * string assetResourcesPath = resourcesPath + "/" + goPath.Replace("/", "~") + ".asset"; * //创建 asset * AssetDatabase.CreateAsset(goRenderersLightmapData, assetResourcesPath); * //设置打包 assetBundleName * AssetImporter importer = AssetImporter.GetAtPath(assetResourcesPath); */ /* * 路径中能存在 [] * 如果一个 perfab 有多个 MeshRenderer 将打入一个 assetBundle 中 * perfab 1 - 1 assetBundle * perfab 1 - n MeshRenderer(RenderersLightmapData) n - 1 assetBundle */ //importer.assetBundleName = goRenderersLightmapData.perfabName;//string.Format("[{0}][{1}]", goRenderersLightmapData.perfabName, goPath);///<<<<<< Prefab Path } #endregion #region 1.Asset数据创建 2.assetBundle打包名设置 foreach (string prefabAssetPath in prefabPath2GoLmData.Keys) { //场景 路径 默认 string assetResourcesPath = resourcesPath + "/" + prefabAssetPath.Replace("/", "~") + ".asset"; GameObjectLightmapData gameObjectLightmapData = prefabPath2GoLmData[prefabAssetPath]; //创建 asset AssetDatabase.CreateAsset(gameObjectLightmapData, assetResourcesPath); //设置打包 assetBundleName AssetImporter importer = AssetImporter.GetAtPath(assetResourcesPath); //string assetBundleName = gameObjectLightmapData.prefabName.Substring(gameObjectLightmapData.prefabName.LastIndexOf("/") + 1); //currSceneName = currSceneName.Replace(".prefab", ""); importer.assetBundleName = gameObjectLightmapData.prefabName + "lmd"; } #endregion }