当前位置:网站首页>Unity自动生成阻挡Collider的GameObject工具
Unity自动生成阻挡Collider的GameObject工具
2022-08-04 05:25:00 【丁小未】
前言
场景中会有碰撞阻挡,或者触发器,一般是美术或者策划配置,策划一般是直接配置collider,但为了调试方便,需要显示对应的mesh方便查看防止的位置是否正确,我们可以写个工具来控制生成mesh和删除mesh方便调试使用。
思路
一般就是几种内置的collider,meshcollider、boxcollider、capsulecollider、spherecollider等,我们判断出对应的collider,然后创建unity内置的一些GameObject即可,meshcollider只需要实例化collider的mesh即可。
效果
代码
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using Sirenix.Utilities.Editor;
using Sirenix.OdinInspector;
using System;
public class ColliderMeshGenerateTool : MonoBehaviour
{
bool show = false;
List<GameObject> mColliderObjCache = new List<GameObject>();
List<GameObject> mGameObjects = new List<GameObject>();
[ShowInInspector]
public bool Show
{
get
{
return show;
}
set
{
ChangeState(value);
show = value;
}
}
void Start()
{
mGameObjects = GetAllSceneObjectsWithInactive();
}
private void ChangeState(bool isShow)
{
if (isShow)
{
foreach (var obj in mGameObjects)
{
if (obj.GetComponent<MeshRenderer>() == null)
AnalyzeCollider(obj);
}
}
else
{
for (int i = mColliderObjCache.Count - 1; i >= 0; i--)
{
GameObject.DestroyImmediate(mColliderObjCache[i]);
}
mColliderObjCache.Clear();
}
}
private void AnalyzeCollider(GameObject obj)
{
var collider = obj.GetComponent<Collider>();
if (collider == null)
{
Debug.LogError("不存在Collider");
return;
}
Type type = collider.GetType();
GameObject tempObj = null;
if (type == typeof(SphereCollider))
{
tempObj = CreatePrimitiveGameObject(obj.transform, PrimitiveType.Sphere);
var sphereCollider = obj.GetComponent<SphereCollider>();
tempObj.transform.localScale = sphereCollider.radius * Vector3.one * 2;
}
else if (type == typeof(BoxCollider))
{
tempObj = CreatePrimitiveGameObject(obj.transform, PrimitiveType.Cube);
var boxCollider = obj.GetComponent<BoxCollider>();
tempObj.transform.localScale = boxCollider.size;
}
else if (type == typeof(MeshCollider))
{
tempObj = CreateGameObject(obj.transform, "MeshGameObject");
tempObj.AddComponent<MeshRenderer>();
var filter = tempObj.AddComponent<MeshFilter>();
var meshCollider = obj.GetComponent<MeshCollider>();
filter.mesh = meshCollider.sharedMesh;
}
else if (type == typeof(CapsuleCollider))
{
tempObj = CreatePrimitiveGameObject(obj.transform, PrimitiveType.Capsule);
var capsuleCollider = obj.GetComponent<CapsuleCollider>();
var doubleRadius = capsuleCollider.radius * 2;
var tempScaleY = capsuleCollider.height < capsuleCollider.radius * 2 ? capsuleCollider.radius : capsuleCollider.height / 2;
tempObj.transform.localScale = new Vector3(doubleRadius, tempScaleY, doubleRadius);
}
if (tempObj != null)
mColliderObjCache.Add(tempObj);
}
private GameObject CreatePrimitiveGameObject(Transform parent, PrimitiveType type = PrimitiveType.Cube)
{
var obj = GameObject.CreatePrimitive(type);
obj.transform.parent = parent;
ResetTransformData(obj);
return obj;
}
private GameObject CreateGameObject(Transform parent, string name)
{
var obj = new GameObject(name);
obj.transform.parent = parent;
ResetTransformData(obj);
return obj;
}
private void ResetTransformData(GameObject obj)
{
obj.transform.localPosition = Vector3.zero;
obj.transform.localRotation = Quaternion.Euler(Vector3.zero);
obj.transform.localScale = Vector3.one;
}
/// <summary>
/// Select All ActiveInHierarchy GameObject in Hierarchy
/// </summary>
/// <returns></returns>
private List<GameObject> GetAllSceneObjectsWithInactive()
{
var allTransforms = Resources.FindObjectsOfTypeAll(typeof(Transform));
var previousSelection = Selection.objects;
Selection.objects = allTransforms.Cast<Transform>()
.Where(x => x != null)
.Select(x => x.gameObject)
.Where(x => x != null && x.activeInHierarchy)
.Where(x => x.GetComponent<Collider>() != null)
.Cast<UnityEngine.Object>().ToArray();
var selectedTransforms = Selection.GetTransforms(SelectionMode.Editable | SelectionMode.ExcludePrefab);
Selection.objects = previousSelection;
return selectedTransforms.Select(tr => tr.gameObject).ToList();
}
}
#endif
更多精品教程
边栏推荐
- 《看见新力量》第四期免费下载!走进十五位科技创业者的精彩故事
- 部署LVS-DR群集【实验】
- 编程大杂烩(四)
- Typora 使用保姆级教程 | 看这一篇就够了 | 历史版本已被禁用
- 【问题解决】同一机器上Flask部署TensorRT报错记录
- 力扣:62.不同路径
- 想好了吗?
- npm init [email protected] 构建项目报错SyntaxError: Unexpected token ‘.‘解决办法
- TensorRTx-YOLOv5工程解读(一)
- C Expert Programming Chapter 5 Thinking about Chaining 5.6 Take it easy --- see who's talking: take the Turning quiz
猜你喜欢
idea设置识别.sql文件类型以及其他文件类型
px、em、rem的区别
Cannot read properties of null (reading 'insertBefore')
Can 't connect to MySQL server on' localhost3306 '(10061) simple solutions
编程大杂烩(三)
sql server如何得到本条记录与上一条记录的差异,即变动值
npm安装依赖报错npm ERR! code ENOTFOUNDnpm ERR! syscall getaddrinfonpm ERR! errno ENOTFOUND
C1认证之web基础知识及习题——我的学习笔记
CentOS7 - yum install mysql
Dynamic programming of the division of numbers
随机推荐
4.1 JdbcTemplate for declarative transactions
MySQL日期函数
word 公式编辑器 键入技巧 | 写数学作业必备速查表
4.3 Annotation-based declarative transactions and XML-based declarative transactions
【评价类模型】Topsis法(优劣解距离法)
4.2 声明式事务概念
心余力绌:企业面临的软件供应链安全困境
OpenCV获取和设置图像的平均亮度
The 2022 PMP exam has been delayed, should we be happy or worried?
JS基础--强制类型转换(易错点,自用)
MySQL log articles, binlog log of MySQL log, detailed explanation of binlog log
Write golang simple C2 remote control based on gRPC
注意!软件供应链安全挑战持续升级
7、特殊SQL的执行
力扣:343. 整数拆分
npm安装依赖报错npm ERR! code ENOTFOUNDnpm ERR! syscall getaddrinfonpm ERR! errno ENOTFOUND
string类简介
腾讯136道高级岗面试题:多线程+算法+Redis+JVM
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.1 数组并非指针
MySql数据恢复方法个人总结