ScriptableObjectUtil
2017-05-31 本文已影响18人
RichMartin
简单脚本创建ScriptableObject
using UnityEngine;
using System.IO;
using UnityEditor;
public class _ScriptableObjectUtil : MonoBehaviour
{
public static T Creat<T> (string _path, string _name)where T:ScriptableObject
{
if (!new DirectoryInfo (_path).Exists) {
Directory.CreateDirectory (_path);
}
if (string.IsNullOrEmpty (_name)) {
Debug.LogError ("Can't Creat Asset " + _name + "Is Null");
return null;
}
string assetPath = Path.Combine (_path, _name + ".asset");
T newT = ScriptableObject.CreateInstance<T> ();
AssetDatabase.CreateAsset (newT, assetPath);
Selection.activeObject = newT;
return newT;
}
/// <summary>
/// 创建对象的对外接口
/// </summary>
/// <param name="_path">Path.</param>
/// <param name="_name">Name.</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
public static void Creat<T> () where T:ScriptableObject
{
string assetName = /*"New" +*/ typeof(T).Name;
string assetPath = "Assets/Resources/ScriptableObject";
if (Selection.activeObject) {
assetPath = AssetDatabase.GetAssetPath (Selection.activeObject);
if (Path.GetExtension (assetPath) != null) {
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 + " is already exits."," Is overwrite?", "Yes", "No");
}
if (doCreate) {
T T_INFO = Creat<T> (assetPath, assetName);
Selection.activeObject = T_INFO;
}
}
public static void Creat(){
Debug.LogError ("You should call 'Creat' method like:Create<ExampleData>");
}
[MenuItem("Assets/ScriptableObj/CreatScriptableObject")]
static void CreatAsset()
{
//创建接口
_ScriptableObjectUtil.Creat<ProductAgeScriptObj>();
}
}
-
using UnityEngine;
[System.Serializable]
public class Data1
{
public int age;
public string app_key;
public string app_name;
}
public class ProductAgeScriptObj : ScriptableObject
{
public int status;
public string info;
public Data1[] data;
// public static ProductAgeScriptObj Instance
// {
// get
// {
// string path = "ScriptableObject/ProductAgeScriptObj";
// if (instance == null)
// instance = Resources.Load<ProductAgeScriptObj>(path);
// return instance;
// }
// }
// static ProductAgeScriptObj instance;
}