当前位置: 首页 > 工具软件 > ObjectScript > 使用案例 >

ScriptObject的使用举例

耿俊
2023-12-01

1、ScriptObject的实现子类

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

public class SysData : ScriptableObject
{
    public List<Vector3> content;
}

2、打包类:

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

public class Export:Editor
{
   [MenuItem("Assets/Export")]
    public static void Execute()
    {
       SysData sd = ScriptableObject.CreateInstance<SysData>();
       sd.content = new List<Vector3>();
       sd.content.Add(new Vector3(1, 2, 3));
       sd.content.Add(new Vector3(4, 5, 6));
       string p = "Assets/SysData.asset";
       AssetDatabase.CreateAsset(sd, p);
       Object o = AssetDatabase.LoadAssetAtPath(p, typeof(SysData));
       AssetBundleBuild[] abb = new AssetBundleBuild[1];
       abb[0].assetBundleName = "SysData.assetbundle";
       abb[0].assetNames = new string[1];
       abb[0].assetNames[0] = p;
       BuildPipeline.BuildAssetBundles(Application.dataPath, abb, BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneWindows);
       AssetDatabase.DeleteAsset(p);
    }

}

3、测试类:

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour 
{

    // Use this for initialization
    IEnumerator Start () {
        string url = "file://" + Application.dataPath + "/sysdata.assetbundle";
        WWW www = new WWW(url);
        yield return www;
        SysData sd = www.assetBundle.LoadAsset("SysData") as SysData;
        print(sd.content[0]);
        print(sd.content[1]);
    }

    // Update is called once per frame
    void Update () {

    }
}
 类似资料: