Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 RobertIvaldi · Dec 23, 2013 at 06:48 PM · c#wwwmonobehaviourstreamingassetscast

Loading .cs classes from startup out of streamingAssets

I have a class called Block and a few classes extending it called Grass, Dirt, and Stone sitting in the streamingAssets folder. The idea is to allow players to "mod" per say by adding their own Blockname.cs and Blockname.png files into it.

The problem is that I cannot figure out just how to initialize the .cs files found in streamingAssets, or any folder for that matter. It has to work flexibly so when the game starts I can create an array of all the blocks and grab their variables.

The second portion, which I found much easier, was to find all the .png files with the same name and create an array of Texture2D. My code for grabbing and attempting to load the cs files are below.

 using UnityEngine;
 using System.Collections;
 using System.IO;
 
 public class Dimension : MonoBehaviour {
     public GameObject world;
     private WWW www;
 
     public IEnumerator grabFile(WWW path) {
         yield return path;  
     }
 
     // Use this for initialization
     void Start () {
 
         DirectoryInfo dir = new DirectoryInfo(Application.streamingAssetsPath);
         FileInfo[] info = dir.GetFiles("*.cs");
         for(int i=0; i<info.Length; i++) {
             string url = "file://"+info[i].FullName;
             print (url);
             www = new WWW(url);
             grabFile(www);
             MonoBehaviour tempScript = (MonoBehaviour) www;
         }
     }
 }

I stop here and test just to see if I can cast the cs file as a Monobehaviour object, but alas I cannot: Assets/Dimension.cs(23,68): error CS0030: Cannot convert type UnityEngine.WWW' to UnityEngine.MonoBehaviour'

How would I go about initializing classes found in streamingAssets? Like Block grass = new Grass() but only because Grass.cs was found in the folder? It does confuse me and I wonder if there is an easier way of doing what I'm trying to do.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Statement · Dec 23, 2013 at 07:47 PM

It's not as simple as you try to make it look unfortunately :)

Check this answer:

  • http://stackoverflow.com/questions/8018417/how-to-load-a-class-from-a-cs-file

But you may want to let users provide a built assembly which you load instead.

  • http://stackoverflow.com/questions/465488/can-i-load-a-net-assembly-at-runtime-and-instantiate-a-type-knowing-only-the-na

And some example from the above link to draw you into it:

 Assembly assembly = Assembly.LoadFrom("MyNice.dll");
 Type type = assembly.GetType("MyType");
 object instanceOfMyType = Activator.CreateInstance(type);
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 RobertIvaldi · Dec 23, 2013 at 08:19 PM 0
Share

Thank you, This does answer my question. I will either continue working this way or, if my classes stay variable-only, I might switch to reading text files at run time to compile a block list. :)

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

19 People are following this question.

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

Related Questions

How to load NavMesh from streaming assets at runtime 0 Answers

Unity can not access StreamingAssets on IOS 1 Answer

Localy load XML data from StreamingAssets folder 0 Answers

Loading an image from streamingassets fails every time on Android 1 Answer

StreamingAssets with OSX 3 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