今天优化了下昨天的代码,并且添加了树木和其他资源的生成。还修复了接近石头后,挖掘图标不出现的bug。目前可以在unity中稳定60-70fps。
详看文章:https://www.cnblogs.com/lixiaoyao123/p/9710601.html
更新后的代码:(还是比较乱 没整理也没写注释,最后一块说明)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class StartMap : MonoBehaviour { private GameObject Bridge; private GameObject Cliff; private GameObject Dirt; private GameObject Grass1; private GameObject Grass2; private GameObject Rock; private GameObject Sand; private GameObject Tree; private GameObject Grass01; private GameObject Grass02; private GameObject Props_Rock; private Vector3 location; private float xpos, ypos, zpos; private int randnum; private GameObject selectedobject; private GameObject selectOther; private bool iscompeleted = false; public GameObject envfather; // Use this for initialization private void Start() { Time.timeScale = 0; createmap(); } private void Update() { if(iscompeleted) { DeletePrefab(); Time.timeScale = 1; Destroy(this.gameObject); } } void DeletePrefab() { Bridge.transform.position = new Vector3(0, -1000, 0); Cliff.transform.position = new Vector3(0, -1000, 0); Dirt.transform.position = new Vector3(0, -1000, 0); Grass1.transform.position = new Vector3(0, -1000, 0); Grass2.transform.position = new Vector3(0, -1000, 0); Rock.transform.position = new Vector3(0, -1000, 0); Sand.transform.position = new Vector3(0, -1000, 0); Grass01.transform.position = new Vector3(0, -1000, 0); Grass02.transform.position = new Vector3(0, -1000, 0); Tree.transform.position = new Vector3(0, -1000, 0); } void RenewPrefab() { Bridge = (GameObject)Instantiate(Resources.Load("Prefabs/Bridge")); Cliff = (GameObject)Instantiate(Resources.Load("Prefabs/Cliff")); Dirt = (GameObject)Instantiate(Resources.Load("Prefabs/Dirt")); Grass1 = (GameObject)Instantiate(Resources.Load("Prefabs/Grass1")); Grass2 = (GameObject)Instantiate(Resources.Load("Prefabs/Grass2")); Rock = (GameObject)Instantiate(Resources.Load("Prefabs/Rock")); Sand = (GameObject)Instantiate(Resources.Load("Prefabs/Sand")); Tree = (GameObject)Instantiate(Resources.Load("Prefabs/Tree_01")); Grass01 = (GameObject)Instantiate(Resources.Load("Prefabs/Grass_01")); Grass02 = (GameObject)Instantiate(Resources.Load("Prefabs/Grass_02")); Props_Rock = (GameObject)Instantiate(Resources.Load("Prefabs/Props_Rock")); DeletePrefab(); } void createmap() { for (xpos = -25; xpos <= 25; xpos++) { for(zpos = -130; zpos <= 0; zpos++) { RenewPrefab(); randnum = Random.Range(0, 100); SelectPrefab(randnum); selectedobject.transform.position = new Vector3(xpos, ypos, zpos); selectedobject.transform.parent = envfather.transform; CreateOthers(selectedobject); if(xpos == 25 && zpos == 0) { DeletePrefab(); iscompeleted = true; break; } } } } private void SelectPrefab( int rand) { if (0 <= rand & rand < 20) { selectedobject = Cliff; } else if(20 <= rand & rand < 40) { selectedobject = Dirt; } else if (40 <= rand & rand < 60) { selectedobject = Grass1; } else if (60 <= rand & rand < 80) { selectedobject = Grass2; } else if (80 <= rand & rand < 90) { selectedobject = Rock; } else { selectedobject = Sand; } } void CreateOthers(GameObject selectObject) { int i = Random.Range(1, 10); if(selectObject==Grass1) { if (i > 5) { selectOther = Tree; selectOther.transform.position = selectObject.transform.position; if (i > 5 && i <=6) { selectOther.transform.eulerAngles = new Vector3(0, 90, 0); } else if (i >6 && i <= 7) { selectOther.transform.eulerAngles = new Vector3(0, 180, 0); } else if (i > 7 && i <= 8) { selectOther.transform.eulerAngles = new Vector3(0, 270, 0); } else if (i > 8 && i <= 10) { selectOther.transform.eulerAngles = new Vector3(0, -90, 0); } selectOther.transform.parent = selectObject.transform; } } else if(selectObject == Grass2) { if (5<i&&i<7) { selectOther = Grass01; selectOther.transform.position = selectObject.transform.position; selectOther.transform.parent = selectObject.transform; } else if(i>=7&&i<10) { selectOther = Grass02; selectOther.transform.position = selectObject.transform.position; selectOther.transform.parent = selectObject.transform; } } else if(selectObject == Dirt) { if(i>6) { selectOther = Grass02; selectOther.transform.position = selectObject.transform.position; selectOther.transform.parent = selectObject.transform; } } else if(selectObject == Rock) { if (i >=4) { selectOther = Props_Rock; selectOther.transform.position = selectObject.transform.position; if (i >4&&i<6) { selectOther.transform.eulerAngles = new Vector3(0, 90, 0); } else if (i >= 6&& i < 7) { selectOther.transform.eulerAngles = new Vector3(0, 180, 0); } else if (i >=7 && i < 8) { selectOther.transform.eulerAngles = new Vector3(0, 270, 0); } else if (i >= 8 && i < 10) { selectOther.transform.eulerAngles = new Vector3(0, -90, 0); } selectOther.transform.parent = selectObject.transform; } } else if (selectObject == Cliff) { if (i >=7) { selectOther = Props_Rock; selectOther.transform.position = selectObject.transform.position; if (i >8&&i<9) { selectOther.transform.eulerAngles = new Vector3(0, 90, 0); } else if(i >=9) { selectOther.transform.eulerAngles = new Vector3(0, 180, 0); } selectOther.transform.parent = selectObject.transform; } } } }
其中在selectedobjet创建之后,用createothers函数判断当前的cube类型,在相应的位置上生成相应的资源,比如石头,草,树木。其中资源的类型和旋转再用随机数进行随机产生。
另外,优化方面,在建立世界后删除这个脚本,并且删除前,把缓存预先生成的物体destroy掉。
至于ui挖掘图片的显示,是因为之前把canvas给误删了,再加上之后,把摄像头选为world space就能正常显示了。
目前的效果:
目前大概就这样。优化方面体会很多,首先是代码逻辑要简化,还要注意update中,尽量不要设计太复杂的算法。能预先载入的,能异步执行的尽量做,节省cpu和gpu。
至于渲染方面,shader编程还没涉及到,很深的学问。。。
随即世界的生成完成了大概20%预期,接下来要做更复杂的。今天就这样。