Using a generic C# open source library (LibNoise) in Unity

I am trying to use a small open source library called libnoise for generating (and modifying) procedural noise for textures, landscapes etc. The library is orginally written in c++, but it has been ported to C#/.NET. It’s a pretty small library so I figured it would be manageble to get it working in Unity.

Link: http://libnoisedotnet.codeplex.com/

Below is a simple noise modifier class from the library:

using System;
using System.Collections.Generic;
using System.Text;

namespace LibNoise.Modifiers
{
    public class Add
        : IModule
    {
        public IModule SourceModule1 { get; set; }
        public IModule SourceModule2 { get; set; }

        public Add(IModule sourceModule1, IModule sourceModule2)
        {
            if (sourceModule1 == null || sourceModule2 == null)
                throw new ArgumentNullException("Source modules must be provided.");

            SourceModule1 = sourceModule1;
            SourceModule2 = sourceModule2;
        }

        public double GetValue(double x, double y, double z)
        {
            if (SourceModule1 == null || SourceModule2 == null)
                throw new NullReferenceException("Source modules must be provided.");

            return SourceModule1.GetValue(x, y, z) + SourceModule2.GetValue(x, y, z);
        }
    }
}

I dragged all the files into the asset manager to see what would happen. I had to do a global search&replace on all the empty get/set in the code, but I wouldn’t expect that to be a problem.

  1. I have no idea how I would access this code. Doing something like “import Libnoise.Add;” doesn’t work. Is there a way to access non-MonoBehaviour classes? Or do I need to change their base classes to inherit from MonoBehaviour? (I’m not planning on attaching their code diretly on GameObjects - only as library calls from either UnityScript or C#).

  2. They obviously wrap all their code in namespaces - is this a problem for Unity? I remember reading somewhere about Unity not supporting C# namespaces.

The library is pretty small, so making modification to all the code is not out of the question - I just need to know what to change it into. :slight_smile:

They also provide a compiled .DLL, but I really see that as the last fallback option, since that would limit me to the Windows standalone platform.

I am using Unity Pro 2.x Trial, but expecting to upgrade to Unity Pro 3.0 later this week.

if you know the namespace, you do import namespace; in unityscript or using namespace; in C#

in this case it would be using LibNoise.Modifiers; for C# for example.

Also note that, if you’re putting the source files into your Unity project rather than compiling them into a DLL, but you want to reference them from Unityscript, the files will need to be in a directory that gets compiled before your Unityscript files – Standard Assets, Plugins, etc. See Script Compilation in the scripting reference for details.

that + using will be gone cause unity does not support namespaces for stuff it compiles itself, so the classes must have unique names independent of namespaces

That’s not been my experience dreamora; there are various restrictions on using namespaces (can’t .Instantiate() a namespaced class, can’t attach a namespaced MonoBehaviour to a game object etc…), but in general use they work fine.

Having dug the appropriate reference out from the wiki yesterday, I just wanted to come back and correct this assumption. Since it’s a pure .NET assembly with no un-managed/native code, the DLL can be used on any supported platform, including web players. c.f. the Extensions page on the wiki.

if its any depends on what functionality it uses.

each platform has different limitations so unless you remain within System, System.collections, System.Collections.Generic you will optimally have to test it