Forum rules - please read before posting.

Request to Set Default Brightness Value in AC Settings for Consistency Across Devices

BikBik
edited December 7 in Technical Q&A

Hi Chris,

I was wondering if it's possible to add my Brightness default value to the AC Settings under the "Default Options" section. My Brightness implementation consists of two scripts:

1) Brightness Slider Script: This is attached to the Slider prefab within the Settings prefab.

2) Brightness Controller Script: This script is attached to each scene in the game and directly manages the global 2D light.

The reason I’m asking is that my brightness default value isn’t consistent across different machines. This results in the game being either too bright or too dark when I test it. I’m hoping to set the default values using AC to ensure consistency.

Brightness Slider Script:
using UnityEngine;
using UnityEngine.UI;

public class BrightnessSlider : MonoBehaviour
{
    private Slider slider;
    private const string BrightnessKey = "BrightnessValue";

    private void Start()
    {
        slider = GetComponent<Slider>();

        float savedBrightness = PlayerPrefs.GetFloat(BrightnessKey, 2f);
        slider.value = savedBrightness;

        slider.onValueChanged.AddListener(OnSliderChanged);
    }

    private void OnSliderChanged(float value)
    {
        if (BrightnessController.instance != null)
        {
            BrightnessController.instance.ApplyBrightness(value);
        }
    }
}

Brightness Controller Script:
using UnityEngine;
using UnityEngine.Rendering.Universal; // For Light2D

public class BrightnessController : MonoBehaviour
{
    public Light2D globalLight;
    public static BrightnessController instance;

    private const string BrightnessKey = "BrightnessValue";

    private void Awake()
    {
        instance = this;
    }

    private void Start()
    {
        float savedBrightness = PlayerPrefs.GetFloat(BrightnessKey, 2f);
        ApplyBrightness(savedBrightness);
    }

    public void ApplyBrightness(float value)
    {
        if (globalLight != null)
        {
            globalLight.intensity = value;
        }
        PlayerPrefs.SetFloat(BrightnessKey, value);
    }
}

Comments

  • You can create custom Options settings by linking them to a Global Variable.

    Create a new Float variable named Brightness, then set its Link to property to Options Data. It's value will then be stored in the Profile data, along with Volume, Subtitles etc, rather than in individual saves.

    You can then set your Slider element's Slider affects property to Float Variable and link it to this new Brightness variable.

    You should then just need this script to sync its value to your Light:

    using UnityEngine;
    using UnityEngine.Rendering.Universal;
    using AC;
    
    public class BrightnessController : MonoBehaviour
    {
    
        public Light2D globalLight;
    
        private void OnEnable () => EventManager.OnUploadVariable += OnUpload;
        private void OnDisable () => EventManager.OnUploadVariable -= OnUpload;
    
        private void Start()
        {
            GlobalVariables.GetVariable("Brightness", true);
        }
    
        private void OnUpload (GVar variable, Variables variables)
        {
            if (variable != null && variable.label == "Brightness" && globalLight)
            {
                globalLight.intensity = variable.FloatValue;
            }
        }
    
    }
    

    For details on this topic, see the Manual's "Options data" chapter.

  • BikBik
    edited December 8

    Thank you for the detailed explanation, Chris. I just have a couple of questions about how to integrate the new script.

    Where exactly should I attach it? Should it go in my first scene, or is it meant to completely replace both of my existing scripts (Brightness Control and Brightness Slider)?

    Here’s what I tried:

    • I replaced my Brightness Control script with your new one, but I received this error:
      Scripts/BrightnessSlider.cs(21,34): error CS0117: 'BrightnessController' does not contain a definition for 'instance'

    • I replaced my Brightness Control script with your new one and deleted my Brightness Slider script, but then the brightness slider stopped working.

    • I kept both Scripts and attached yours to my first scene but I got these 2 errors:
      BrightnessController.cs(4,14): error CS0101: The namespace '' already contains a definition for 'BrightnessController'

    BrightnessController.cs(16,18): error CS0111: Type 'BrightnessController' already defines a member called 'Start' with the same parameter types
    **
    Could you clarify the correct setup?**

  • It's designed to replace both scripts. It's named BrightnessController as it's intended to be a replacement.

    You'll need to update the Slider to have it control the Option-linked Variable's value. If it doesn't appear to work, keep the Variables Manager open at runtime to check that the Variable's value is being controlled by it.

  • Thank you for the explanation, Chris. I replaced your script with my BrightnessController script and removed BrightnessSlider. I also updated the slider so it now controls the option-linked variable, and it seems to be working—when I adjust the Brightness slider in the settings menu, the linked float value updates correctly.

    However, the in-game brightness itself doesn’t change the brightness anymore. The brightness looks the same regardless of whether I increase or decrease the brightness slider.

    https://prnt.sc/K1TZk-hxFXQL
    https://prnt.sc/DgmsqpNycRmq
    https://prnt.sc/s6NW_UECapQG
    https://prnt.sc/esKeIctJwdYD

  • BikBik
    edited December 10

    Update:
    I managed to tweak your script (with the help of AI) so that it can adjust the brightness, and it worked. I can confirm that the brightness slider is now working 100%. I appreciate your help, Chris.

    I’ll place the updated script here in case anyone else wants to use it.

    using UnityEngine;
    using UnityEngine.Rendering.Universal;
    using AC;
    
    public class BrightnessController : MonoBehaviour
    {
        public Light2D globalLight;
    
        private void OnEnable() => EventManager.OnVariableChange += OnVariableChange;
        private void OnDisable() => EventManager.OnVariableChange -= OnVariableChange;
    
        private void Start()
        {
            // Apply initial value
            var variable = GlobalVariables.GetVariable("Brightness");
            if (variable != null && globalLight)
                globalLight.intensity = variable.FloatValue;
        }
    
        private void OnVariableChange (GVar variable)
        {
            if (variable != null && variable.label == "Brightness" && globalLight)
                globalLight.intensity = variable.FloatValue;
        }
    }
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.