Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Enable or disable shadows and set shadow quality at runtime?

Discussion in 'Editor & General Support' started by Krileon, May 13, 2013.

Thread Status:
Not open for further replies.
  1. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    642
    Any idea how to enable/disable shadows and set shadow quality (high res, etc..) at runtime? Such settings don't seam to be in QualitySettings and can only change projection, cascades, and distance. I'm working on ingame options to adjust graphic settings and it's working fine for many of the other settings.
     
  2. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    642
    No one have any idea? I'm also looking into setting soft particles at runtime (graphic options for it ingame). Is there any plans for this to be improved with future Unity releases? It's pretty critical for a PC game to be able to control various graphic options to offer best performance for a wide range of computers.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    All the quality level variables are listed in the docs under QualitySettings. The specific shadow settings for each light are listed in the docs under Light.

    --Eric
     
    BeefyMcWhatnow likes this.
  4. FlaxSycle

    FlaxSycle

    Joined:
    Oct 6, 2012
    Posts:
    232
    I was looking for a way to do this a while back, as far as I can tell, there's no script interface that is equivalent to changing the Shadows dropdown box in Quality Settings.

    In my case I was able to work around it by customising my graphic settings to the various shadow configurations I wanted then using QualitySettings.SetQualityLevel to switch at runtime. Not sure if that will help in your case though...
     
  5. Krileon

    Krileon

    Joined:
    Oct 30, 2012
    Posts:
    642
    Yes, I am quite aware of this and the 3 options I listed and want to change are not there.

    I want to change shadows globally, not on individual lights.

    Yeah, that's what I figured I'd have to do. Not finding a setting for soft particles either unfortunately. My plan is to set all the quality levels to be exactly the same then have them each with different shadow settings, change the quality level when the shadow option is changed, then change the other individual quality settings after the quality level is changed.

    I hope in future releases Unity will give us more script access to these settings. It's very nice for players to be able to adjust settings for optimal performance for their PC.
     
    Last edited: May 14, 2013
  6. shkar-noori

    shkar-noori

    Joined:
    Jun 10, 2013
    Posts:
    833
    Still wanting to do this, I'm already using the quality setting customization for each shadow quality and type exp {HardShadowsLowRes , HardShadowsMedRes,SoftShadowsHighRes}... , and the per light setting is a no-go for me , but even an explanation for why won't they expose this would be good.

    and yes I'm aware that the thread is 2 years old.
     
  7. Philippe Vincenti

    Philippe Vincenti

    Joined:
    Mar 17, 2015
    Posts:
    2
    Got the same problem, wanted to add advanced quality settings customization in a project, but this is kind of a problem... What I did was kinda hacky, I just set the shadow max distance to 0. It probably doesn't avoid all the shadow drawing processes but at least it's something.
     
  8. Mikea15

    Mikea15

    Joined:
    Sep 20, 2012
    Posts:
    93
    You can always find the Main Directional light and set its shadow type to none? ;)
     
    Fisherama likes this.
  9. Draco-Nared

    Draco-Nared

    Joined:
    Jan 29, 2014
    Posts:
    18
    There is a ticket for this. https://feedback.unity3d.com/suggestions/allow-shadow-quality-to-be-changed-at-runtime-via-scripts
    I think it is really important to have it in Unity. I think it is one of the core features that should be able to be changed at runtime via script. It is strange that Unity does not have it (it has been on the internet for over 2 years!) as it is a standard in games to be able to change shadow quality.

    I know there are at least two workarounds. But I think not everybody wants to do it around.
     
    StaffanEk likes this.
  10. Alabatross

    Alabatross

    Joined:
    Dec 15, 2012
    Posts:
    223
    There is really no way to do this that is actually practical?

    Nevermind, I just duplicated my same quality level 4 times and only change the resolution since everything else is through a custom menu
     
    Last edited: Jan 14, 2016
    Stardog and shkar-noori like this.
  11. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    In case anyone needs a solution and doesn't want to use quality levels, one relatively clean way of doing this is to persistently store the in-game shadow quality setting and reference it in Start() for all light objects in a simple script.

    I agree that this shouldn't be necessary, though.
     
  12. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,533
    This is so dumb... was trying to found an api setting to do this....

    It's funny you can set all of these via code using the QualitySettings.<>
    shadowProjection: 1
    shadowCascades: 1
    shadowDistance: 15
    shadowNearPlaneOffset: 2
    shadowCascade2Split: 0.33333334
    shadowCascade4Split: {x: 0.06666667, y: 0.2, z: 0.46666667}

    yet this one...
    shadows: 0
    ...for completely disabling /hard/hard &soft.
    :(
    edit: You can't set ShadowResolution at runtime via code either.
     
    Last edited: May 28, 2016
  13. Raitoning

    Raitoning

    Joined:
    Dec 13, 2014
    Posts:
    100
    For me, the lack of possibility to enable/disable shadow rendering at runtime is maybe one of the biggest problem you can find in Unity. For a past game I had to choose between no shadow at all or shadows. As I aim my game to be playable even in the lowest specs PC that can be build, having this lack of control is pretty annoying. I've finally choosed to put shadows, but on very low resolution, this way players got some ( ugly ) shadows that doesn't consume all their PC ressources.
     
  14. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    168
    Technically, it's possible to change the Shadow's to OFF/HardOnly/HardSoft by using the QualityLevel as a base and, then, overwriting anything that we want to change.

    I know it's not the best kind of temporary/possible solution, but it's there. If you're offering the player a in-game option menu (which you mostly should anyway), the old (and lacking) Quality settings with the usual "Fantastic" to "Fastest" setting can easily be reduced to "ShadowsOFF" / "ShadowsHardOnly" / "ShadowsHardSoft" which, when the option are confirmed/set, all the remaining options gets overwritten manually by the script based on the players' preferences/stored options.

    In other words, make sure the Display Dialogue only contains the 3 Shadows type related option, make a UI menu for the option and the first thing you make the engine do every times before applying the changes to the exposed options is applying the new Quality Settings that includes the right shadows type of your choice. That's a available possibility as of now.

    I must warn everyone though... that from my experience, sometimes modifying the rendering assets such as texture or shadows doesn't give the best results unless the client is restarted. You might want to test it out and see if you don't get a load of glitchy behavior out of a forced change. ;)
     
  15. RYANxSCOUT

    RYANxSCOUT

    Joined:
    Jun 20, 2017
    Posts:
    1
    Bro! first I was also feeling really hard to disable it, but then i figure it out it's quite simple

    all you need us to put :

    QualitySettings.shadows = Shadows.Disable;

    Shadows.Disable = to disable shadows
    Shadows.HardOnly = Hard shadows only
    Shadows.All = Hard and Soft Shadows

    hope it helps you. Wish you all the best with your project.
     
    twobob and Daahrien like this.
  16. will54321

    will54321

    Joined:
    Aug 7, 2017
    Posts:
    4
    This code goes in the light.
    it changes the quality and resolution in real time as the player moves away from the lamp



    public Transform player;

    // Update is called once per frame
    void Update () {

    float dist = Vector3.Distance(this.transform.position, player.position);
    if (dist < 3f)
    {
    GetComponent<Light> ().shadows = LightShadows.Soft;
    GetComponent<Light> ().shadowResolution = UnityEngine.Rendering.LightShadowResolution.VeryHigh;

    }

    if (dist > 4f)
    {
    GetComponent<Light> ().shadows = LightShadows.Hard;
    GetComponent<Light> ().shadowResolution = UnityEngine.Rendering.LightShadowResolution.Low;
    }

    if (dist > 5f)
    {
    GetComponent<Light> ().shadows = LightShadows.None;

    }

    }
     
    Last edited: Oct 7, 2017
Thread Status:
Not open for further replies.