Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[SOLVED] Editor Code not working when Web Player selected as build settings

Discussion in 'Scripting' started by jmunozar, Jul 2, 2014.

  1. jmunozar

    jmunozar

    Joined:
    Jun 23, 2008
    Posts:
    1,091
    Hey guys, maybe this has been solved already but I havent been able to find a solution for this :/.

    Basically I have an editor script, and this editor script creates a texture that i need to save in the project settings. something like:

    Code (csharp):
    1.  
    2. #if UNITY_EDITOR
    3.     byte[] bytes = canvas.EncodeToPNG();
    4.     File.WriteAllBytes(pathOfAtlas, bytes);
    5. #endif
    6.  
    when I run this on any platform it executes correctly, but when I set the build platform to web player I get this error:

    `System.IO.File' does not contain a definition for `WriteAllBytes'

    Using this:
    Code (csharp):
    1.  
    2. #if UNITY_EDITOR &&  !UNITY_WEBPLAYER
    3.         byte[] bytes = canvas.EncodeToPNG();
    4.         File.WriteAllBytes(pathOfAtlas, bytes);
    5. #endif
    6.  
    will of course not execute the line where I write the png in the project. any idea how to solve this?, how to create a texture in the project view running on an editor script when the web player is selected as build platform?

    Thanks guys :)
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    I think that's to be expected. When you are working in a web player build, Unity uses the web player version of Mono. Since the web player doesn't have access to the file system, neither does the script running in the web player. Note that script code inside the Editor folder executes with the less-protective version of Mono, so perhaps your code needs to be an editor script?
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I think his question was more why the UNITY_EDITOR directive is true when he builds a Web Player.
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    From what I understand, he didn't build anything yet and he is still in the Editor, so the UNITY_EDITOR is accurate.
    However, Unity loaded a Mono without System.IO.File to emulate the behaviour on a real build.
     
  5. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Was what I thought, also. If code inside a UNITY_EDITOR check runs inside a web player build, then that's a bug and needs to be reported.
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Ah yes - I misread that. Sorry!
     
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    If it's an editor script, why does it has the "#if UNITY_EDITOR" operator? It doesn't need that is your files are in the Editor folder.
     
  8. jmunozar

    jmunozar

    Joined:
    Jun 23, 2008
    Posts:
    1,091
    Good point, I was calling everything from an editor class inside the "editor" folder and the Core files where outside of the editor folder. Placed the "core" colder inside the editor folder and problem solved.

    Thanks! :)