Question Unity assetboundle with objects with scripts?

Battou

New member
Messages
6
Reactions
2
Points
3
Is there any way to create assetboundle for VAM with objects using scripts attached to them?
 
Yes.

Checkout for instance my last release The White Room, it includes a script to drive the shader color of the room. You can also checkout my most recent particle systems, they all use a way to interact with the gameobject instance.

At the moment I have an issue to make it work with parented objects, but I'm trying to fix that : )
You cannot create assetbundles with embedded c# scripts on them. This is a limitation of the assetbundle system.

PS : don't forget to mark the question answered if it is.
 
Upvote 0
You cannot create assetbundles with embedded c# scripts on them. This is a limitation of the assetbundle system.
There is a way to load a DLL together with the asset bundle. However, so far I could not make it work for code that does have any references to VaM.

SecretRoom also uses VaM plugins to adjust things loaded by an CustomUnityAsset atom. Like changing material colors but also moving/rotating things.
 
Upvote 0
There is a way to load a DLL together with the asset bundle. However, so far I could not make it work for code that does have any references to VaM.

SecretRoom also uses VaM plugins to adjust things loaded by an CustomUnityAsset atom. Like changing material colors but also moving/rotating things.
Does objects that have scripts attached to them will retain their parameters values if script is loaded using DLL? For example I have 3 objects with the same script but different parameters. How do I do this in VAM?
 
Upvote 0
Does objects that have scripts attached to them will retain their parameters values if script is loaded using DLL? For example I have 3 objects with the same script but different parameters. How do I do this in VAM?

DLL is not the only solution... I haven't checked it because I can do mostly what I want with .cs files, so I can't tell you much about them.
But, for classical .cs scripts... I released a tutorial yesterday on the subject.
 
Upvote 0
Does objects that have scripts attached to them will retain their parameters values if script is loaded using DLL? For example I have 3 objects with the same script but different parameters. How do I do this in VAM?
  1. You use VisualStudio to setup a project that references Unity libraries. Remove project references to any C# reflection code, VisualStudio likes to add those automatically when creating the project. Create a MonoBehaviour with your code and compile the whole thing as DLL.
  2. Put the DLL into your Unity project and build a prefab or scene that uses your MonoBehaviour. Just as you would normally do in Unity.
  3. Export the prefab or scene as AssetBundle (do NOT include the DLL)
  4. Place the AssetBundle somewhere in your Custom\Assets folder.
  5. Place the DLL in the same folder with the same name as the AssetBundle, just with *.dll extension.
  6. Use a CustomUnityAsset atom to load the AssetBundle, VaM will auto-detect the DLL and load it.
Last time I tried this was over a year ago, but I think the limitations where this:
  • You DLL can't use UnityEditor code, just UnityEngine. At least I could not figure out how. Usually you would build a second DLL with the editor-only code, but it messes up your script references stored in your prefab/scene. This rules out much code you would just take from the Unity AssetStore, like various special effects, etc.
  • You can't reference VaM code. At least I could not figure out how.



Regarding finding a prefab spawned by a CustomUnityAsset from a plugin goes just like this:
C#:
        // Get spawned prefab from CustomUnityAsset atom. Note that these are loaded asynchronously,
        // this function returns null while the prefab is not yet there.
        public static GameObject GetCustomUnityAsset(Atom atom, string prefabName)
        {
            Transform t = atom.transform.Find("reParentObject/object/rescaleObject/"+prefabName+"(Clone)");
            if (t == null)
                return null;
            else
                return t.gameObject;
        }
You just have to be aware that it returns null while the AssetBundle is still loading. That's what the loop in @hazmhox tutorial is about. Make sure to store the result in a variable once you found it to avoid the constant polling.
 
Upvote 0
  1. You use VisualStudio to setup a project that references Unity libraries. Remove project references to any C# reflection code, VisualStudio likes to add those automatically when creating the project. Create a MonoBehaviour with your code and compile the whole thing as DLL.
  2. Put the DLL into your Unity project and build a prefab or scene that uses your MonoBehaviour. Just as you would normally do in Unity.
  3. Export the prefab or scene as AssetBundle (do NOT include the DLL)
  4. Place the AssetBundle somewhere in your Custom\Assets folder.
  5. Place the DLL in the same folder with the same name as the AssetBundle, just with *.dll extension.
  6. Use a CustomUnityAsset atom to load the AssetBundle, VaM will auto-detect the DLL and load it.
Last time I tried this was over a year ago, but I think the limitations where this:
  • You DLL can't use UnityEditor code, just UnityEngine. At least I could not figure out how. Usually you would build a second DLL with the editor-only code, but it messes up your script references stored in your prefab/scene. This rules out much code you would just take from the Unity AssetStore, like various special effects, etc.
  • You can't reference VaM code. At least I could not figure out how.



