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

Question How can i set shadows from PlayerPrefs?

Discussion in 'Universal Render Pipeline' started by fgsoftwarestudio, Aug 26, 2023.

  1. fgsoftwarestudio

    fgsoftwarestudio

    Joined:
    Jul 23, 2020
    Posts:
    6
    I'm actually saving shadows settings trough a boolean as integer on PlayerPrefs, like this:
    Code (CSharp):
    1. PlayerPrefs.SetInt("shadows", toggleShadows.isOn ? 1 : 0);
    toggleShadows is a toggle
    but i can't set it like this:
    Code (CSharp):
    1. sun.shadows = (LightShadows)PlayerPrefs.GetInt("shadows") == 1 ? true : false;
    sun is the main light
     
  2. vagelis199

    vagelis199

    Joined:
    Jul 27, 2012
    Posts:
    167
    Why are you not saving it as a boolean? PlayerPrefs.SetBool()
     
  3. fgsoftwarestudio

    fgsoftwarestudio

    Joined:
    Jul 23, 2020
    Posts:
    6
    there's no boolean on PlayerPrefs, for me at least
     
  4. vagelis199

    vagelis199

    Joined:
    Jul 27, 2012
    Posts:
    167
    Oh yeah, you're right, You may wanna use PlayerPrefsX which adds support for booleans, colors etc.
     
  5. fgsoftwarestudio

    fgsoftwarestudio

    Joined:
    Jul 23, 2020
    Posts:
    6
    but that won't work either though
     
  6. vagelis199

    vagelis199

    Joined:
    Jul 27, 2012
    Posts:
    167
    Is your sun assigned to a DirectionalLight?
     
  7. fgsoftwarestudio

    fgsoftwarestudio

    Joined:
    Jul 23, 2020
    Posts:
    6
    sun is actually the the main DirectionalLight
     
    Last edited: Aug 26, 2023
  8. vagelis199

    vagelis199

    Joined:
    Jul 27, 2012
    Posts:
    167
    The reason it's not working is because the light.shadows is not a bool but an enum "LightShadows" which has the options LightShadows.None, LightShadows.Hard , LightShadows.Soft,
    So you may wanna do this :
    Code (CSharp):
    1. sun.shadows = PlayerPrefs.GetInt("shadows") == 1 ? LightShadows.Soft : LightShadows.None;
     
  9. fgsoftwarestudio

    fgsoftwarestudio

    Joined:
    Jul 23, 2020
    Posts:
    6
    Fixed:
    Code (CSharp):
    1. sun.shadows = PlayerPrefsX.GetBool("shadows") == true ? LightShadows.Soft : LightShadows.None;
     
  10. vagelis199

    vagelis199

    Joined:
    Jul 27, 2012
    Posts:
    167
    On top go to Thread Tools, Edit title, and change the prefix to Resolved.