Search Unity

Material works in editor and only in standalone if just used in editor

Discussion in 'General Graphics' started by jmballantyne, Nov 25, 2014.

  1. jmballantyne

    jmballantyne

    Joined:
    Oct 21, 2014
    Posts:
    8
    I'm running into an issue with a basic material using the unlit transparent cutout. I have a script that is adding to the main camera under certain conditions. If I run the script in the editor everything works. If I then build after running in the editor everything works fine. However, if the script isn't run before a build it will not show up in the stand-alone version.

    The material is in the resource folder and loaded with Resources.Load()

    Am I missing something easy here?
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. jmballantyne

    jmballantyne

    Joined:
    Oct 21, 2014
    Posts:
    8
    okay, here's the basic code. Basically, has a score 0 -100 and the score comes from a plugin. Made it static in the example. The radial progress bar slowly increases in percentage until it reaches the actual score.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class MovementResultView : MonoBehaviour
    5. {
    6.     //Publically available properties.
    7.     public Texture2D            Background;
    8.     public Texture2D            scoreBackground;
    9.     public Material                scoreCutout;
    10.     public float                 presentationTime = 1.5f;
    11.    
    12.     //Internals
    13.     private float                scorePercent = 0.0f;
    14.     private Rect                scoringCenterLocation = new Rect( 674.5f, 878.5f, 105.0f, 105.0f );
    15.     private Rect                centerScoreCutoutLocation = new Rect( 618.0f, 822.0f, 218.0f, 218.0f );
    16.  
    17.     //tmp with fake data.
    18.     private float                score = 87.0f;
    19.    
    20.     /// <summary>
    21.     /// Start this instance.
    22.     /// </summary>
    23.     void Start ()
    24.     {      
    25.         //prepare textures/materials.
    26.         if ( Background == null )
    27.         {
    28.             Background = (Texture2D)Resources.Load( "v5.3/UI/MovementResult_Center", typeof(Texture2D) );
    29.         }
    30.         if ( scoreBackground == null )
    31.         {
    32.             scoreBackground = (Texture2D)Resources.Load( "v5.3/UI/MovementResult_ScoreBackground", typeof(Texture2D) );
    33.         }
    34.         if ( scoreCutout == null )
    35.         {
    36.             scoreCutout = (Material)Resources.Load( "Materials/MovementScore_CutoutMaterial", typeof(Material) );
    37.         }
    38.     }
    39.    
    40.     /// <summary>
    41.     /// Update this instance.
    42.     /// </summary>
    43.     void Update ()
    44.     {
    45.         if ( scorePercent < 1.0f )
    46.         {
    47.             scorePercent += Time.deltaTime / presentationTime;
    48.             if ( scorePercent >= 1.0f )
    49.             {
    50.                 scorePercent = 1.0f;
    51.             }
    52.         }
    53.     }
    54.    
    55.     /// <summary>
    56.     /// Raises the GU event.
    57.     /// </summary>
    58.     void OnGUI()
    59.     {
    60.         //Score cutout.
    61.         float scoreToShow = scorePercent * score;
    62.         if ( scoreToShow > 0.0f && Event.current.type.Equals(EventType.Repaint) )
    63.         {
    64.             scoreCutout.SetFloat( "_Cutoff", Mathf.InverseLerp( 0.0f, 100.0f, 100.0f - scoreToShow) );
    65.             Graphics.DrawTexture( centerScoreCutoutLocation, scoreCutout.mainTexture, scoreCutout );
    66.         }
    67.        
    68.         //Score background.
    69.         GUI.DrawTexture( scoringCenterLocation, scoreBackground, ScaleMode.StretchToFill );
    70.     }
    71.    
    72.     /// <summary>
    73.     /// Raises the disable event.
    74.     /// </summary>
    75.     void OnDisable()
    76.     {
    77.        
    78.     }
    79. }
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Why are using making e.g. Background a public variable? Do you have a good reason to do so? Do you have the texture assigned in the inspector or is the field empty?
     
  5. jmballantyne

    jmballantyne

    Joined:
    Oct 21, 2014
    Posts:
    8
    i made it public so when I perform tests to ensure it is working, I can set it in the editor. In the real application the script is loaded via Camera.main.gameObject.AddComponent.
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Did you place some Debug.Logs to see whether the loading of the resources is successful?
    In which folder did you place those resource files?
     
  7. jmballantyne

    jmballantyne

    Joined:
    Oct 21, 2014
    Posts:
    8
    in my main directory, I have a folder called Resources and then the folder structure follows as with the code. The main thing is that if I run it in the build directory everything works fine, no issues. If I then create an installer and install the software on a different machine, the cutout material no longer displays.
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I can't see an issue at the moment. Did you try to create a debug build and have some Debug.Logs to check whether the resource load is called and if something was assigned.
     
  9. Mistale

    Mistale

    Joined:
    Apr 18, 2012
    Posts:
    173
    Are you using the shader (unlit transparent cutout) for anything else?
    I'm guessing that it might be stripped from the build, if it's not used in the scene.

    It's still strange, since you dont have [ExecuteInEditMode] in your script (which would explain it since then the public Material is indeed serialized in the scene).
    But perhaps you had that flag turned on before, when it worked?

    For shaders that are only used at runtime, add them to Edit -> Project settings -> Graphics, in the "Always Included Shaders" to make sure they are included even when there are no scene dependencies.
     
  10. jmballantyne

    jmballantyne

    Joined:
    Oct 21, 2014
    Posts:
    8
    sorry for the delay and thanks for the replies. I've added log statements and it seems that all the resources are being loaded as I am not getting any null objects. Not using this shader for anything else at the moment, just this one script.

    i have added the Unlit/Transparent Cutout to the Graphics settings ("Always included shaders").

    I'm still experiencing the same issue that it runs in the editor but not after an install. Bizarre and frustrating
     
  11. jmballantyne

    jmballantyne

    Joined:
    Oct 21, 2014
    Posts:
    8
    After some more testing it seems odd. I create a dummy object in the scene that uses the same shader. This works properly after an install, however, the Graphics.DrawTexture() call draws nothing.

    Everything works correctly in playmode and in the build folder, however, it doesn't work after an install. Driving me a bit nuts.
     
  12. jmballantyne

    jmballantyne

    Joined:
    Oct 21, 2014
    Posts:
    8
    okay, finally sorted and it wasn't unity at all. I have a small program that launches to check for updates which then launches the main application. I was using ShellExecute to launch, as soon as I switched to CreateProcess, everything is working perfectly.