创建和使用 ScriptableObject

创建  

using System;
using UnityEditor;
using UnityEngine;
public class CreateAsset : Editor
{

    //在菜单栏创建功能项
    [MenuItem("数据/Excel")]
    static void Create()
    {
        ScriptableObject scriptable = Objects.CreateInstance<Objects>();
        // string path = string.Format("{0}/{1}.asset", "Assets/Resources", "nanexcle");
        //路径名称
        string path = string.Format("Assets/Resources/nanexcle.asset");
        //ExcelTable table = new ExcelTable();
        // table.Du();

        AssetDatabase.CreateAsset(scriptable, path);
    }
}

赋值

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Objects : ScriptableObject
{
    public string[] str;
    public GameObject game;
    public Texture2D texture2D;
    public List<int> vs;
    [ContextMenu("刷新数据")]
    void Update()
    {
        //给数据赋值
        Debug.Log("给数据赋值");
    }
}

取值

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AppObject : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Objects objects= Resources.Load<Objects>("nanexcle");
        Debug.Log(objects.str.Length);
    }  
}

 

上一篇:Python 最佳实践指南 2018 学习笔记


下一篇:ScrollRect重写 循环利用