創建和使用 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);
    }  
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM