使用UGUI的朋友,肯定都知道Sprite(精灵), 关于如何打包图集,MOMO大神已经写出了博客。http://www.xuanyusong.com/archives/3304
就这个问题,我自己想了想,不知道于雨松MOMO 差距有多大,坐等他的想法。
但是在没有看到之前自己项目还是要进行的,一下是我的做法:
1分析 雨松说的 把文件夹下的图片序列化在一个Prefab上, 那什么是序列化? 不知道什么是序列化能不能做,(直接把资源拖到脚本上吧)
AssetBundle 打包 创建一个StreamingAssets文件夹
using UnityEngine;
using System.Collections;
using UnityEditor;
public class AssetBundleEditor : Editor {
[MenuItem("Custom Editor /Create AsstBundles Main")]
static void CreateAssetBundlesMain()
{
// 获取在Project 视图中选择的所有游戏对象
Object [] SelectAsset = Selection.GetFiltered (typeof(Object),SelectionMode.Deep);
foreach (Object Obj in SelectAsset) {
string sourcePath = AssetDatabase.GetAssetPath(Obj);
string targetPath = Application.dataPath +"/StreamingAssets/"+Obj.name+".assetbundle";
if (BuildPipeline.BuildAssetBundle(Obj,null,targetPath,BuildAssetBundleOptions.CollectDependencies)) {
Debug.Log(Obj.name +"资源打包成功");
}
else {
Debug.Log(Obj.name + "资源打包失败");
}
}
AssetDatabase.Refresh ();
}
}
预设上面的脚步,用来保存数据
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TestSprite : MonoBehaviour {
[Header("序列化的Sprite")]
public List<Sprite> mainSprite;
// key value
public static Dictionary<string ,Sprite> mainDic = new Dictionary<string, Sprite>();
void Start () {
for (int i = 0; i < mainSprite.Count; i++) {
mainDic.Add(mainSprite[i].name,mainSprite[i]);
}
}
}
加载
using UnityEngine;
using System.Collections;
public class RunScript : MonoBehaviour {
// public static readonly string PathURL ="file://" + Application.dataPath + "/StreamingAssets/";
public static readonly string PathURL =
#if UNITY_ANDROID
"jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
"file://" + Application.dataPath + "/StreamingAssets/";
#else
string.Empty;
#endif
public SpriteRenderer spritemy;
void Update()
{
if (Input.GetMouseButton(1)) {
spritemy.sprite = TestSprite.mainDic["qizi_2"];
}
}
void OnGUI()
{
// 测试
if (GUILayout .Button("Main AssetBundle")) {
StartCoroutine(LoadMainGameObject(PathURL +"Speak.assetbundle"));
}
}
private IEnumerator LoadMainGameObject(string path)
{
WWW bundle = new WWW (path);
yield return bundle;
// 加载到游戏
yield return Instantiate (bundle.assetBundle.mainAsset);
bundle .assetBundle.Unload (false);
}
}