public static GameObject CreateObject(string prefabName, string id) { GameObject obj = PoolObjectManager.GetObject(prefabName); if (!itemDic.ContainsKey(id)) { itemDic.Add(id, obj); } return(obj); }
public static GameObject Create(string prefabName, out RenderTexture renderTexture, int renderTextureWidth = 1024, int renderTextureHeight = 1024) { GameObject temp0 = new GameObject("UIModelShow"); GameObject temp1 = new GameObject("Camera"); temp1.transform.SetParent(temp0.transform); temp1.transform.localPosition = new Vector3(0, 5000, 0); Camera ca = temp1.AddComponent <Camera>(); ca.clearFlags = CameraClearFlags.SolidColor; ca.backgroundColor = new Color(0, 0, 0, 5 / 255f); ca.orthographic = true; ca.orthographicSize = 0.68f; ca.depth = 100; ca.cullingMask = 1 << LayerMask.NameToLayer("UI"); GameObject root = new GameObject("Root"); root.transform.SetParent(temp1.transform); root.transform.localPosition = new Vector3(0, 0, 100); root.transform.eulerAngles = new Vector3(0, 180, 0); GameObject obj = PoolObjectManager.GetObject(prefabName); obj.transform.SetParent(root.transform); obj.transform.localPosition = new Vector3(0, 0, 0); obj.transform.localEulerAngles = Vector3.zero; Transform[] trans = obj.GetComponentsInChildren <Transform>(); for (int i = 0; i < trans.Length; i++) { trans[i].gameObject.layer = LayerMask.NameToLayer("UI"); } SkinnedMeshRenderer[] mes = obj.GetComponentsInChildren <SkinnedMeshRenderer>(); for (int i = 0; i < mes.Length; i++) { mes[i].shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; mes[i].receiveShadows = false; } renderTexture = new RenderTexture(renderTextureWidth, renderTextureHeight, 100); renderTexture.autoGenerateMips = false; renderTexture.anisoLevel = 1; // tex.antiAliasing = 2 ca.targetTexture = renderTexture; return(obj); }
/// <summary> /// 生成场景物体 /// </summary> /// <param name="id"></param> public static void CreateMapGameObjectInScene(int id) { List <GameObject> listObjs = new List <GameObject>(); LightMapObject lightMapObject; MapWorld mapWorld = GetMapObject(id, out lightMapObject); if (mapWorld == null) { mapWorld = new MapWorld(); } List <MapObject> mapObjs = mapWorld.mapObjects; if (mapRoot == null) { mapRoot = new GameObject(MapUseConstValues.MapRootObjectName); } Transform idRoot = mapRoot.transform.Find(id.ToString()); if (idRoot == null) { idRoot = new GameObject(id.ToString()).transform; idRoot.SetParent(mapRoot.transform); } if (mapObjs == null) { mapObjs = new List <MapObject>(); return; } GameObject mapObj = new GameObject(MapUseConstValues.MapObjectsObjectName); mapObj.transform.SetParent(idRoot); string lightPrefabName = MapUseConstValues.LightPrefabNamePrefix + MapInfosDic[id].mapFileName; GameObject mapLights = null; mapLights = PoolObjectManager.GetObject(lightPrefabName); if (mapLights == null) { mapLights = new GameObject(lightPrefabName); } mapLights.transform.SetParent(idRoot); string colliderPrefabName = MapUseConstValues.ColliderPrefabNamePrefix + MapInfosDic[id].mapFileName; GameObject mapColliderObj = null; mapColliderObj = PoolObjectManager.GetObject(colliderPrefabName); if (mapColliderObj == null) { mapColliderObj = new GameObject(colliderPrefabName); } mapColliderObj.transform.SetParent(idRoot); if (lightMapObject != null) { LightmapSettings.lightmapsMode = lightMapObject.lightmapsMode; RenderSettings.fog = lightMapObject.fogInfo.fog; RenderSettings.fogMode = lightMapObject.fogInfo.fogMode; RenderSettings.fogColor = lightMapObject.fogInfo.fogColor; RenderSettings.fogStartDistance = lightMapObject.fogInfo.fogStartDistance; RenderSettings.fogEndDistance = lightMapObject.fogInfo.fogEndDistance; RenderSettings.fogDensity = lightMapObject.fogInfo.fogDensity; List <Texture2D> lightmapDir = new List <Texture2D>(); for (int i = 0; i < lightMapObject.lightmapDir.Count; i++) { string name = lightMapObject.lightmapDir[i]; Texture2D tex = ResourcesManager.LoadUnityAssetByName <Texture2D>(name); lightmapDir.Add(tex); } List <Texture2D> lightmapColor = new List <Texture2D>(); for (int i = 0; i < lightMapObject.lightmapColor.Count; i++) { string name = lightMapObject.lightmapColor[i]; Texture2D tex = ResourcesManager.LoadUnityAssetByName <Texture2D>(name); lightmapColor.Add(tex); } LightmapData[] lightmapData = new LightmapData[lightmapDir.Count > lightmapColor.Count ? lightmapDir.Count : lightmapColor.Count]; for (int i = 0; i < lightmapData.Length; i++) { lightmapData[i] = new LightmapData(); lightmapData[i].lightmapColor = i < lightmapColor.Count ? lightmapColor[i] : null; lightmapData[i].lightmapDir = i < lightmapDir.Count ? lightmapDir[i] : null; } LightmapSettings.lightmaps = lightmapData; } for (int i = 0; i < mapObjs.Count; i++) { MapObject mp = mapObjs[i]; GameObject obj = PoolObjectManager.GetObject(mp.name); obj.isStatic = mp.isStatic; obj.transform.SetParent(mapObj.transform); obj.transform.localPosition = mp.transformInfo.position; obj.transform.localRotation = Quaternion.Euler(mp.transformInfo.rotation); obj.transform.localScale = mp.transformInfo.scale; for (int j = 0; j < mp.behavirComponets.Count; j++) { ClassValue cv = mp.behavirComponets[j]; cv.GetValue((type) => { object v = obj.GetComponent(type); if (v == null) { v = obj.AddComponent(type); } return(v); }); } listObjs.Add(obj); if (lightMapObject != null && obj.isStatic) { MapObjectRendererLightMapInfo renderInfo = lightMapObject.mapObjectRendererLightMapInfo[i]; MeshRenderer[] msR = obj.GetComponentsInChildren <MeshRenderer>(); for (int j = 0; j < msR.Length; j++) { RendererLightMapInfo r = renderInfo.renderLightMapInfos[j]; msR[j].lightmapIndex = r.lightmapIndex; msR[j].lightmapScaleOffset = r.lightmapScaleOffset; } } } if (lightMapObject != null) { Dictionary <string, string> reflectionProbesInfoDic = new Dictionary <string, string>(); for (int i = 0; i < lightMapObject.reflectionProbeInfos.Count; i++) { ReflectionProbeInfo info = lightMapObject.reflectionProbeInfos[i]; reflectionProbesInfoDic.Add(info.gameObjectName, info.reflectionProbeFileName); } ReflectionProbe[] reflectionProbes = mapLights.GetComponentsInChildren <ReflectionProbe>(); for (int i = 0; i < reflectionProbes.Length; i++) { ReflectionProbe re = reflectionProbes[i]; string name = reflectionProbesInfoDic[re.gameObject.name]; Texture tex = ResourcesManager.LoadUnityAssetByName <Texture>(name); re.bakedTexture = tex; } } if (!mapInstanceBuffer.ContainsKey(id)) { mapInstanceBuffer.Add(id, listObjs); } }