今天搞了一个还算有用的小工具
自己的东西用着舒心。
代码
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 {
}