So basically I have assetbundle I’m able to load and place my scriptableobject (TestAsset) into my list variable but non of the variables of scriptableobject follow it. I know currently looks very basic and other methods can be used however my scriptableobject will be made more complicated once this issue is resolved.
_
I am going to place all my scripts bellow and all requirements needed for whats I’m getting.
_
You need following.
_
- StreamingAssets folder under Assets
- AssetBundle folder inside StreamingAssets
- AssetBundle folder under Assets
- Create TestAsset and assign it proper bundle via SetBundle
- Drag and drop complete assetbundle from Assets/AssetBundle to Assets/StreamingAssets/Assetbundle
_
Assets.CS
_
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
public class Assets : MonoBehaviour
{
public List<TestAsset> Items;
IEnumerator Start()
{
var bundleLoadRequest = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + "/AssetBundles/" + "items.primal");
yield return bundleLoadRequest;
var myLoadedAssetBundle = bundleLoadRequest.assetBundle;
if(myLoadedAssetBundle == null)
{
Debug.Log("Failed to load AssetBundle! from" + " " + bundleLoadRequest.ToString());
yield break;
}
else
{
UnityEngine.Object[] assetLoadRequest = myLoadedAssetBundle.LoadAllAssets<UnityEngine.Object>();
yield return assetLoadRequest;
foreach (UnityEngine.Object g in assetLoadRequest)
{
TestAsset AT = new TestAsset();
AT.name = g.name;
Items.Add(AT);
}
}
}
}
_
TestAsset.CS
_
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CreateAssetMenu(fileName = "Test", menuName = "Test/Asset")]
public class TestAsset : ScriptableObject
{
public int ID;
public string Name;
}
_
CreateAssetBundles.CS place inside Editor folder.
_
using UnityEditor;
public class CreateAssetBundles
{
[MenuItem("Assets/Build AssetBundles")]
static void BuildAllAssetBundles()
{
BuildPipeline.BuildAssetBundles("Assets/AssetBundles", BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneWindows64);
}
[MenuItem("Assets/SetBundle")]
static void SetBundle()
{
string assetPath = AssetDatabase.GetAssetPath(Selection.activeInstanceID);
AssetImporter.GetAtPath(assetPath).SetAssetBundleNameAndVariant("items", "primal");
}
}
_
This results in my list having scriptableobject appear in my list however variables of scriptableobject are set to nothing. Example bellow.
_
Logs.ID = 0;
Logs.Name = "";