当前位置:网站首页>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
更多精品教程
边栏推荐
- Chapter 5 C programming expert thinking 5.4 alert Interpositioning of links
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.3 What is a Declaration and What is a Definition
- Can 't connect to MySQL server on' localhost3306 '(10061) simple solutions
- Will the 2023 PMP exam use the new version of the textbook?Reply is here!
- Web Basics and Exercises for C1 Certification - My Study Notes
- 去重的几种方式
- JS基础--强制类型转换(易错点,自用)
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.2 Why does my code not work
- 4.2 声明式事务概念
- 7.13 Day20----MYSQL
猜你喜欢

day13--postman interface test

C语言 -- 操作符详解

The difference between px, em, and rem

Resolved error: npm WARN config global `--global`, `--local` are deprecated

canal实现mysql数据同步

Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法

高性能高可靠性高扩展性分布式防火墙架构

谷粒商城-基础篇(项目简介&项目搭建)

word 公式编辑器 键入技巧 | 写数学作业必备速查表

el-Select selector bottom fixed
随机推荐
LCP 17. 速算机器人
MySQL log articles, binlog log of MySQL log, detailed explanation of binlog log
在被面试官说了无数次后,终于潜下心来整理了一下JVM的类加载器
Can 't connect to MySQL server on' localhost3306 '(10061) simple solutions
字节最爱问的智力题,你会几道?
[SemiDrive source code analysis] [MailBox inter-core communication] 47 - Analysis of RPMSG_IPCC_RPC mode limit size of single transmission and limit bandwidth test
9、动态SQL
MySQL date functions
编程大杂烩(四)
Grain Mall - Basics (Project Introduction & Project Construction)
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection
7、特殊SQL的执行
Delphi-C端有趣的菜单操作界面设计
DataTable uses Linq for grouping and summarization, and converts the Linq result set into DataTable
What are the steps for how to develop a mall system APP?
JS basics - forced type conversion (error-prone, self-use)
C Expert Programming Chapter 5 Thinking about Linking 5.2 Advantages of Dynamic Linking
npm init [email protected] 构建项目报错SyntaxError: Unexpected token ‘.‘解决办法
应届生软件测试薪资大概多少?
TensorRT例程解读之语义分割demo