Tools(三)——CollsionDetection

本文介绍了一个使用Unity实现的碰撞体生成器,该生成器能够根据地图尺寸和指定图层生成碰撞体,并将碰撞信息保存到文件中。此外,还提供了创建碰撞体对象的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录为: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
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值