Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rhylvin2016 · Jul 15, 2016 at 12:02 PM · assetbundledllassemblytextassetbytes

Script Import AssetBundle: ClassDerivedFromMonoBehaviour?

I think I'm almost done with this Script that Includes scripts in assetbundle.

I'm using this documentation here: http://docs.unity3d.com/Manual/scriptsinassetbundles.html + all the answer/question I've read from here:

http://answers.unity3d.com/questions/259569/how-to-compile-script-to-include-it-to-assetbundle.html

http://answers.unity3d.com/questions/9688/dynamically-loading-scripts.html

this is my script:

 using UnityEngine;
 using System.Collections;
 
 public class BinaryImporter : MonoBehaviour {
 
     string url = "file:///C:/Users/rhylvin2016/Documents/Unity%20Folders/WWW/Assets/AssetBundles/bytetext";
     IEnumerator Start()
     {
         while (!Caching.ready)
             yield return null;
 
         Debug.Log("1");
         WWW www = WWW.LoadFromCacheOrDownload(url,1);
         if (www != null) { Debug.Log(www); }
         Debug.Log("2");
         yield return www;
 
         Debug.Log("3");
         AssetBundle bundle = www.assetBundle;
         if (bundle != null) { Debug.Log(bundle); }
         Debug.Log("4");
         TextAsset txt = bundle.LoadAsset("CameraMove", typeof(TextAsset)) as TextAsset;
 
         Debug.Log("5");
         var assembly = System.Reflection.Assembly.Load(txt.bytes);
         if (assembly != null)  { Debug.Log(assembly); }
         var type = assembly.GetType("Class1");
 
         Debug.Log("6"+assembly.GetType().Name);
         GameObject go = new GameObject();
         Debug.Log("7");
         go.AddComponent(type);
         Debug.Log("8");
     }
 }
 

I don't get an error rather a notification that's saying "AddComponent asking for invalid type UnityEngine.GameObject:AddComponent(Type)"

that line

  var type = assembly.GetType("Class1");

is probably the problem. "Class1" for me is the "ClassDerivedFromMonoBehaviour" in the documentation. "Class 1 is the class with MonoBehaviour in the dll that I created"..

to be more understandable I'm gonna tell you step by step what I did.

  1. Open Visual Studio and create new Class Library naming Assembly name CameraMove

  2. Change Target framework to 3.5, deleted references that isn't use.

  3. Deleted namespace and other using, added using UnityEngine

  4. made this script as the DLL

      public class Class1 : MonoBehaviour
         {
         public void Update()
         {
             Debug.Log("hey");
             //originally wanted to create a script that could Find the MainCamera and make it move, but I didn't get to it cause I got stuck with this problem
         }
     }
     
    

5.)Change Solution Configuration to Release and Built it.

6.)Change the built dll to .bytes

7)Created new Unity Project, made this script to create an AssetBuild Button

 using UnityEditor;
 
 public class CreateAssetBundles
 {
     [MenuItem("Assets/Build AssetBundles")]
     static void BuildAllAssetBundles()
     {
         BuildPipeline.BuildAssetBundles("Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
     }
 }

8)Created AssetBundles Folder, manually put the MoveCamera.bytes in Unity. and made its assetbundle name into bytetext

9)Built it and copied the byetext bundle directory and paste it in string url

In the documentation it says

 TextAsset txt = bundle.Load("myBinaryAsText", typeof(TextAsset)) as TextAsset;

so that's why I wrote "MoveCamera", seems to work, its not null. so probably on the right track...just don't know about that

 var type = assembly.GetType("MyClassDerivedFromMonoBehaviour");

Thanks in advance

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rhylvin2016 · Jul 15, 2016 at 11:31 AM 0
Share

@AngryAnt

@rohankad

@Bunny83

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AngryAnt · Jul 15, 2016 at 12:05 PM

So you get the expected name logged out in 6?

Just for kicks, could you change the GameObject construction line #30 to the following (removing the AddComponent step) and see if this affects the error you get logged?

 new GameObject ("Test", type);
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rhylvin2016 · Jul 17, 2016 at 09:22 AM 0
Share

i did what you said. but still the same. not an error but a notification saying

"AddComponent asking for invalid type UnityEngine.GameObject:.ctor(String, Type[])"

also I tried this.`new GameObject("Camera$$anonymous$$ove", assembly.GetType("Class1"));`

still the same, but when I change the assembly.GetType("any string") to assembly.GetType("Camera$$anonymous$$ove"), I get an error saying

"TypeLoadException: Could not load type 'System.Runtime.Versioning.TargetFrameworkAttribute' from assembly 'Camera$$anonymous$$ove'. System.$$anonymous$$onoCustomAttrs.GetCustomAttributesBase (ICustomAttributeProvider obj, System.Type attributeType)"

avatar image rhylvin2016 · Jul 17, 2016 at 10:35 AM 0
Share

I don't know if this is relevant or not but maybe my type is null, but I don't think it is null though anyway I found this comment:

"Got it!

var myType = Type.GetType(dbInfo.ScriptName);

was always returning NULL, because the class I was looking for is wrapped in a namespace. It's the same namespace the rest of the code is in, but as soon as I included the namespace in the GetType lookup it started finding a valid Type, and everything else is gravy.

Thanks for all of the help!!"

on the forum:

http://forum.unity3d.com/threads/generating-a-script-then-using-addcomponent-to-attach-it-to-a-gameobject.299685/

but I already deleted the default namespace

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using UnityEngine;
     public class Class1 : $$anonymous$$onoBehaviour
     {
     public void Update()
     {
         Debug.Log("hey");
     }
 }


avatar image
0

Answer by twinkle · Jul 22, 2016 at 06:07 PM

you can try “System.Type type =assembly.GetType("Class1");”

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image philmzo · Nov 19, 2016 at 02:59 PM 0
Share

Thanks @rhylvin2016 and @twinkle!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

BadImageFormatException: Include Script using AssetBundle 1 Answer

in Unity 5, how can I get/set an asset's AssetBundle assignment via scripting? 1 Answer

How to auto find a component inside an assembly (dll)? 0 Answers

Enumerators and pdb2mdb 1 Answer

Cross reference multiple DLLs through AssetBundles 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges