/// <summary> /// 收集Unit使用了的材质列表,包括光照贴图相关材质 /// </summary> public void CollectMaterials() { this.materials = new List <Material>(); if (this.ins.renderer != null && this.ins.renderer.sharedMaterial != null) { this.materials.Add(this.ins.renderer.sharedMaterial); } int count = this.lightmapPrototypes.Count; for (int i = 0; i < count; i++) { LightmapPrototype lightmapPrototype = this.lightmapPrototypes[i]; Renderer renderer = null; if (lightmapPrototype.rendererChildIndex > -1) { if (lightmapPrototype.rendererChildIndex < this.ins.transform.childCount) { renderer = this.ins.transform.GetChild(lightmapPrototype.rendererChildIndex).renderer; } if (renderer != null && renderer.sharedMaterial != null) { this.materials.Add(renderer.sharedMaterial); } } } }
/// <summary> /// 重命名unit游戏对象 /// </summary> public void Renamme() { if (!GameScene.isPlaying) { string[] array = this.prePath.Split(new char[] { '/' }); if (this.lightmapPrototypes.Count > 0 && this.ins.renderer != null) { this.ins.name = string.Concat(new object[] { array[array.Length - 1], ":LM{", this.lightmapPrototypes[0].scale, "} : Unit_", this.createID }); } else { this.ins.name = array[array.Length - 1] + ":Unit_" + this.createID; } int count = this.lightmapPrototypes.Count; for (int i = 0; i < count; i++) { LightmapPrototype lightmapPrototype = this.lightmapPrototypes[i]; Renderer renderer = null; if (lightmapPrototype.rendererChildIndex > -1) { if (lightmapPrototype.rendererChildIndex < this.ins.transform.childCount) { renderer = this.ins.transform.GetChild(lightmapPrototype.rendererChildIndex).renderer; } if (renderer != null) { string[] array2 = renderer.gameObject.name.Split(new char[] { ':' }); renderer.gameObject.name = string.Concat(new object[] { array2[0], ":LM{", lightmapPrototype.scale, "}" }); } } } } else { this.ins.name = "Unit_" + this.createID; } }
/// <summary> /// 销毁回收 /// </summary> public void Destroy() { if (GameScene.isPlaying) { if (this.waterData != null) { this.waterData.Release(); } if (this.terrain != null) { this.terrain.Destroy(); DelegateProxy.GameDestory(this.terrain); DelegateProxy.GameDestory(this.terrain.gameObject); this.terrain = null; } if (this.water != null) { this.water.Destroy(); DelegateProxy.GameDestory(this.water); DelegateProxy.GameDestory(this.water.gameObject); this.water = null; } } else { if (this.terrain != null) { DelegateProxy.DestroyObjectImmediate(this.terrain.gameObject); } if (this.water != null) { DelegateProxy.DestroyObjectImmediate(this.water.gameObject); } } //同时销毁tile上的(静态)unit if (this.units != null) { while (this.units.Count > 0) { GameObjectUnit gameObjectUnit = this.units[0]; //进行destory,放入cache待重用 if (this.scene.ContainUnit(gameObjectUnit)) { this.scene.RemoveUnit(gameObjectUnit, true, true); } else { //场景不包含的unit,销毁,并加入静态缓存待重用 gameObjectUnit.Destroy(); this.scene.RemoveEmptyUnit(gameObjectUnit); } } this.units.Clear(); this.units = null; this.unitsMap.Clear(); this.unitsMap = null; } this.left = null; this.right = null; this.top = null; this.bot = null; this.top_left = null; this.top_right = null; this.bot_left = null; this.bot_right = null; this.region = null; this.scene = null; this._lightmapPrototype = null; this.waterData = null; this.heights = new float[0, 0]; this.grids = new int[0, 0]; this.heights = null; this.grids = null; }
/// <summary> /// 初始化unit场景显示游戏对象 /// </summary> private void Initialize() { if (this.destroyed || this.willRemoved) //待销毁或移除,无法初始化 { return; } if (this.ins == null && this.pre != null) { //实例化gameObject if (!this.isStatic) //动态unit直接instantiate { this.ins = (DelegateProxy.Instantiate(this.pre) as GameObject); } else //静态unit,根据数据信息解析相关属性来构建 { this.ins = (this.unitParser.Instantiate(this.pre) as GameObject); } if (!GameScene.isPlaying) { this.type = UnitType.GetType(this.ins.layer); //根据layer获取unit的类型 } if (!this.isStatic && this.needSampleHeight) //非静态物体,计算unit位置点的地面高度给位置坐标y值,保证站立在地面上 { this.position.y = this.scene.SampleHeight(this.position, true); } this.ins.transform.position = this.position; //设定位置 if (this.components != null) //挂载component,问题components从哪儿获取?????// { int count = this.components.Count; for (int i = 0; i < count; i++) { this.ins.AddComponent(this.components[i]); } } if (this.isStatic) { this.ins.transform.rotation = this.rotation; this.ins.transform.localScale = this.localScale; if (LightmapSettings.lightmaps.Length > 0) //光照贴图属性赋值 { int count = this.lightmapPrototypes.Count; for (int j = 0; j < count; j++) { LightmapPrototype lightmapPrototype = this.lightmapPrototypes[j]; Renderer renderer = null; if (lightmapPrototype.rendererChildIndex == -1) { renderer = this.ins.renderer; } else if (lightmapPrototype.rendererChildIndex < this.ins.transform.childCount) { renderer = this.ins.transform.GetChild(lightmapPrototype.rendererChildIndex).renderer; } if (renderer != null) { renderer.lightmapIndex = lightmapPrototype.lightmapIndex; renderer.lightmapTilingOffset = lightmapPrototype.lightmapTilingOffset; } } } //收集所有Render列表 List <Renderer> list = new List <Renderer>(); if (this.ins.renderer != null) { list.Add(this.ins.renderer); } for (int k = 0; k < this.ins.transform.childCount; k++) { Renderer renderer2 = this.ins.transform.GetChild(k).renderer; if (renderer2 != null) { list.Add(renderer2); } } if (GameScene.isPlaying) //场景开始运行 { for (int l = 0; l < list.Count; l++) { for (int m = 0; m < list[l].materials.Length; m++) { Material material = list[l].materials[m]; if (material != null) { if (list[l].gameObject.layer == GameLayer.Layer_Ground) //实时场景运行时,只有地面接受阴影 { list[l].receiveShadows = true; } else { list[l].receiveShadows = false; } this.shaderName = material.shader.name; if (!this.scene.terrainConfig.enablePointLight) //根据是否启用点光源,使用不同的材质shader { if (this.shaderName == GameObjectUnit.diffuseShaderName || this.shaderName == GameObjectUnit.snailDiffusePointShaderName) { material.shader = GameObjectUnit.snailDiffuseShader; } if (this.shaderName == GameObjectUnit.diffuseCutoutShaderName || this.shaderName == GameObjectUnit.snailDiffusePointCutoutShaderName) { material.shader = GameObjectUnit.snailDiffuseCutoutShader; } } else { if (this.shaderName == GameObjectUnit.diffuseShaderName || this.shaderName == GameObjectUnit.snailDiffuseShaderName) { material.shader = GameObjectUnit.snailDiffusePointShader; } if (this.shaderName == GameObjectUnit.diffuseCutoutShaderName || this.shaderName == GameObjectUnit.snailDiffuseCutoutShaderName) { material.shader = GameObjectUnit.snailDiffusePointCutoutShader; } } } } } } else { //场景没有运行时,需要开启接受阴影和产生阴影,便于bake光照贴图 for (int n = 0; n < list.Count; n++) { list[n].receiveShadows = true; list[n].castShadows = true; for (int num = 0; num < list[n].sharedMaterials.Length; num++) { Material material2 = list[n].sharedMaterials[num]; if (this.scene.statisticMode) { Statistic.Push(material2, AssetType.Material); } if (material2 != null) { this.shaderName = material2.shader.name; if (this.shaderName == GameObjectUnit.snailDiffuseShaderName || this.shaderName == GameObjectUnit.snailDiffusePointShaderName) { material2.shader = GameObjectUnit.diffuseShader; } if (this.shaderName == GameObjectUnit.snailDiffuseCutoutShaderName || this.shaderName == GameObjectUnit.snailDiffusePointCutoutShaderName) { material2.shader = GameObjectUnit.diffuseCutoutShader; } } } } } } else if (this.createInsListener != null) { try { this.createInsListener(); //动态unit调用创建对象回调 } catch (Exception ex) { LogSystem.LogError(new object[] { "监听创建单位函数中出错!" + ex.ToString() }); } } this.Renamme(); if (!GameScene.isPlaying) //场景没有开始运行 { this.CollectMaterials(); this.AddMeshRenderColliders(); if (this.cullingFactor <= 0.01f) { this.cullingFactor = this.scene.terrainConfig.defautCullingFactor; } if (this.isStatic) //如果是静态unit,计算更新影响的阻塞tile列表 { this.ComputeTiles(); } } } else { this.lostAsset = true; if (GameScene.isEditor) { this.ins = GameObject.CreatePrimitive(PrimitiveType.Cube); this.ins.transform.position = this.position; this.ins.transform.rotation = this.rotation; this.ins.transform.localScale = new Vector3(2f, 2f, 2f); string[] array = this.prePath.Split(new char[] { '/' }); if (this.lightmapPrototypes.Count > 0 && this.ins.renderer != null) { this.ins.name = string.Concat(new object[] { array[array.Length - 1], ":LM{", this.lightmapPrototypes[0].scale, "} : Unit_", this.createID }); } else { this.ins.name = array[array.Length - 1] + ":Unit_" + this.createID; } } } this.PlayBornEffect(); //初始化完成,开始播放出生效果 }
/// <summary> /// 读取unit数据信息 /// </summary> /// <param name="br"></param> /// <param name="cID"></param> public void Read(BinaryReader br, int cID) { if (this.readed && this.dataLength > 0L) { br.BaseStream.Position += this.dataLength; return; } long num = 0L; this.createID = cID; long num2 = br.BaseStream.Position; long num3 = num2; this.prePath = br.ReadString(); num2 = br.BaseStream.Position; if (br.ReadInt32() == 10009) { this.type = br.ReadInt32(); if (!GameScene.isPlaying) { } this.unitParser = UnitType.GenUnitParser(this.type); //根据不同unit类型,使用对应的解析器解析数据 this.unitParser.unit = this; if (GameScene.isPlaying && this.type == UnitType.UnitType_Light && this.scene.lightDataLength > 0L) { br.BaseStream.Position += this.scene.lightDataLength; return; } if (this.type == UnitType.UnitType_Light) { num = br.BaseStream.Position; } this.unitParser.Read(br); } else { br.BaseStream.Position = num2; this.unitParser = new UnitParser(); } this.dependResCount = br.ReadInt32(); if (this.dependResCount != 0) { } for (int i = 0; i < this.dependResCount; i++) { br.ReadInt32(); } int num4 = br.ReadInt32(); this.lightmapPrototypes = new List <LightmapPrototype>(); for (int i = 0; i < num4; i++) { LightmapPrototype lightmapPrototype = new LightmapPrototype(); lightmapPrototype.rendererChildIndex = br.ReadInt32(); lightmapPrototype.lightmapIndex = br.ReadInt32(); num2 = br.BaseStream.Position; if (br.ReadInt32() == 10006) { lightmapPrototype.scale = br.ReadSingle(); } else { br.BaseStream.Position = num2; } lightmapPrototype.lightmapTilingOffset.x = br.ReadSingle(); lightmapPrototype.lightmapTilingOffset.y = br.ReadSingle(); lightmapPrototype.lightmapTilingOffset.z = br.ReadSingle(); lightmapPrototype.lightmapTilingOffset.w = br.ReadSingle(); this.lightmapPrototypes.Add(lightmapPrototype); } br.ReadSingle(); this.position = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); this.rotation = new Quaternion(br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); this.localScale = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); num2 = br.BaseStream.Position; if (br.ReadInt32() == 20001) { this.combineParentUnitID = br.ReadInt32(); bool flag = br.ReadBoolean(); if (flag) { if (this.combinUnitIDs == null) { this.combinUnitIDs = new List <int>(); } num4 = br.ReadInt32(); for (int i = 0; i < num4; i++) { this.combinUnitIDs.Add(br.ReadInt32()); } } } else { br.BaseStream.Position = num2; } this.cullingFactor = br.ReadSingle(); num2 = br.BaseStream.Position; if (br.ReadInt32() == 40001) { this.center = new Vector3(br.ReadSingle(), br.ReadSingle(), br.ReadSingle()); } else { this.center = this.position; br.BaseStream.Position = num2; br.ReadSingle(); br.ReadSingle(); br.ReadSingle(); } br.ReadSingle(); br.ReadSingle(); if (this.cullingFactor <= 0.01f) //太小,使用默认剔除因子 { this.cullingFactor = this.scene.terrainConfig.defautCullingFactor; } this.UpdateViewRange(); long num5 = br.BaseStream.Position; this.dataLength = num5 - num3; if (this.type == UnitType.UnitType_Light) { this.scene.lightDataLength = num5 - num; } this.readed = true; }