using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using XLua;
[CustomEditor(typeof(LuaComponent),true)]
public class LuaComponentEditor : Editor
{
LuaComponent luaComponent;
//序列化对象
private SerializedObject obj;
//脚本路径
private SerializedProperty serializedObject;
private void OnEnable()
{
//获取当前编辑自定义Inspector的对象
luaComponent = (LuaComponent)target;
obj = new SerializedObject(target);
serializedObject = obj.FindProperty("luaScriptName");
}
public override void OnInspectorGUI()
{
EditorGUILayout.BeginVertical();
EditorGUILayout.PropertyField(serializedObject);
//获得一个长500的框
Rect mPathRect = EditorGUILayout.GetControlRect(GUILayout.Width(500));
//如果鼠标正在拖拽中或拖拽结束时,并且鼠标所在位置在文本输入框内
if ((Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragExited) && mPathRect.Contains(Event.current.mousePosition))
{
//改变鼠标的外表
DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
{
string retPath = DragAndDrop.paths[0];
retPath = retPath.Substring(18).Replace(".lua.txt","");
serializedObject.stringValue = retPath;
luaComponent.luaScriptName = retPath;
}
EditorUtility.SetDirty(target);
obj.ApplyModifiedProperties();
obj.Update();
}
EditorGUILayout.EndVertical();
}
}