Regarding finding a prefab spawned by a CustomUnityAsset from a plugin goes just like this:
C#:
        // Get spawned prefab from CustomUnityAsset atom. Note that these are loaded asynchronously,
        // this function returns null while the prefab is not yet there.
        public static GameObject GetCustomUnityAsset(Atom atom, string prefabName)
        {
            Transform t = atom.transform.Find("reParentObject/object/rescaleObject/"+prefabName+"(Clone)");
            if (t == null)
                return null;
            else
                return t.gameObject;
        }
You just have to be aware that it returns null while the AssetBundle is still loading. That's what the loop in @hazmhox tutorial is about. Make sure to store the result in a variable once you found it to avoid the constant polling.
Great! Thanks! Will try.)
 
Upvote 0
  1. You use VisualStudio to setup a project that references Unity libraries. Remove project references to any C# reflection code, VisualStudio likes to add those automatically when creating the project. Create a MonoBehaviour with your code and compile the whole thing as DLL.
  2. Put the DLL into your Unity project and build a prefab or scene that uses your MonoBehaviour. Just as you would normally do in Unity.
  3. Export the prefab or scene as AssetBundle (do NOT include the DLL)
  4. Place the AssetBundle somewhere in your Custom\Assets folder.
  5. Place the DLL in the same folder with the same name as the AssetBundle, just with *.dll extension.
  6. Use a CustomUnityAsset atom to load the AssetBundle, VaM will auto-detect the DLL and load it.
Last time I tried this was over a year ago, but I think the limitations where this:
  • You DLL can't use UnityEditor code, just UnityEngine. At least I could not figure out how. Usually you would build a second DLL with the editor-only code, but it messes up your script references stored in your prefab/scene. This rules out much code you would just take from the Unity AssetStore, like various special effects, etc.
  • You can't reference VaM code. At least I could not figure out how.



Regarding finding a prefab spawned by a CustomUnityAsset from a plugin goes just like this:
C#:
        // Get spawned prefab from CustomUnityAsset atom. Note that these are loaded asynchronously,
        // this function returns null while the prefab is not yet there.
        public static GameObject GetCustomUnityAsset(Atom atom, string prefabName)
        {
            Transform t = atom.transform.Find("reParentObject/object/rescaleObject/"+prefabName+"(Clone)");
            if (t == null)
                return null;
            else
                return t.gameObject;
        }
You just have to be aware that it returns null while the AssetBundle is still loading. That's what the loop in @hazmhox tutorial is about. Make sure to store the result in a variable once you found it to avoid the constant polling.
So I have tried it. And I have a problem. VAM getting stuck at loading AssetBoundle, "Loading please wait" forever in CUA. If I uncheck load DLL then it loads, but without scripts. Here is my DLL code, it works in Unity.


C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace TestVamDLL
{
    
    public class RotateAround : MonoBehaviour
    {
        public float speed;

        void Update()
        {
            Rotate(speed);
        }

        public void Rotate(float speed)
        {
            transform.Rotate(Vector3.up, speed * Time.deltaTime);
        }


    }
}
 
Upvote 0
Random ideas:
  • Check Unity error log. Not all errors are shown inside VaM. Go to %appdata%\..\LocalLow\MeshedVR\VaM\output_log.txt
  • VisualStudio by default adds some AssemblyInfo.cs to the project. You need to remove that as VaM does not allow any references to Reflection stuff for security. (File IO is also forbidden)
  • Since you mention to be a C# coder, you could use ILSpy decompiler to open VaM_Data\Managed\Assembly-CSharp.dll, it contains VaM's source code. Best "VaM documentation" available. Specifically I recommend to have a look at CustomUnityAssetLoader.SyncAssetUrl as it contains the code to load your DLL. https://github.com/icsharpcode/ILSpy/releases
 
Upvote 0
Random ideas:
  • Check Unity error log. Not all errors are shown inside VaM. Go to %appdata%\..\LocalLow\MeshedVR\VaM\output_log.txt
  • VisualStudio by default adds some AssemblyInfo.cs to the project. You need to remove that as VaM does not allow any references to Reflection stuff for security. (File IO is also forbidden)
  • Since you mention to be a C# coder, you could use ILSpy decompiler to open VaM_Data\Managed\Assembly-CSharp.dll, it contains VaM's source code. Best "VaM documentation" available. Specifically I recommend to have a look at CustomUnityAssetLoader.SyncAssetUrl as it contains the code to load your DLL. https://github.com/icsharpcode/ILSpy/releases
Yeah, you are right, it was because of AssemblyInfo, forgot to check it. Thank you.)
 
Upvote 0
( don't forget to mark the thread as answered, it helps the other to find helpful content on the forum )
 
Upvote 0
Back
Top Bottom