BOBTHEBUILDER
BOBTHEBUILDER

Reputation: 393

changing unity project settings during run

I have just started using unity and I'm just checking out other peoples projects to get an idea of how it works instead of jumping into my own without any knowledge first. and I'm experimenting with a unity project from someone else's github and I've been enjoying manipulating the game settings while the game is running, but I've only managed to do this through playing the scene within the unity project editor (if that's what it's called) with the inspect sidebar. I would like to be able to do the same after I have built and run to get a better view. is there a way to keep using the setting board or a way to transfer these into in game variables I can change somehow?

here is the github I took the code from: https://github.com/SebLague/Slime-Simulation.git

Upvotes: 0

Views: 1243

Answers (1)

derHugo
derHugo

Reputation: 90683

Note that in general not everyone has the ambitions to clone a git project just to figure out what exactly you are asking for and how it could be done ;)

Also note that calling these "project settings" is extremely misleading! In Unity the ProjectSettings is a complete different thing and usually only changed within the editor before building the application.

The settings you are most probably referring to are just the three ScriptableObject instances A, B and C which are instances of

public class SlimeSettings : ScriptableObject
{
    [Header("Simulation Settings")]
    [Min(1)] public int stepsPerFrame = 1;
    public int width = 1280;
    public int height = 720;
    public int numAgents = 100;
    public Simulation.SpawnMode spawnMode;

    [Header("Trail Settings")]
    public float trailWeight = 1;
    public float decayRate = 1;
    public float diffuseRate = 1;

    public SpeciesSettings[] speciesSettings;

    [System.Serializable]
    public struct SpeciesSettings
    {
        [Header("Movement Settings")]
        public float moveSpeed;
        public float turnSpeed;

        [Header("Sensor Settings")]
        public float sensorAngleSpacing;
        public float sensorOffsetDst;
        [Min(1)] public int sensorSize;
    }
}

This is then referenced in the component Simulation

public class Simulation : MonoBehaviour
{
    ...

    public SlimeSettings settings;

    ...
}

It is a bit unclear what exactly you are wanting to achieve but I can imagine two possible things

  • You want to exchange the referenced settings asset at runtime

    You can do so by e.g. having an additional controller component like

    [RequireComponent(typeof(Simulatuion))]
    public class SettingsChanger : MonoBehaviour
    {
        [SerializeField] private Simulation _simulation;
    
        [SerializeField] private List<SlimeSettings> AllSettings;
    
        private int _current;
    
        private void Update()
        {
            // as an example just go to the next settings every time you press Space key
            if(Input.GetKeyDown(KeyCode.Space))
            {
                if(!_simulation) _simulation = GetComponent<Simulation>();
    
                _current = (_current + 1) % AllSettings.Length;
                _simulation.settings = AllSettings[_current];
            }
        }
    }
    
  • Or what you want is actually changing the values of the current asset (be aware that within the editor such changes in ScriptableObjects will be persistent!)

    You can do this as before in a separate script like e.g.

    // Somehow get a reference to the component like e.g.
    var simulation = GetComponent<Simulation>();
    
    // get the currently referenced settings asset reference
    var settings = simulation.settings;
    
    // No make whatever changes you want to make
    settings.spawnMode = Simulation.SpawnMode.RandomCircle;
    

Note though that apparently most of these settings only have affect once in start and it won't matter if they are changed afterwards so you would need to somehow reinitialize the simulation every time you make a change if this is what you are trying to achieve.

Upvotes: 1

Related Questions