更新了一下,支持数组和嵌套数据结构。

 using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Reflection; [CustomPropertyDrawer(typeof(ObjectToPathAttribute))]
public class ObjectToPathDrawer : PropertyDrawer
{ public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Object target = property.serializedObject.targetObject;
ObjectToPathAttribute otpa = attribute as ObjectToPathAttribute;
System.Type objType = otpa._objType; string path = property.stringValue;
position.width = EditorGUIUtility.labelWidth * 2.0f / 3.0f; EditorGUI.LabelField(position, label);
position.x += EditorGUIUtility.labelWidth * 2.0f / 3.0f; Object _obj = null;
bool _foundObj = true;
if (!string.IsNullOrEmpty(path))
{
_obj = AssetDatabase.LoadMainAssetAtPath(path);
if (_obj == null)
{
_foundObj = false;
}
}
_obj = EditorGUI.ObjectField(position, _obj, objType, false);
if (_obj != null)
{
path = AssetDatabase.GetAssetPath(_obj);
}
else
{
if (_foundObj)
{
path = string.Empty;
}
}
position.x += EditorGUIUtility.labelWidth * 2.0f / 3.0f;
position.width = EditorGUIUtility.labelWidth;
property.stringValue = EditorGUI.TextField(position, path);
}
}

ObjectToPathDrawer

 using UnityEngine;
using System.Collections; public class ObjectToPathAttribute : PropertyAttribute
{
public System.Type _objType;
public ObjectToPathAttribute(System.Type t)
{
_objType = t;
}
}

ObjectToPathAttribute

 using UnityEngine;
using System.Collections; public class TestClass : MonoBehaviour
{
[ObjectToPath(typeof(GameObject))]
public string _prefabPath;
}

TestClass

使用上面的代码可以通过拖拽一个prefab的方式把相应的路径直接存储到public string _itemPerfabPath里,省去键盘输入步骤。不支持场景中的GameObject的拖入。

截图如下:

当prefab为空的时候的截图如下:

通过拖拽prefab来存储相应的路径的更多相关文章

  1. Dev GridView行拖拽

    http://blog.csdn.net/keyrainie/article/details/8513802 http://www.cnblogs.com/qq4004229/archive/2012 ...

  2. [Unity]背包效果-使用NGUI实现物品的拖拽效果Drag

    背包效果-使用NGUI实现物品的拖拽效果Drag 效果实现如图 对象层级关系图 PacketCell - Right 对象作为单元格背景 PacketContainer 对象作为单元格容器 Packe ...

  3. 快速开发 HTML5 WebGL 的 3D 斜面拖拽生成模型

    前言 3D 场景中的面不只有水平面这一个,空间是由无数个面组成的,所以我们有可能会在任意一个面上放置物体,而空间中的面如何确定呢?我们知道,空间中的面可以由一个点和一条法线组成.这个 Demo 左侧为 ...

  4. WPF拖拽文件(拖入拖出),监控拖拽到哪个位置,类似百度网盘拖拽

    1.往wpf中拖文件 // xaml <Grid x:Name="grid_11" DragOver="Grid_11_DragOver" Drop=&q ...

  5. HTML5 02. 多媒体控件、拖拽事件、历史记录、web存储、应用程序缓存、地理定位、网络状态

    多媒体 video:是行内块(text-align: center; 对行内块适用) <figure></figure>: 多媒体标签 : <figcaption> ...

  6. Web存储及文件拖拽

    存储 实现内容的永久保存(localStorage) 保存: localStorage.自定义键名="123"; 获取: //判断是否有内容 if(localStorage.自定义 ...

  7. 从零开始学 Web 之 HTML5(四)拖拽接口,Web存储,自定义播放器

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  8. Html5+NodeJS——拖拽多个文件上传到服务器

    实现多文件拖拽上传的简易Node项目,可以在github上下载,你可以先下载下来:https://github.com/Johnharvy/upLoadFiles/. 解开下载下的zip格式包,建议用 ...

  9. canvas 图片拖拽旋转之二——canvas状态保存(save和restore)

    引言 在上一篇日志“canvas 图片拖拽旋转之一”中,对坐标转换有了比较深入的了解,但是仅仅利用坐标转换实现的拖拽旋转,会改变canvas坐标系的状态,从而影响画布上其他元素的绘制.因此,这个时候需 ...

随机推荐

  1. spring boot2.1读取 apollo 配置中心3

    上篇记录了springboot读取apollo的配置信息,以及如何获取服务端的推送更新配置. 接下来记录一下,如何获取公共namespace的配置. 上文中使用如下代码共聚公共命名空间的配置: @Ap ...

  2. 机器学习笔记—K-均值聚类

    在聚类问题中,给定训练集 {x(1),...,x(m)},要把数据分成内聚的“簇”.这里 x(i)∈R,没有 y(i).所以,这是一个无监督学习问题. k-均值聚类算法如下: 1.随机初始化簇中心 μ ...

  3. PHP libevent函数基本介绍

    3.2   主要函数介绍 按照使用libevnet库顺序,看一下相关函数做什么操作. 3.2.1  event_init 调用event_base_new,初始化struct event_base对象 ...

  4. C++(三十二) — 常对象、常成员变量、常成员函数

    常量:对于既需要共享.又需要防止改变的数据.在程序运行期间不可改变. const 修饰的是对象中的 this 指针.所以不能被修改. 1.常对象 数据成员值在对象的整个生存期内不能改变.在定义时必须初 ...

  5. hdu4678 Mine 规律或者博弈。(博弈的sg函数不懂我是找的规律)

    链接:题意就是告诉你一个扫雷图里面每个雷的位置,有两个人,每个人都知道雷的确切位置,每个人一次可以点一部,问你谁能赢. 链接:http://acm.hdu.edu.cn/showproblem.php ...

  6. neutron源码分析(一)OpenStack环境搭建

    一.OpenStack安装 安装一个初始化的Mitaka版本的OpenStack环境用于分析,neutron源码 序号 角色 IP地址 版本 1 controller 172.16.15.161 mi ...

  7. leetcode 427. Construct Quad Tree

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

  8. halcon中你不知道的标定板细节

    本人文着重阐述以下问题: halcon是否只能使用halcon专用的标定板? halcon标定板如何生成? halcon标定板如何摆放,拍照数量有无限制? halcon是否只能使用halcon专用的标 ...

  9. HDU 1198 Farm Irrigation(并查集+位运算)

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  10. web版源码管理软件SCM-Manager安装配置

    背景 一直使用 “VisualSvn Server” 作为源码管理工具,使用一段时间之后,使用场景遇到以下问题 添加用户必需登录到服务器. 一台服务器,只能安装一个 “VisualSvn Server ...