So I have an Asset Bundle file that includes 3D model and Icon sprite which i want to use as an button in the item list.
I would like to ask if it's possible to pass the sprite object from Asset bundle to the UI button's source image. I never did this before, so just want to ask if it's possible.
If it's not possible then, what can i do instead?
I have a logic that loads the asset bundle as a list of items. and it represents as a button with default source image, just a rectangle. So i want to get access to the Asset bundle for retrieving the sprite image and passing it to button's source image.
Here's API logic file which is responsible for loading item list and assetbundle file from the server.
//-----------------------------------------------------------------------------------------------------------
public void LoadContent(string name, UnityAction<GameObject> callback)
{
api.GetBundleObject(name, callback, SpawnableFurnitureMother.transform);
}
//-----------------------------------------------------------------------------------------------------------
public void GetItemList(UnityAction<List<string>> callback)
{
StartCoroutine(GetItemListRoutine(callback));
}
//-----------------------------------------------------------------------------------------------------------
IEnumerator GetItemListRoutine(UnityAction<List<string>> callback)
{
UnityWebRequest www = UnityWebRequest.Get(ItemList);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError)
{
Debug.Log("Network error");
}
else
{
string rawText = www.downloadHandler.text;
//split string by comma
string[] items = rawText.Split(',');
//remove empty values and convert to list
List<string> itemList = items.Where(x => !string.IsNullOrEmpty(x)).ToList();
//return list to caller
callback.Invoke(itemList);
}
}
//-----------------------------------------------------------------------------------------------------------
public void GetBundleObject(string assetName, UnityAction<GameObject> callback, Transform bundleParent)
{
StartCoroutine(GetDisplayBundleRoutine(assetName, callback, bundleParent));
}
//-----------------------------------------------------------------------------------------------------------
IEnumerator GetDisplayBundleRoutine(string assetName, UnityAction<GameObject> callback, Transform bundleParent)
{
string bundleURL = BundleFolder + assetName + "-";
//append platform to asset bundle name
#if UNITY_ANDROID
bundleURL += "Android";
#else
bundleURL += "IOS";
#endif
Debug.Log("Requesting bundle at " + bundleURL);
//request asset bundle
UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(bundleURL);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError)
{
Debug.Log("Network error");
}
else
{
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
if (bundle != null)
{
string rootAssetPath = bundle.GetAllAssetNames()[0];
mySystem.AddToLog("Spawning");
GameObject arObject = Instantiate(bundle.LoadAsset(rootAssetPath) as GameObject, bundleParent);
bundle.Unload(false);
callback(arObject);
arObject.SetActive(false);
}
else
{
Debug.Log("Not a valid asset bundle");
}
}
}
Here's the logic that's instantiating the Button prefab that's assigned in the Inspector.
public void SpawnObject(List<string> itemList)
{
foreach (string item in itemList)
{
//load button prefab into scene
GameObject buttonObject = Instantiate(ButtonPrefab, ButtonParent);
//set button text
// buttonBehavior.LoadIcon(item, callback, ButtonParent);
buttonBehavior = buttonObject.GetComponent<ButtonBehavior>();
buttonBehavior.Init(item);
//set button action
Button uiButton = buttonObject.GetComponent<Button>();
// mySystem.AddToLog("Checking uiButton: " + (uiButton != null));
uiButton.onClick.AddListener(() =>
{
if (!ObjectWithIDExists(currentObjectID) && placementIndicator != null)
{
ActivateMenu(false);
LoadContent(item, (SpawnableFurnitureChild) =>
{
// logic for instantiating the 3D model itself
});
}
});
}
}
and here's the php file i don't know if we need it.
<?php
$folder = "AssetBundles/";
$mask = "*Android*";
$files = glob("" . $folder . $mask);
foreach ($files as $file) {
$file_name = basename($file,substr($mask,1));
$name = explode("-", $file_name);
echo $name[0].",";
}
?>
Found a solution, not the best thou, made the icon not the asset bundle but the sprite it self, and also cheking on the name of the file so it can find the file that should be represent the icon.