当前位置:网站首页>Unity3d 模型中心点的转换(源代码)
Unity3d 模型中心点的转换(源代码)
2022-07-17 13:31:00 【范小多】
我们在使用Unity3d模型时,从3DS导出的模型可能中心点不在轴对称点,需要重新在3DS编辑后导出,很不方便。用以下代码可直接在Unity3d中转换 中心点。

将下面代码拷贝 到你工程后边即可,Unity3d->tools-MyTool 点击重新设置即可
public class MyTool : ScriptableObject
{
//重置模型的轴心为中心
[MenuItem("Tools/MyTool/ResetPivot")]
static void ResetPivot()
{
//获取选中的物体
GameObject target = Selection.activeGameObject;
string dialogTitle = "Tools/MyTool/ResetPivot";
if (target == null)
{
EditorUtility.DisplayDialog(dialogTitle, "没有选中需要重置轴心的物体!!!", "确定");
return;
}
//获取目标物体下所有网格渲染
MeshRenderer[] meshRenderers = target.GetComponentsInChildren<MeshRenderer>(true);
if (meshRenderers.Length == 0)
{
EditorUtility.DisplayDialog(dialogTitle, "选中的物体不是有效模型物体!!!", "确定");
return;
}
//将所有的网格渲染的边界进行合并
Bounds centerBounds = meshRenderers[0].bounds;
for (int i = 1; i < meshRenderers.Length; i++)
{
centerBounds.Encapsulate(meshRenderers[i].bounds);
}
//创建目标的父物体
Transform targetParent = new GameObject(target.name + "-Parent").transform;
//如果目标原来已有父物体,则将创建目标父物体的父物体设为原父物体;
Transform originalParent = target.transform.parent;
if (originalParent != null)
{
targetParent.SetParent(originalParent);
}
//设置目标父物体的位置为合并后的网格渲染边界中心
targetParent.position = centerBounds.center;
//设置目标物体的父物体
target.transform.parent = targetParent;
Selection.activeGameObject = targetParent.gameObject;
EditorUtility.DisplayDialog(dialogTitle, "重置模型物体的轴心完成!", "确定");
}
}
边栏推荐
猜你喜欢

Leetcode丑数题解

常见集合特性

Design of the multi live architecture in different places of the king glory mall

人大、微软等提出InclusiveFL:异构设备上的包容性联邦学习

ROS 重名

LeetCode 2331. 计算布尔二叉树的值(树的遍历)

LeetCode 2249. Count the number of grid points in the circle

二分类学习推广到多分类学习

Design and Simulation of intelligent storage cabinet control system

Scala在Idea中的配量
随机推荐
【在vivado中调ila IP核】
Satellite network capacity improvement method based on network coding
追根问底:Objective-C关联属性原理分析
Svn learning
Pytorch.nn实现多层感知机
Pytorch. NN implementation of multi-layer perceptron
C serialport configuration and attribute understanding
Satellite network capacity improvement method based on network coding
Take a look at this ugly face | MathWorks account unavailable - technical issue
[PostgreSQL] PostgreSQL 15 optimizes distinct
Redis集群、一主二从三哨兵的搭建
树链剖分思想讲解 + AcWing 2568. 树链剖分(dfs序 + 爬山法 + 线段树)
Pytorch手动实现多层感知机
Leetcode丑数题解
高数__方程与函数的关系
Pytoch and weight decay (L2 norm)
[Acwing] 第 60 场周赛 C-AcWing 4496. 吃水果
Avi 部署使用指南(2):Avi 架构概述
发明闪存能赚多少钱?这是一个日本的狗血故事
Find balanced binary tree