2D Toolkit Forum
2D Toolkit => Support => Topic started by: ryu@actkey.jp on January 20, 2014, 08:03:05 am
-
My game's sources contain a lot of game object that is attached by tk2dsprite.
Recently, the size of my game's sources becomes too large.
I decided to make tk2dSpriteCollectionData to assetBundle.
Because game object contains a lot of scripts, and game object is complicated.
My idea of development's order is as follows:
-----------------------------------
(1) i create game project.
(2) i export tk2dSpriteCollectionData Directory as assetBundle.
(3) compress textures maximally.(texture is dirty .)
(4) When the game started, the game load assetBundel and into tk2dSpriteCollection.
-----------------------------------
I succeeded to make assetBundle by spriteCollectionData Directory.
I succeeded to load assetBundel and into tk2dSprite.
This is a sample code.
-----------------------------------
string tk2dSpriteCollectionName = "xxxxxxx"
int version = 1;
string os = "Android";
while (!Caching.ready) yield return 0;
string url = "http://xxxxxxxxxx.com/AssetBundle/" + tk2dSpriteCollectionName + "Data." + os + ".unity3d";
using(WWW www = WWW.LoadFromCacheOrDownload(url, version)){
yield return www;
if (www.error != null){
Debug.Log("error :" + www.error);
yield break;
}
AssetBundle bundle = www.assetBundle;
tk2dSpriteCollectionData d = typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData);
bundle.Unload(false);
bundle = null;
//reset tk2dSprite
foreach(GameObject g in manyGameObjects){
tk2dSprite s = g.GetComponent<tk2dSprite>();
string spritename = s.Collection.spriteDefinitions[s.spriteId].name;
s.Collection = d;
s.SetSprite(spritename);
s.Build();
}
-----------------------------------
But there is a problem.
(1) new tk2dSpriteCollectionData (from AssetBundel) is, delete every time the scene changes. I wonder can not save?
(2) new tk2dSpriteCollection (from AssetBundle) 's name is "-". why?
(3) I don't want to put tk2dSpriteCollectionData into tk2dSpriteCollection by loading assetBundel whenever the scene changed.
Every time, I don't want to rewrite a lot of GameObject's tk2dSprite as follows:
-----------------
foreach(GameObject g in manyGameObjects){
tk2dSprite s = g.GetComponent<tk2dSprite>();
string spritename = s.Collection.spriteDefinitions[s.spriteId].name;
s.Collection = d;
s.SetSprite(spritename);
s.Build();
}
-----------------
For example,make assetBundle by Material and Textuer, and replace tk2dSpriteCollectionData existing?
Please tell me someone or more.
-
If you don't have any sprites using an asset bundle, then the sprite collection data will get destroyed when you change scenes. This has to do with object lifetime in Unity and nothing to do with tk2d as far as I can tell. You can create game object with a public tk2dSpriteCollectionData object and assign it there to save it, or use hideFlags to make sure it doesn't get deleted, but you will have full responsibility over it at that point.
1. You don't need the tk2dSpriteCollection object at all. The only thing required is the tk2dSpriteColectionData object. The Sprite Collection object contains references to the source images... You can also create your sprite prefab for each collection and put that in an asset bundle, if that will work better for you.
2. The name being "-" indicates that it isn't in your project. The drop down only shows sprite collections that exist in your project.
-
Would you tell me a specific example?
I did the following processes in detail.
####################
## create SampleSpriteCollectionData
####################
1 i create tk2dSpriteCollection. example name is "SampleSpriteCollection"
2 I pressed the "OpenEditor..." button, and I added images , and committed.
3 Then, the folder that named "SampleSpriteCollection Data" was created by automatic.
####################
## make assetBundel
####################
4 I selected "SampleSpriteCollection Data" folder and did ExportAssetBundle.
--sample code--
using UnityEngine;
using UnityEditor;
//ref by http://shibomb.1108tones.com/archives/1885
public class ExportAssetBundles {
[MenuItem("Assets/Build AssetBundle From Selection - Track dependencies - MobilePlayer")]
static void ExportResourceToMobilePlayer () {
// Bring up save panel
string path = EditorUtility.SaveFilePanel ("Save Resource", "", Selection.activeObject.name, "");
Debug.Log(path);
if (path.Length != 0) {
// Build the resource file from the active selection.
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path + ".unity3d", BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets,
BuildTarget.WebPlayer);
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path + ".iPhone.unity3d", BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets,
BuildTarget.iPhone);
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path + ".Android.unity3d", BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets,
BuildTarget.Android);
Selection.objects = selection;
}
}
}
----
5 I compressed atlas images maximally.
Here is core of my idea.
When the program is executed, switched from "dirty atlas SpriteCollection in project" to "clean atlas SpriteCollection that load from assetBundle".
i think, "dirty atlas SpriteCollection in project" is Proxy
####################
## load assetBundel
####################
6 The assetBundle loaded as tk2dSpriteCollectionData type.
--sample code--
tk2dSpriteCollectionData d = bundle.Load("SampleSpriteCollection", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
----
####################
## reset sprite
####################
7 listup game objects.
--sample code--
List<GameObject> UIs = new List<GameObject>();
List<GameObject> Stage = new List<GameObject>();
List<GameObject> items = new List<GameObject>();
public GameObject StagePrefab; //it is PreFab
public void search(){
addList(list,GameObject.Find("Button1"));
addList(list,GameObject.Find("Button2"));
findchild(StagePrefab);
foreach(GameObject g inGameObject. FindGameObjectsWithTag("staticItem")){
addList(items,g);
}
}
public void findchild(GameObject g){
foreach( Transform child in g.transform)
{
addList(Stage,child.gameObject);
findchild(child);
}
}
public void addList(List<GameObject> l, GameObject g){
tk2dSprite s = g.GetComponent<tk2dSprite>();
if(s.Collection.name != "SampleSpriteCollection"){
return;
}
l.add(g);
return;
}
-----
8 It rewrite all of tk2dSpriteCollecttion gameObjects that listed.
--sample code--
foreach(GameObject g in list1){
if(s.Collection.name != "SampleSpriteCollection"){
continue;
}
tk2dSprite s = g.GetComponent<tk2dSprite>();
string spritename = s.Collection.spriteDefinitions[s.spriteId].name;
s.Collection = d;
s.SetSprite(spritename);
s.Build();
}
foreach(GameObject g in list2){
.....
-----
9 Game start!
In particular, No.8 and No.7, is slow by generating for each scene.
Increasing processes for each type of SpriteCollection is slow too.
If it can replace only the "dirty atlas images of "SampleSpriteCollection" that are present in my project and I don't need to do No.8, I'm glad.
Please tell me if there is an other recommended development method with using assetBundle.
-
What are you trying to do exactly with asset bundles? Are you trying to put platform sprite collections into asset bundles?
-
I'm sorry, my English is poor.
I think because it trial and error, that it is not able to try well.
So I have a question here.
So I thought I want to teach more detail process.
I wrote how to develop tk2d using assetBundel I've thought.
I wonder if would be helpful of the things I want to tell something. I was considered.
-
If you want to put a sprite collection into an asset bundle that is easy, but step 7 & 8 makes it look like you want to swap sprites that are already in the scene?
-
> If you want to put a sprite collection into an asset bundle that is easy, but step 7 & 8 makes it look like you want to swap sprites that are already in the scene?
yes.
I could not imagine the only way to swap.
You mean, are you processing as described below?
----
1 make game object prefab
items(empty gameObject)
+--item1(gameobject / SampleSpriteCollection / spriteName = "red_sword")
+--item2(gameobject / SampleSpriteCollection / spriteName = "green_box")
+--ButtonAttack(gameobject / UISpriteCollection / spriteName = "attackButton")
Save as Assets/Resources/items.prefab
2 make assetBundle
Assets/Resources/items.prefab
Assets/Sprite/SampleSpriteCollection Data
Exporting to assetBundle by selecting the above-mentioned two
3 load assetBundle
and only Instantiate items.prefab
I was understanding as described above. Correct?
I'm worried IOS or available in this way.
----
-
That will work fine on IOS.
If your prefab includes sprites, then you don't need to add them to the asset bundle explicitly.
Eg.
BuildPipeline.BuildAssetBundle(ItemsGameObject, new Object[] { ItemsGameObject }, path, BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.CollectDependencies);
will automatically include the necessary sprite collections
-
ahhhhhhh,,,,,,,i am fool,,,,,,,,,
I was able to finally understand.
only prefab export assetBundle , can display tk2dSprite!!!!
but, tk2dSpriteAnimator is not work.
can tk2dSpriteAnimator include AssetBundle ???
-
Yes tk2dSpriteAnimator should work fine too, just include what you need as the main object.
-
thank you so much!!!
i can make assetBundle and execute!
sprite ok, animation ok!