Unity - UGUI - Sprite 如何打包

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

}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值