Tools(三)——CollsionDetection
来源:互联网 发布:linux 启动脚本 编辑:程序博客网 时间:2024/05/20 22:28
目录为:Assets/Scripts/Tools/Editor,CollsionDetection.cs如其名,这个文件里面的函数就是用于创建碰撞体。
using UnityEngine; using System.Collections;using System.Collections.Generic;using System.IO;using System; public class CollsionDetection:MonoBehaviour{ public Transform obj; void Start() { } void Update() { } /// <summary> /// Creates the block file. /// </summary> /// <param name="map_width">Map_width.</param> /// <param name="map_height">Map_height.</param> /// <param name="fileName">File name.</param> /// <param name="layer_name">Layer_name.</param> //map_height和map_width定义的矩形里的点,把有碰撞的地方都测出来(排除layer_name的layer层物体),然后保存到文件 //感觉这个函数像是为寻路做准备的,要具体看看在哪里有用到 public static void CreateTxtFile( float map_width, float map_height, string fileName, string layer_name ) { byte tag = 0; //layermask,可以google一下用法 int layer = 1 << LayerMask.NameToLayer(layer_name); // string filePath = "E:\\"+fileName+".dat"; FileStream fs = new FileStream (fileName, FileMode.OpenOrCreate, FileAccess.Write); BinaryWriter bw = new BinaryWriter (fs); for(int i = 0;i < map_height;i++) { for(int j = 0;j < map_width;j++) { float a = Convert.ToSingle(j / 2.00f + 0.25f); float b =Convert.ToSingle(i / 2.00f + 0.25f); Vector3 pos = new Vector3(a,66.00f,b); //这个函数检查pos为球心,0.2f为半径的球范围内,是否发生collider碰撞,不检测layer层物体的碰撞 bool isBunker = Physics.CheckSphere(pos,0.2f,layer); if(isBunker || (i == 0 && j == 0) || ((i == map_height -1) && (j == map_width -1)) ) { tag = 1; } else { tag = 0; } if(i == 0 && j == 0) { tag = 1; } bw.Write(tag); } } fs.Close (); bw.Close (); } //和上面有点类似 public static void SetCollisionBuf(int map_width, int map_height,string layer_name ,out byte [] buf) { Debug.Log("map_width " + map_width + " height " + map_height); int layer = 1 << LayerMask.NameToLayer(layer_name); byte tag = 0; buf = new byte[map_width * map_height]; for(int i = 0;i < map_height;i++) { for(int j = 0;j < map_width;j++) { float a = Convert.ToSingle(j + 0.5f); float b = Convert.ToSingle(i + 0.5f); Vector3 pos = new Vector3(a,66.00f,b); bool isBunker = Physics.CheckSphere(pos,0.5f,layer); if(isBunker) { tag = 1; } else { tag = 0; } buf[j+i*map_width] = tag; //if ((j + i * map_width) % 10 == 0) //{ // Debug.Log(tag); //} } } buf [0] = 1; buf [map_width * map_height -1] = 1; } #region 创建碰撞体 //如其名,是真的在创建碰撞体 public static void createCollsionObject( float map_width, float map_height, string layer_name ) { //Created BY Run ----Start------------ int tag = 0; int layer = 1 << LayerMask.NameToLayer(layer_name); GameObject obj_parent = UnityEngine.Object.Instantiate (Resources.Load ("Cube/Cubebb"), Vector3.zero, Quaternion.identity) as GameObject; for(int i = 0;i < map_height;i++) { for(int j = 0;j < map_width;j++) { float a = Convert.ToSingle(j / 2.00f + 0.25f); float b =Convert.ToSingle(i / 2.00f + 0.25f); Vector3 pos = new Vector3(a,66.00f,b); bool isBunker = Physics.CheckSphere(pos,0.2f,layer); if(i == 0 && j < 5) { tag = 1; } if(isBunker) { tag = 1; GameObject tobj = UnityEngine.Object.Instantiate(Resources.Load("Cube/Cubebb") , pos , Quaternion.identity) as GameObject; tobj.transform.parent = obj_parent.transform; } else { tag = 0; } } } } #endregion}
阅读全文
0 0
- Tools(三)——CollsionDetection
- jquery tools 系列(三)——scrollable(1)
- jquery tools 系列(三)——scrollable(2)
- Tools(一)——BuildMethod
- Tools(二)——BytesAdd
- Tools(四)——FileU3DBuild
- Tools——PVRTrace
- Development environment and tools of device driver(三)
- Tools——Snapdragon Profiler
- Jquery Tools介绍—Tooltips
- WebIDE—Firefox Developer Tools
- GP学习(二)—Executing tools and Accesubg licensing0
- Controller Tools——场景类型介绍
- ArcToolbox使用之一——Analysis Tools
- 软件安装——安装Vmware Tools
- java——gis 开发 Geo Tools
- Tools——Mali Graphics Debugger
- EXCEL图表工具箱—JWalk Chart Tools
- jquery操作复选框的总结
- Selenium with Python中文翻译(八)
- js的call方法和apply方法
- POJ 3259Wormholes
- nohup command 2>&1 &的解释
- Tools(三)——CollsionDetection
- javascript对字符串的操作
- Python matplotlib库scatter函数参数详解
- 第二周 程序的多文件组织
- 算法和数据结构可视化教程
- jquery库实现iframe自适应内容高度和宽度
- FLEX布局
- Android显示提示信息,实现两个界面之间的跳转
- 针对transform中的几种值的先后顺序