How can I access both classes from a single type?
Hello, I have a camera script that I wish to have multiple configurations for different modes. For example, default and aim. I want the main camera script to read the config from one of two classes, the default class, or the aim class. In the camera script for example, I would have x = mouse.x * config.rotationSensitivity;. Since the config is in two different classes, I added them to the same script so that I could make references to them from the monobehaviour class at the top. So there are now 3 classes. I want to be able to switch between what config class I am using as the config, but I don't know what type to make the config variable to be able to hold both classes and access their variables. Would I have to use a different method of holding the variables like a list? Here are parts of the camera script:
public ? config;
protected virtual void Start () {
thirdPersonCameraConfig = GetComponent<ThirdPersonCameraConfig> ();
config = thirdPersonCameraConfig.defaultConfig;
x += mouse.Value.x * config.rotationSensitivity;
}
Here is the config script:
public class ThirdPersonCameraConfig : MonoBehaviour {
public DefaultConfig defaultConfig;
public AimConfig aimConfig;
}
[System.Serializable]
public class DefaultConfig {
public float rotationSensitivity = 3.5f; // The sensitivity of rotation
}
[System.Serializable]
public class AimConfig {
public float rotationSensitivity = 2.5f; // The sensitivity of rotation
}
}
Any help would be awesome, Thanks.
Answer by EmHuynh · Mar 04, 2016 at 09:51 PM
Hello, @chadfranklin47.
Update:
We can work with a single class, it's more simple and gets the job done.
using UnityEngine;
using System;
using System.Collections;
[ System.Serializable ]
public class Config
{
public float cameraRotationSensitivity;
public bool isWindowMode;
public Config() {
cameraRotationSensitivity = 2.5f; // Default value.
isWindowMode = false;
}
public static void CopyConfigAToB( Config configA, Config configB )
{
// Manually clone everything. There are other ways of cloning,
// but this gives us the most control.
configB.cameraRotationSensitivity = configA.cameraRotationSensitivity;
configB.isWindowMode = configA.isWindowMode;
}
}
Implementation:
public Config defaultConfig;
public Config customConfig;
void InitializeConfigs() {
defaultConfig = new Config();
customConfig = new Config();
// Set [customConfig].
customConfig.cameraRotationSensitivity = 5f;
customConfig.isWindowMode = true;
}
void Start() {
InitializeConfigs();
// Debugging.
Debug.Log( "Default = " + defaultConfig.cameraRotationSensitivity.ToString() );
Debug.Log( "Custom = " + customConfig.cameraRotationSensitivity.ToString() );
Config.CopyConfigAToB( customConfig, defaultConfig );
Debug.Log( "Copied Custom to Default..." );
Debug.Log( "Default is now = " + defaultConfig.cameraRotationSensitivity.ToString() );
}
Old:
public class ThirdPersonCameraConfig : MonoBehaviour {
public DefaultConfig defaultConfig;
public AimConfig aimConfig;
// The sensitivity of rotation
public float rotationSensitivity;
}
[ System.Serializable ]
public class DefaultConfig : ThirdPersonCameraConfig {
// Default constructor
public DefaultConfig() {
// Assign a value to a member variable
// from base class.
rotationSensitivity = 3.5f;
}
}
[ System.Serializable ]
public class AimConfig : ThirdPersonCameraConfig {
public AimConfig() {
rotationSensitivity = 2.5f;
}
}
Sorry @EmHuynh I had to edit the question because I did something wrong. Could you please look at it again?
Hey, @chadfranklin47. I have updated my answer. (:
Thanks so much for the answer. It helped a bunch.
No problem, @chadfranklin47. Let us know if you have any more questions!