- Home /
Loading Assetbundle scriptableobject settings. I can load the Scriptableobjects and place them in a list just fine but variables that are apart of it don't load.
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 = "";
Answer by darkal · Aug 15, 2019 at 01:06 AM
I figured out issue from friend who got back to me very simple fix
Under the Assets.CS
foreach (UnityEngine.Object g in assetLoadRequest)
{
TestAsset AT = new TestAsset();
AT.name = g.name;
Items.Add(AT);
}
TestAsset AT = new TestAsset(); needs be like so.
foreach (UnityEngine.Object g in assetLoadRequest)
{
TestAsset AT = g as TestAsset;
AT.name = g.name;
Items.Add(AT);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612210648im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why isn't my scriptable object storing correctly in the AssetBundle? 0 Answers
Load A complete unity game using other program 2 Answers
Find all assetbundles in folder 0 Answers
When building AssetBundles some scriptable objects get corrupted 2 Answers
Shader References seem broken for AssetBundle in HDRP Project 1 Answer