unity编辑器拓展三——一键调整scene中物体坐标中心点

1.描述

  上一篇提到了物体的坐标朝向,这一篇来说说物体的坐标中心点。单个物体 ,或者包含很多物体的父物体,我们希望

他的坐标在中心点 ,或者在底端中心,或者在顶端中心。我们有什么办法么,只能手动拖,而且只能拖个大概,没办法准

确控制。所以我们要做的就是能通过工具一键控制它的坐标位置

1.底端中心点   2 中间中心点 3 顶端中心点

2.代码如下

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class SceneTool : EditorWindow
{
    [MenuItem("Owen/Tool")]
    //绘制窗口
    static void tranPoint()
    {
        SceneTool win = (SceneTool)EditorWindow.GetWindow(typeof(SceneTool), false, "Tool", false);
        win.Show();
    }
    void OnGUI()
    {
        //设置字体类型
        GUIStyle style1 = new GUIStyle();
        //字体大小为15
        style1.fontSize = 15;
        //字体颜色为灰白色
        style1.normal.textColor = new Color(0.7f, 0.7f, 0.7f);

        GUIStyle style2 = new GUIStyle();
        style2.fontSize = 13;
        style2.normal.textColor = new Color(0.7f, 0.7f, 0.7f);

        //垂直绘制
        GUILayout.BeginHorizontal();
        {
            //lab的绘制
            EditorGUILayout.LabelField("▼TransformPoint", style1);


            //控制坐标位置的按钮绘制

            if (GUI.Button(new Rect(60, 40, 70, 25), "Top"))
            {
                if (Selection.activeGameObject)
                    TopPoint();

            }
            if (GUI.Button(new Rect(60, 70, 70, 25), "Center"))
            {
                if (Selection.activeGameObject)
                    CenterPoint();
            }
            if (GUI.Button(new Rect(60, 100, 70, 25), "Button"))
            {
                if (Selection.activeGameObject)
                    Buttom();
            }

        }
    }

        //坐标放在物体中心
        void CenterPoint()
        {

            Transform parent = Selection.activeGameObject.transform;
            if (parent.GetComponent<Renderer>() != null)
            {
                GameObject parent02 = new GameObject("A");

                parent02.transform.position = parent.position;
                parent02.transform.SetParent(parent.parent);
                parent.SetParent(parent02.transform);
                Selection.activeGameObject = parent02;
                parent = parent02.transform;

            }

            Vector3 position = parent.position;
            Vector3 scale = parent.localScale;
            //Vector3 rotate = parent.localEulerAngles;


            parent.position = Vector3.zero;
            parent.localScale = Vector3.one;
            //parent.localEulerAngles = Vector3.zero;


        Renderer[] m_renderer = parent.GetComponentsInChildren<Renderer>();
        Vector3 center = Vector3.zero;

        foreach (Renderer child in m_renderer)
        {
            center += child.bounds.center;
        }
        center /= m_renderer.Length;



        Bounds m_bounds = new Bounds(center, Vector3.zero);
        foreach (Renderer b in m_renderer)
        {
            m_bounds.Encapsulate(b.bounds);
        }
        foreach (Transform t in parent)
        {
            t.position = t.position - m_bounds.center;
        }

        parent.position = position + m_bounds.center;
        parent.localScale = scale;
        //parent.localEulerAngles = rotate;
        }

        //坐标放在物体中心的最顶端
        void TopPoint()
        {
            Transform parent = Selection.activeGameObject.transform;
            if (parent.GetComponent<Renderer>() != null)
            {
                GameObject parent02 = new GameObject("A");

                parent02.transform.position = parent.position;
                parent02.transform.SetParent(parent.parent);
                parent.SetParent(parent02.transform);
                Selection.activeGameObject = parent02;
                parent = parent02.transform;
            }

            Vector3 position = parent.position;
            Vector3 scale = parent.localScale;
            //Vector3 rotate = parent.localEulerAngles;


            parent.position = Vector3.zero;
            parent.localScale = Vector3.one;
            //parent.localEulerAngles = Vector3.zero;


            Renderer[] m_renderer = parent.GetComponentsInChildren<Renderer>();
            Vector3 center = Vector3.zero;

            foreach (Renderer child in m_renderer)
            {
                center += child.bounds.center;
            }
            center /= m_renderer.Length;


            Bounds m_bounds = new Bounds(center, Vector3.zero);
            foreach (Renderer b in m_renderer)
            {
                m_bounds.Encapsulate(b.bounds);
            }

            Vector3 V = m_bounds.center + new Vector3(0, m_bounds.size.y / 2, 0);

            foreach (Transform t in parent)
            {
                t.position -= V;
            }

            parent.position = position + V;
            parent.localScale = scale;
            //parent.localEulerAngles = rotate;
        }

        //坐标放在物体中心的最底端
        void Buttom()
        {
            Transform parent = Selection.activeGameObject.transform;
            if (parent.GetComponent<Renderer>() != null)
            {
                GameObject parent02 = new GameObject("A");

                parent02.transform.position = parent.position;
                parent02.transform.SetParent(parent.parent);
                parent.SetParent(parent02.transform);
                Selection.activeGameObject = parent02;
                parent = parent02.transform;
            }

            Vector3 position = parent.position;
            Vector3 scale = parent.localScale;
            //Vector3 rotate = parent.localEulerAngles;


            parent.position = Vector3.zero;
            parent.localScale = Vector3.one;
            //parent.localEulerAngles = Vector3.zero;


            Renderer[] m_renderer = parent.GetComponentsInChildren<Renderer>();
            Vector3 center = Vector3.zero;

            foreach (Renderer child in m_renderer)
            {
                center += child.bounds.center;
            }
            center /= m_renderer.Length;


            Bounds m_bounds = new Bounds(center, Vector3.zero);
            foreach (Renderer b in m_renderer)
            {
                m_bounds.Encapsulate(b.bounds);
            }

            Vector3 V = m_bounds.center - new Vector3(0, m_bounds.size.y / 2, 0);

            foreach (Transform t in parent)
            {
                t.position -= V;
            }

            parent.position = position + V;
            parent.localScale = scale;
            //parent.localEulerAngles = rotate;
        }

    
}

 

