- Home /
What is an alternative to using System.IO Stream when building for webplayer ?
I am streaming data from in my Resources folder in my DLL.
Web Player does not handle System.IO do what is an alternative to System IO Stream ?
In this script I get an image resource from a dll.
It uses Stream, which seems to not be handles by web player ?? MonoCompatability
public static Texture2D Load(string resourceName)
{
// first try to load as local resource, in case not running dll version
// also lets you override dll resources locally for rapid iteration
Texture2D texture = (Texture2D)Resources.Load(resourceName);
if (texture != null)
{
Debug.Log("Loaded local resource: " + resourceName);
return texture;
}
// if unavailable, try assembly
Assembly myAssembly = Assembly.GetExecutingAssembly();
Stream myStream = myAssembly.GetManifestResourceStream(myAssembly.GetName().Name + ".Resources." + "monkey.jpg");
texture = new Texture2D(10, 10, TextureFormat.ARGB32, false);
texture.LoadImage(ReadToEnd(myStream));
if (texture == null)
{
Debug.LogError("Missing Dll resource: " + resourceName);
}
return texture;
}
I would then pipe myStream into a Stream
static byte[] ReadToEnd(Stream stream)
{
long originalPosition = stream.Position;
stream.Position = 0;
try
{
byte[] readBuffer = new byte[4096];
int totalBytesRead = 0;
int bytesRead;
while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
{
totalBytesRead += bytesRead;
if (totalBytesRead == readBuffer.Length)
{
int nextByte = stream.ReadByte();
if (nextByte != -1)
{
byte[] temp = new byte[readBuffer.Length * 2];
Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
readBuffer = temp;
totalBytesRead++;
}
}
}
byte[] buffer = readBuffer;
if (readBuffer.Length != totalBytesRead)
{
buffer = new byte[totalBytesRead];
Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
}
return buffer;
}
finally
{
stream.Position = originalPosition;
}
}
UPDATE TO PROBLEM
I removed to script from the dll , left the monkey.jpg image in the dll as an embedded resource and then made sure to include a dummy class in the dll which i call in a mono script which is placed on a gameobject so that that the dll gets built too when i build the game . then i realised that it was not the scrpt that was the problem but the image ...or its setting. I am not sure what to do here
Answer by DaiMangouDev · Jul 23, 2015 at 08:27 PM
The problem was not in the script , but in the Resource Designer script of the Project where the DLL is built.
When building for web and you are using a managed dll, not native; with your own embedded image resources . Webplayer does not support System.Drawing. So open the 'Resource Designer' script and ensure that any reference to System.Drawing is removed.
as shown in these pictures
replace Bitmap with Unity's Texture2D
Answer by Bunny83 · Jul 22, 2015 at 04:46 AM
@DMDev:
In a webbuild you can't access the packed assemblies of your game manually. Those are loaded by the webplayer and your scripts don't have any access to those files in memory. Even when you placed the dlls additionally next to your webbuild so you can load the dll file from your webspace, you still need a way to access the resource data in that dll.
It would make much more sense to place your images directly in your project and just use them. As alternative you can host your images on your webspace and load them with the WWW class.
Btw: The System.IO
namespace works fine in a webbuild. You just can't use any function that works with files. Simply anything that could potentially be a security problem is not allowed. Memory streams, stream readers / writers work just fine in a webbuild.
According to the Mono compatibility page "GetManifestResourceStream" should work in a webbuild as well. Do you get any compilation errors? If so which ones? Do you get any errors at runtime? Have you checked the logfile? Any runtime errors when you use your code? If so, which ones?
HI thanks a lot for the information . I too had noticed that many of the members under System.IO were in green so i was confused.
I get 2 build errors.
I can send you a example project if that is okay.
the first build error is "ArgumentException: The Assembly System.Drawing is referenced by Name of dll ('path to asset.dll'). But the dll is not allowed to be included or could not be found. UnityEditor.AssemblyHelper.AddReferencedAssembliesRecurse (System.String assemblyPath, System.Collections.Generic.List`1 alreadyFoundAssemblies, System.String[] allAssemblyPaths, System.String[] foldersToSearch, System.Collections.Generic.Dictionary`2 cache, BuildTarget target) (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/AssemblyHelper.cs:154) UnityEditor.AssemblyHelper.AddReferencedAssembliesRecurse (System.String assemblyPath, System.Collections.Generic.List`1 alreadyFoundAssemblies, System.String[] allAssemblyPaths, System.String[] foldersToSearch, System.Collections.Generic.Dictionary`2 cache, BuildTarget target) (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/AssemblyHelper.cs:160) UnityEditor.AssemblyHelper.FindAssembliesReferencedBy (System.String[] paths, System.String[] foldersToSearch, BuildTarget target) (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/AssemblyHelper.cs:192) UnityEditor.HostView:OnGUI()"
the second build error is "Error building Player: Extracting referenced dlls failed."
from the errors I assumed that I am using some member that is not compatible with eeb player so i checked out the mono compatibility page.
I have traced the issue back to the monkey.jpg image itself.
@Bunny83 , I do not fully understand these settings . but when it is set to Embedded Resource and I try to build out the game I get an error .
I removed to script from the dll , left the monkey.jpg image in the dll as an embedded resource and then made sure to include a dummy class in the dll which i call in a mono script which is placed on a gameobject so that that the dll gets built too when i build the game . then i realised that it was not the scrpt that was the problem but the image ...or its setting. I am not sure what to do here
In a webbuild you can't use the "System.Drawing" library as it contains code that work directly with files. Assemblies with such code can't be included. Unity has a special mscorlib (which contains most of the System.IO namespace) for each platform. The webplayer version literally doesn't have those "banned classes / methods".
What do you actually use from the "System.Drawing.dll"? Are you sure you need that dll? You should remove references to other assemblies you don't need / use. So check the references list in your DLL project.
Besides that I still don't understand why you embed that image in a .NET assembly? If the assembly is in your project it will get packed into your built project. Why don't you add the image as seperate asset to your Unity project?
That is just it , I use nothing from System.Drawing in my code.
Oh, the Dll is used by an editor window also , that's why i embed the images.
I removed the System.Drawing Reference and tried to rebuild the dll and got a few errors that seem to points to the problem.
The Resource Designer was using System.Drawing ....
So I decided to replace Bitmap with Unity's Texture2D
Thanks for the help. Problem solved.
Answer by Positive7 · Jul 18, 2015 at 10:55 PM
Use WWW. System.IO is disabled on webplayer for good.
okay great , can you tell me how i would go about strea$$anonymous$$g data from the dll using WWW , I have been trying but i have made no progress.
Thanks for the help, It seems that only some members of System.IO cant be run in web
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Builds Crashing with "UnityPlayer.dll caused an Access Violation (0xc0000005)" 2 Answers
How do you save the Audio Mixer once changed? 0 Answers
Multiple Cars not working 1 Answer
System.Net.Mail For Web Player 0 Answers