直接给代码了:1,创建一个类
public class ResourceMsg : ScriptableObject { public string mdname; public Vector3 pos; public int id; public void Init() { mdname="test"; pos= new Vector3(0f, 1f, 2f); id=1; } } 2,打包 using UnityEngine; using System.Collections; using UnityEditor; using System.Collections.Generic; public class ExportResource : MonoBehaviour { [MenuItem("Example/Create/Export")] public static void Execute() { //实例化ResourceMsg ResourceMsg config = ScriptableObject.CreateInstance<ResourceMsg>(); //初始化 config.Init(); // ResourceMsg将创建为一个对象,这时在project面板上会看到这个对象 string p = "Assets/ResourceMsg.asset"; AssetDatabase.CreateAsset(config, p); Object o = AssetDatabase.LoadAssetAtPath(p, typeof(ResourceMsg)); //打包为ResourceMsg.assetbundle文件。 BuildPipeline.BuildAssetBundle(o, null, "Assets/ResourceMsg.assetbundle"); } 3使用 public class LoadSceneRes : MonoBehaviour { private ResourceMsg msg; void Start() { //先加载配置信息 StartCoroutine("LoadConfig"); } IEnumerator LoadConfig() { //加载打包的资源路径 WWW config = new WWW("file:///C:/Users/Administrator/Desktop/MyResouce/ResourceMsg.assetbundle"); yield return config; msg = config.assetBundle.mainAsset as ResourceMsg; Debug.log("mdname="+msg.mdname+" pos="+msg.pos+"id="+msg.id); } } } 注意:类的打包需在此类前加【Serializable】
|