3.窗口如下

亲,如果您觉得本文不错,愿意给我一些动力的话,请用手机扫描二维码即可向我打赏

                                         打赏

 

 

 

 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Unity编辑器拓展(Editor Extension)可以通过自定义的脚本来扩展Unity编辑器的功能和界面,以满足特定项目的需求。通过编辑器拓展,开发者可以创建自定义的编辑器窗口、工具栏按钮、菜单项、检视面板等,来增强Unity编辑器的功能和流程。 要创建一个Unity编辑器拓展,你可以使用C#编写一个继承自Editor类的脚本。这个脚本可以通过Unity的Inspector面板来设置相关的属性和行为。以下是一个简单的示例: ```csharp using UnityEngine; using UnityEditor; public class MyEditorExtension : EditorWindow { [MenuItem("Custom Tools/My Editor Window")] public static void OpenWindow() { // 创建并打开一个自定义的编辑器窗口 MyEditorExtension window = (MyEditorExtension)EditorWindow.GetWindow(typeof(MyEditorExtension)); window.Show(); } private void OnGUI() { // 在编辑器窗口绘制UI元素 GUILayout.Label("Hello, I am a custom editor window!"); if (GUILayout.Button("Click Me")) { Debug.Log("Button clicked!"); } } } ``` 上述代码创建了一个自定义的编辑器窗口,并在窗口绘制了一个标签和一个按钮。通过在Unity编辑器点击"Custom Tools"菜单下的"My Editor Window",可以打开这个自定义的编辑器窗口。 除了编辑器窗口,你还可以通过继承Editor类来创建自定义的检视面板、菜单项等。Unity官方文档有更详细的教程和示例,可以帮助你更深入地了解和使用Unity编辑器拓展
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值