通过拖拽prefab来存储相应的路径
更新了一下,支持数组和嵌套数据结构。
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来存储相应的路径的更多相关文章
- Dev GridView行拖拽
http://blog.csdn.net/keyrainie/article/details/8513802 http://www.cnblogs.com/qq4004229/archive/2012 ...
- [Unity]背包效果-使用NGUI实现物品的拖拽效果Drag
背包效果-使用NGUI实现物品的拖拽效果Drag 效果实现如图 对象层级关系图 PacketCell - Right 对象作为单元格背景 PacketContainer 对象作为单元格容器 Packe ...
- 快速开发 HTML5 WebGL 的 3D 斜面拖拽生成模型
前言 3D 场景中的面不只有水平面这一个,空间是由无数个面组成的,所以我们有可能会在任意一个面上放置物体,而空间中的面如何确定呢?我们知道,空间中的面可以由一个点和一条法线组成.这个 Demo 左侧为 ...
- WPF拖拽文件(拖入拖出),监控拖拽到哪个位置,类似百度网盘拖拽
1.往wpf中拖文件 // xaml <Grid x:Name="grid_11" DragOver="Grid_11_DragOver" Drop=&q ...
- HTML5 02. 多媒体控件、拖拽事件、历史记录、web存储、应用程序缓存、地理定位、网络状态
多媒体 video:是行内块(text-align: center; 对行内块适用) <figure></figure>: 多媒体标签 : <figcaption> ...
- Web存储及文件拖拽
存储 实现内容的永久保存(localStorage) 保存: localStorage.自定义键名="123"; 获取: //判断是否有内容 if(localStorage.自定义 ...
- 从零开始学 Web 之 HTML5(四)拖拽接口,Web存储,自定义播放器
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- Html5+NodeJS——拖拽多个文件上传到服务器
实现多文件拖拽上传的简易Node项目,可以在github上下载,你可以先下载下来:https://github.com/Johnharvy/upLoadFiles/. 解开下载下的zip格式包,建议用 ...
- canvas 图片拖拽旋转之二——canvas状态保存(save和restore)
引言 在上一篇日志“canvas 图片拖拽旋转之一”中,对坐标转换有了比较深入的了解,但是仅仅利用坐标转换实现的拖拽旋转,会改变canvas坐标系的状态,从而影响画布上其他元素的绘制.因此,这个时候需 ...
随机推荐
- Anaconda 环境中使用pip安装时候出现的一些问题
author:pprp date:18/8/12 --- 1. AttributeError: Module Pip has no attribute 'main' solution:降低pip的版本 ...
- Python学习札记(八) Basic5 循环
参考:循环 Note: A.for···in循环: 1.for x in ...循环就是把每个元素代入变量x,然后执行缩进块的语句. eg. #!/usr/bin/env python3 list_A ...
- linux一键安装nginx脚本
#!/bin/sh echo "----------------------------------start install nginx ------------------------- ...
- spring boot2.1读取 apollo 配置中心1
第一篇:搭建apollo配置中心 为什么选择apollo,我做了一些对比: Diamond Disconf Apollo Spring Cloud Config 数据持久性 mysql mysql ...
- Fedora安装opengl
Fedora和Ubuntu下安装OpenGL开发环境配置(我整理的)OpenGL开发库的详细介绍fedora23 安装OpenGL 开发OpenGL工程需要3个库文件和对应的头文件:libglut.s ...
- Jenkins基础复习
- server2012/win8 卸载.net framework 4.5后 无法进入系统桌面故障解决
故障:服务器装的是windows2012 standard(2012版本从低到高依次为Foundation.Essentials.StandardDatacenter,以及它们的升级版R2),由于要安 ...
- 解决boot空间不足问题
uname -a :查看现在系统信息,内核版本 dpkg --get-selections |grep linux-image : 查看内核列表 sudo apt-get remove linux-i ...
- NOIP2018小反思
今天下午做了一道叫邮票 Stamps的题.敲代码的时候就发现,好像和去年D1T2货币系统有点像,原理都是一个完全背包DP.做完之后交上去发现有几个点RE了,于是马上把数组改大,AC. 我赶忙找到去年那 ...
- hdu4347The Closest M Points kdtree
kdtree讲解: https://blog.csdn.net/qing101hua/article/details/53228668 https://blog.csdn.net/acdreamers ...