• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
~Joke_crazy
爱生活,爱拉芳!
   首页    新随笔       管理     

本地资源_Asset

数据

using System.Collections.Generic;
using UnityEngine;

public enum Enum_Test
{
    A,
    B,
    C,
}

[System.Serializable]
public class Data_Test : ScriptableObject
{
    public Enum_Test state;
    [Range(0, 10)]
    public float value;
    public List<int> numbers;
    public List<string> infomations;
    public string description;
    //可挂载资源
    public GameObject obj;
}
View Code

处理

using System.IO;
using UnityEditor;
using UnityEngine;

public class AssetBuilder
{
    public static void CreateAsset<T>() where T : ScriptableObject
    {
        ScriptableObject data = ScriptableObject.CreateInstance<T>();

        LoadData(data);

        string path = Application.dataPath + "/Data";
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        path = string.Format("Assets/Data/{0}.asset", (typeof(T).ToString()));
        AssetDatabase.CreateAsset(data, path);
    }

    public static T LoadAsset<T>() where T : ScriptableObject
    {
        string name = typeof(T).ToString();
        string path = string.Format("Assets/Data/{0}.asset", name);
        T data = AssetDatabase.LoadAssetAtPath<T>(path);
        return data;
    }

    public static void LoadData(ScriptableObject data)
    {
        //可以去load表
        if (data.GetType() == typeof(Data_Test))
        {
            Data_Test dt = (Data_Test)data;
            dt.description = "测试";
        }
    }
}
View Code

测试

using UnityEngine;

public class LoadAssetData : MonoBehaviour
{
    void Start()
    {
        AssetBuilder.CreateAsset<Data_Test>();

        Data_Test dt = AssetBuilder.LoadAsset<Data_Test>();

        Debug.LogError(dt.description);
    }
}
View Code
posted @ 2018-07-18 13:09  ~Joke_crazy  阅读(184)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3