【Unity3d】ScriptableObject的简单用法
ScriptableObject非常适合小数量的游戏数值。
使用ScriptableObject的时候需要注意,生成ScriptableObject数据文件需要自己写Editor代码实现。
大概的工作流程是:写一个ScriptableObject(继承ScriptableObject的类,相当于数据模板) → 使用自己写的Editor代码生成一个实现该模板的数据文件 → Inspector面板中编辑数据。
给出我的Editor代码:
using UnityEngine;
using System.IO;
using System.Collections;
using UnityEditor; public class Scriptablity : MonoBehaviour { public static T Create<T> ( string _path, string _name) where T : ScriptableObject { if ( new DirectoryInfo(_path).Exists == false ) {
Debug.LogError ( "can't create asset, path not found" );
return null;
}
if ( string.IsNullOrEmpty(_name) ) {
Debug.LogError ( "can't create asset, the name is empty" );
return null;
}
string assetPath = Path.Combine( _path, _name + ".asset" ); T newT = ScriptableObject.CreateInstance<T>();
AssetDatabase.CreateAsset(newT, assetPath);
Selection.activeObject = newT;
return newT;
} public static void Create<T>() where T : ScriptableObject { string assetName = "New " + typeof(T).Name;
string assetPath = "Assets";
if(Selection.activeObject) {
assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
if (Path.GetExtension(assetPath) != "")
{
assetPath = Path.GetDirectoryName(assetPath);
}
} bool doCreate = true;
string path = Path.Combine( assetPath, assetName + ".asset" );
FileInfo fileInfo = new FileInfo(path);
if ( fileInfo.Exists ) {
doCreate = EditorUtility.DisplayDialog( assetName + " already exists.",
"Do you want to overwrite the old one?",
"Yes", "No" );
}
if ( doCreate ) {
T T_info = Create<T> ( assetPath, assetName );
Selection.activeObject = T_info;
}
} public static void Create() { Debug.LogError("You should call 'Create' method like this : Create<ExampleData>();");
}
}
使用方法:
1.写个ScriptableObject。
public class Example : ScriptableObject {
public int id;
public string name;
}
2.写个MonoBehavior调用Create方法生成Example格式的数据。
public class ExampleItem: MonoBehaviour {
[MenuItem("Example/Create/Example Data")]
static void CreatExample() {
Scriptablity.Create<Example>();
}
}
3.在Inspector面板编辑数据。
4.使用该数据
public class ExampleUsage : MonoBehavior {
public Example exampleInfo;
void Start()
{
Debug.Log(exampleInfo.name);
}
}
【Unity3d】ScriptableObject的简单用法的更多相关文章
- CATransition(os开发之画面切换) 的简单用法
CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...
- jquery.validate.js 表单验证简单用法
引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...
- NSCharacterSet 简单用法
NSCharacterSet 简单用法 NSCharacterSet其实是许多字符或者数字或者符号的组合,在网络处理的时候会用到 NSMutableCharacterSet *base = [NSMu ...
- [转]Valgrind简单用法
[转]Valgrind简单用法 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian S ...
- Oracle的substr函数简单用法
substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H' *从字符串第一个字符开始截取长度为1的字符串 subst ...
- Ext.Net学习笔记19:Ext.Net FormPanel 简单用法
Ext.Net学习笔记19:Ext.Net FormPanel 简单用法 FormPanel是一个常用的控件,Ext.Net中的FormPanel控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ...
- TransactionScope简单用法
记录TransactionScope简单用法,示例如下: void Test() { using (TransactionScope scope = new TransactionScope()) { ...
- WPF之Treeview控件简单用法
TreeView:表示显示在树结构中分层数据具有项目可展开和折叠的控件 TreeView 的内容是可以包含丰富内容的 TreeViewItem 控件,如 Button 和 Image 控件.TreeV ...
- listActivity和ExpandableListActivity的简单用法
http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html 今天自己简单的总结了listActivity和Expandable ...
随机推荐
- leetcode shell
leetcode 195. 第十行 # | | 第一种是先取出前10行,然后取出最后一行.(但是不足10行,也可以取出最后一行) 正解: tail -n +K :从第K行取出所有 然后取出第一行 le ...
- Python中的类(一)
Python中的类(一) 一. 应用场景 如果多个函数中有一些相同的参数时,转换成面向对象. 二. 如何创建类 类是用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法 ...
- 4、Dubbo-工程实践
4.实践测试 4.1).提出需求 某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址: 我们现在 需要创建两个服务模块进行测试 测试预期结果: 订单服务web模块在A服务器,用户服务模块在 ...
- 2019.3.25 IDEA控制台乱码解决 &&idea关闭代码自动提示
设置Tomcat里面的conf文件夹下的properties结尾的文件
- Kali-linux查看打开的端口
对一个大范围的网络或活跃的主机进行渗透测试,必须要了解这些主机上所打开的端口号.在Kali Linux中默认提供了Nmap和Zenmap两个扫描端口工具.为了访问目标系统中打开的TCP和UDP端口,本 ...
- mvc4中viewbag viewdata 和 tempdata的区别
ViewBag 不再是字典的键值对结构,而是 dynamic 动态类型,它会在程序运行的时候动态解析. eg: ViewBag.NumberObjs = new string[] { "on ...
- math.random用法
Math.random():获取0~1随机数 Math.floor() method rounds a number DOWNWARDS to the nearest integer, and ret ...
- selenium + python自动化测试unittest框架学习(一)selenium原理及应用
unittest框架的学习得益于虫师的<selenium+python自动化实践>这一书,该书讲得很详细,大家可以去看下,我也只学到一点点用于工作中,闲暇时记录下自己所学才能更加印象深刻. ...
- Go 测试单个方法
1.目录 gotest.go package mytest import ( "errors" ) func Division(a, b float64) (float64, er ...
- python 装饰器 传递参数简单案例
def debug(func): def wrapper(*args, **kwargs): # 指定宇宙无敌参数 print "[DEBUG]: enter {}()".form ...