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*.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.

It’s not as simple as you try to make it look unfortunately :slight_smile:

Check this answer:

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

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);