2018-10-11 【UnityEditor】创建路点物体小工具

今天搞了一个还算有用的小工具
自己的东西用着舒心。

在scene中贴着地面创建物体

代码

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

    namespace WenTools
    {
        [CustomEditor(typeof(Way))]
        public class WPCreater : Editor
        {
            Way m_selectedWay;

            Transform[] m_curWayPoints;    //当前路线所有子cube坐标

            GameObject wpObj;

            private void OnEnable()
            {
                wpObj = (GameObject)Resources.Load("WayPoint");

                //包含该组件的物体被选中时调用
                m_selectedWay = target as Way;
                Debug.Log("Way : " + m_selectedWay.name);

                DrawLine(m_selectedWay);
            }

            private void DrawLine(Way mw)
            {
                m_curWayPoints = mw.GetComponentsInChildren<Transform>();
                for (int i = 1;i < m_curWayPoints.Length - 1; i ++)
                {
                    Debug.DrawLine(m_curWayPoints[i].position, m_curWayPoints[i + 1].position, Color.yellow,0.1f);
                }
            }


            private void OnSceneGUI()
            {
                InitStyle();

                //当前事件
                Event e = Event.current;
                if(e.isKey)
                {
                    if((e.type == EventType.KeyDown) && (e.keyCode == KeyCode.C)&&(m_selectedWay != null))
                    {
                        //event中鼠标位置和unity中鼠标y轴相反
                        Camera sceneCamera = SceneView.lastActiveSceneView.camera;

                        Vector3 pos = Vector3.zero;
                        //Ray ray = sceneCamera.ScreenPointToRay(e.mousePosition);

                        Ray ray = HandleUtility.GUIPointToWorldRay(e.mousePosition);

                        RaycastHit hitInfo;
                        if(Physics.Raycast(ray,out hitInfo))
                        {
                            pos = hitInfo.point;
                        }

                        GameObject obj = (GameObject)Instantiate(wpObj);
                        obj.transform.position = pos;
                        obj.transform.parent = m_selectedWay.transform;

                        DrawLine(m_selectedWay);
                    }
                }

            }

            GUIStyle _boneNameStyle = new GUIStyle();
            GUIStyle _windowTextStyle = new GUIStyle();
            Rect _windowRect = new Rect(20, 20, 120, 20);

            void InitStyle()
            {
                _boneNameStyle.normal.textColor = Color.red;
                _windowTextStyle.fontSize = 15;
                _windowTextStyle.normal.textColor = Color.white;
            }

        }
    }

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

    public class Way : MonoBehaviour {

    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,460评论 25 708
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,960评论 2 59
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,017评论 3 119
  • 一晃就五月了,简直不得了。这周要锻炼身体的项目包括:每天两公里+八部金刚功一次 254. Factor Combi...
    健时总向乱中忙阅读 193评论 0 0
  • /细水长流 十年重聚首,把酒话相知。 杏圃同携手,歌台共妙姿。 茗香和笑语,琴曲化诗词。 月夜离钟响,莲开会有期。
    细水长流瑜阅读 425评论 2 7