Unity3 b3 Trouble with native mono libraries (Solved)

I just got my Unity3 beta 3 today and have no intention of muddying the forums with Unity 3 bug report-ish posts (it’s not supported after all).

I’m experiencing rather strange and show-stopping errors in the beta and was wondering if I was just doing something wrong or if I should take action (like file a bug report or something).

In scripts which worked in 2.6, I am now receiving the following errors:
‘System.Runtime.InteropServices.DllImportAttribute’ does not contain a constructor that takes ‘1’ arguments,
and ‘System.IO.File’ does not contain a definition for ‘WriteAllBytes’
As [DllImport(“pluginName”)] is even in the Unity 3 documentation as the correct way to designate a function to import from a plugin and WriteAllBytes is in the Unity 3 documentation for EncodeToPNG and my code did not change, I can only assume it is something that has changed with 3.0b.

Win7 x64 w/ Unity 3.0.0b3

Did I do something wrong and how do I fix this? Or is this a Unity 3 b3 bug that has not been mentioned anywhere that I’ve read and should I file a bug report?

Thanks for any advice/answers.

EDIT: The problem was that the build target was set to Web Player. Changing the build target to Standalone seems to have resolved the issues.

With U3 b3 the mono version was finally updated so things can change and the docs are not needfully all up to date.

optimally you would look into the mono docs at get-mono or alternatively into the C# docs at msdn

Fair enough. I understood that the mono version was changed and that the in-progress docs are not to be trusted.

I also checked the mono docs as well at: Redirecting… and:
http://www.go-mono.com/docs/monodoc.ashx?link=C%3ASystem.Runtime.InteropServices.DllImportAttribute(System.String)
and DllImport which is pretty standard does indeed have a constructor that takes 1 argument.

The documentation for File.WriteAllBytes is not yet complete in the mono docs, hence the question of what I am doing wrong. If no such function exists anymore, how do I write a PNG file for example?

According the MSDN docs the function exists and I doubt that Mono would break with it ( File.WriteAllBytes Method (System.IO) | Microsoft Learn )

I assume you have using **** etc in for the stuff, not just System.IO.File.WriteAllBytes, right?

have not transfered code in relation to such aspects yet so this is primarily “guess”

Obviously I have the proper includes (I would in stead be getting the error “The name ‘File’ does not exist in the current context”), but technically the using statement is unnecessary as I could just as easily fully qualify the namespace like

System.IO.File.WriteAllBytes("foo.bar", byteArray);

and the same error would occur. Other functions in the same class and namespace still work like

System.IO.File.Exists("foo.bar");

which indicates that it is just this and maybe certain other functions.

Here’s some quick test code that produces the errors:

WriteAllBytesTest.cs

using UnityEngine;
using System.Collections;
using System.IO;

public class WriteAllBytesTest : MonoBehaviour
{
    void Start()
    {
        StartCoroutine("WritePNG");
    }

    IEnumerator WritePNG()
    {
        // We should only read the screen buffer after rendering is complete
        yield return new WaitForEndOfFrame();

        // Create a texture the size of the screen, RGB24 format
        int width = Screen.width;
        int height = Screen.height;
        Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

        // Read screen contents into the texture
        tex.ReadPixels(new Rect(0, 0, width, height), 0, 0, false);
        //tex.Apply(); //Needed? setPixels not called and it works without Apply.

        // Encode texture into PNG
        byte[] bytes = tex.EncodeToPNG();
        Destroy(tex);

        // Write to a file in the project folder
        File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
    }
}

DllTest.cpp which compiles to DllTest.dll in the Plugins directory

#if _MSC_VER
#define EXPORT_API __declspec(dllexport) // Visual Studio needs annotating exported functions with this
#endif //_MSC_VER

//Wrap in C to prevent function name mangling
extern "C" {
	int EXPORT_API Increment(int number)
	{
		return number++;
	}
} //extern "C"

and DllImportTest.cs

using UnityEngine;
using System.Runtime.InteropServices; 

public class DllImportTest : MonoBehaviour {
    //Increments a number
    [DllImport("DllTest")]
    static extern int Increment(int number);

    void Start () {
        int value = 0;
        value = Increment(value);

        // Should output '1'
        Debug.Log(value);
    }
}

EDIT: Refocus to the topic

But I think I’m digressing so I’ll restate the original question: What should I do? Is there something that I can/should do to fix or get around this? Or should I submit a bug report or something else?

I submitted this as a bug because I’m getting the same problem as well.

You guys have U3, i hope it releases soon… :roll:

U3 b3 pro, and I see the following:
The type System.Runtime.InteropServices.DllImportAttribute' does not contain a constructor that takes 1’ arguments

Same problem for me importing UniWii.boundle :frowning: with U3 b3

FWIW, I got this exact error when doing a DllImport test, and it cleared up when I switched build targets from Web to Android.

on the web you can’t use native code plugins, so no InteropServices at all (nor system.environment for example or file commands). and no using managed c++ does not change it, no p/invoke, independent of how you try to fake / mask it

@skovacs1

What target platform are you building for?

@iByte

Thank you. This is why I asked if I did something wrong and how to fix it. I believed I was building Windows Standalone because I had always been building Windows Standalone, but somewhere either during the upgrade to U3b or during some test that I had run along the way, my build target got set to Web Player.

Switching my build target to the correct one seems to have resolved the errors.

@dreamora

Thank you as well. You provided the same answer in a sense although I didn’t realize that my build target had been changed (whatever the reason) and thus didn’t put the two together.

Hi all,
Is there something new about this WriteAllBytes problem ?

I currently use Unity b7 and I got this problem when I call the BuildPipeline.BuildPlayer function.

Here is the error displayed by Unity :

Assets/Scripts/MonoBehaviors/***.cs(129,14): error CS0117: System.IO.File' does not contain a definition for WriteAllText’

Error building Player because scripts had compiler errors
UnityEditor.BuildPipeline:BuildPlayer(String[ ], String, BuildTarget, BuildOptions)
AssetBundlesManager:BuildAssetBundles() (at Assets/Editor/Prod/AssetBundlesManager.cs:535)

By the way, WriteAllLines and WriteAllText also triggers this error.

The weird thing is the compiler don’t show up in editor mode, this only occurs when I call BuildPipeline:BuildPlayer.

EDIT:
It seems File.WriteAllBytes is not allowed in web player. But our main build will be a standalone build, with additive scenes streamed when the player moves in the world. Hence I have to build our additive scenes with a WebPlayer target and with BuildAdditionalStreamedScenes build options. I also tried to build WebPlayerStreamed, the same error occurs.

Mine just started bombing one day between builds on stuff that had been untouched for a while. There were errors like what was listed above but was also complaining about System.Collections.ObjectModel.Collection.Items not being a property that was accessible. I checked my build settings and it was set to Windows standalone like it always is but I reset it to the same thing (it scanned for a while) and the errors magically went away. That is so very suspicious.

So I just hit the System.IO.File' does not contain a definition for WriteAllText’ out of the blue.

whats the deal with that function?

I can do this longer version instead with System.IO:

var fs : FileStream = FileStream(path, FileMode.Create);
var w : BinaryWriter = BinaryWriter(fs);
w.Write(bytes);
w.Close();
fs.Close();

@AndyZ: Did you happen to build any asset bundles from a script or otherwise touch the build pipeline through an automated build script?

Yea i’ve had that happen also, building assetbundles automatically changes the Build Settings from Standalone → WebPlayer… its kind of rough, I managed to fast exit unity several times before i figured that one out.

I have started a thread here to talk about the occurrence being related to building asset bundles.