Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

c# - Unity Multiple sliders dont update at the same time

i'm currently working on a Unity project in which i need to update 8 Slider values from a JSON file. I tested the loading part and everything works but when i apply the values to the sliders, they dont behave like i thought they would.

I have a OnButton function that: 1. Triggers JSON Loading -> 2. Passes the loaded JSON values to static variables -> 3. Calls a function to update the sliders.

Now here is the problem: On Button press, only one slider at a time gets updated, top to bottom and I do not understand why. So in order for every time you load a json file, you have to press the LOAD button up to 8 times, before every slider is updated. I use the same LoadSliders function for when the UI loads and it works there.

Thanks for your help!

using UnityEngine;
using UnityEngine.UI;
.
.
.

 public void UpdateSettings(BoidPresetSaveData loadedSave)
    {
        maxSpeed = loadedSave.maxSpeed;
        
        maxForce = loadedSave.maxForce;
        arriveRadius = loadedSave.arriveRadius;
        desiredSeparation = loadedSave.desiredSeparation;
        neighbourDistance = loadedSave.neighbourDistance;
        
        //Weights
        separation = loadedSave.separation;
        alignment = loadedSave.alignment;
        cohesion = loadedSave.cohesion;
        wander = loadedSave.wander;
        avoidWalls = loadedSave.avoidWalls;
    }

    public void LoadSliders()
    {
        maxSpeedSlider.value = maxSpeed;
        maxForceSlider.value = maxForce;
        arriveRadiusSlider.value = arriveRadius;
        desiredSeperationSlider.value = desiredSeparation;
        neighbourDistanceSlider.value = neighbourDistance;

        separationSlider.value = separation;
        alignmentSlider.value = alignment;
        cohesionSlider.value = cohesion;
        wanderSlider.value = wander;
        avoidWallsSlider.value = avoidWalls;
    }

And the OnButton function isnt very fancy either:

public void LoadPreset()
{
    var loadKey = presetDropdown.captionText.text;
    if (!SaveManager.SaveExists(loadKey,folderKey))
    {
        Debug.Log("Preset file does not exist.");
        return;
    }
    BoidPresetSaveData loadedSave = SaveManager.Load<BoidPresetSaveData>(loadKey, folderKey);
    BoidSettings.instance.UpdateSettings(loadedSave);
    BoidSettings.instance.LoadSliders();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, i found out, that the slider.value call also invokes the OnSliderChange event. With this in mind, the code above is not functional because you created a kind of a loop there. Also I lied that this works on Gui load, i just did not test enough. I had to split up the UpdateSettings function, so that every slider gets its own OnSliderChange method. So the functional code looks like this:

 public void UpdateMaxSpeed()
{
    maxSpeed = maxSpeedSlider.value;
}
public void UpdateMaxForce()
{
    maxForce = maxForceSlider.value;
}
public void UpdateArriveRadius()
{
    arriveRadius = arriveRadiusSlider.value;
}public void UpdateDesiredSeparation()
{
    desiredSeparation = desiredSeperationSlider.value;
}
public void UpdateNeighbourDistance()
{
    neighbourDistance = neighbourDistanceSlider.value;
}
public void UpdateSeparation()
{
    separation = separationSlider.value;
}

public void UpdateAlignment()
{
    alignment = alignmentSlider.value;
}
public void UpdateCohesion()
{
    cohesion = cohesionSlider.value;
}
public void UpdateWander()
{
    wander = wanderSlider.value;
}
public void UpdateAvoidWalls()
{
    avoidWalls = avoidWallsSlider.value;
}

There may be a cleaner method for this, but I do not know enough about C# and Unity to give an educated guess about this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...