Save directionnal light color32 (byte to int32 and int32 to byte)

Hello everyone,

I want to save and load RGB color (not A) of my directionnal light with unity PlayerPrefs.

Example: if I leave the game with directionnal light color egal to 230 / 150 / 50, when I load the 3 int I have this number 1 / 1 / 0.

I think my problem is in the “load code” because I load the int before convert the int32 to byte… But I don’t know how to do that.

- This is my load code:

        DirectionalLightColorR = PlayerPrefs.GetInt("DirectionalLightColorR", DirectionalLightColorR);
        DirectionalLightColorG = PlayerPrefs.GetInt("DirectionalLightColorG", DirectionalLightColorG);
        DirectionalLightColorB = PlayerPrefs.GetInt("DirectionalLightColorB", DirectionalLightColorB);

        NightAndDayCycleScript.DirectionalLight.color = new Color32(Convert.ToByte(DirectionalLightColorR), Convert.ToByte(DirectionalLightColorG), Convert.ToByte(DirectionalLightColorB), 255);

- This is my save code:

        DirectionalLightColorR = Convert.ToInt32(NightAndDayCycleScript.DirectionalLight.color.r);
        DirectionalLightColorG = Convert.ToInt32(NightAndDayCycleScript.DirectionalLight.color.g);
        DirectionalLightColorB = Convert.ToInt32(NightAndDayCycleScript.DirectionalLight.color.b);

            PlayerPrefs.SetInt ("DirectionalLightColorR", DirectionalLightColorR);
            PlayerPrefs.SetInt ("DirectionalLightColorG", DirectionalLightColorG);
            PlayerPrefs.SetInt ("DirectionalLightColorB", DirectionalLightColorB);

Thank you in advance for your help.

Best regards,

Alexandre.

Is NightAndDayCycleScript.DirectionalLight.color a Color or a Color32?

1 Like

Thank you for your answer.
It’s a Color not Color32!
That’s the problem certainly…
But I’m still blocked, I need to convert Color to Color32? Because I want to keep the color32…

Just create a Color32 from the color and save the values that you need. They’re even implicitly convertible from one to the other. You can also combine the bytes of the Color32 and save them as a single integer value rather than 4 separate values.

1 Like

Ok guys.
Thank you for your help! :slight_smile: