您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页ScriptableObjectUtil

ScriptableObjectUtil

来源:二三四教育网

简单脚本创建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, _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 =  (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;

}

本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。

热门图文

Copyright © 2019-2025 how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务