- Home /
Public variables from another class's nested class
So I have a question about using public variables from an inherited class's nested class. Here's my base class:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
// Singleton access
private static CameraController _instance;
public static CameraController Instance
{
get { return _instance; }
}
// Create a new class defining variables for the default camera movements...
public class DefaultCamMovements {
public float camOffsetX = 5.5f; // Cam offset on the X axis (forward & backward).
public float camOffsetY = 2.5f; // Cam offset on the Y axis (up & down).
public float camOffsetZ = -30.0f; // Cam offset on the Z axis (zoom).
public float maxCamPosZ = -160.0f; // Max cam Z position (max zoom amount).
public float rightDamping = 13.5f; // Amount of dampening (smoothness of movement) when moving forward.
public float leftDamping = 20.0f; // Amount of dampening (smoothness of movement) when moving backward.
public float upDamping = 1.5f; // Amount of dampening (smoothness of movement) when moving up.
public float downDamping = 13.0f; // Amount of dampening (smoothness of movement) when moving down.
public float groundDamping = 8.0f; // Amount of dampening (smoothness of movement) when landing.
public float zoomDampingUp = 0.25f; // Amount of zoom dampening (smoothness of movement) when bike is jumping up.
public float zoomDampingDown = 1.0f; // Amount of zoom dampening (smoothness of movement) when bike is coming down.
public float zoomDampingGround = 3.0f; // Amount of zoom dampening (smoothness of movement) when bike is grounded.
public float rayLenghtDown = 30.0f; // Lenght of the ray used to detect ground collision while airborne.
public float rayLenghtRight = 15.0f; // Lenght of the ray used to detect forward collision.
}
public DefaultCamMovements camMov = new DefaultCamMovements();
}
Then I have another class attached to a GameObject, which inherits from CameraController:
using UnityEngine;
using System.Collections;
public class SideCam_3D : CameraController {
// Define the transform this script is attached to, and cache it on Awake for speed improvements
private Transform thisTransform;
// Define tunable variables
public float camOffsetX; // Cam offset on the X axis (zoom).
public float camOffsetY; // Cam offset on the Y axis (up & down).
public float camOffsetZ; // Cam offset on the Z axis (forward & backward).
public float maxCamPosZ; // Max cam Z position (max zoom amount).
public float rightDamping; // Amount of dampening (smoothness of movement) when moving forward.
public float leftDamping; // Amount of dampening (smoothness of movement) when moving backward.
public float upDamping; // Amount of dampening (smoothness of movement) when moving up.
public float downDamping; // Amount of dampening (smoothness of movement) when moving down.
public float groundDamping; // Amount of dampening (smoothness of movement) when landing.
public float zoomDampingUp; // Amount of zoom dampening (smoothness of movement) when bike is jumping up.
public float zoomDampingDown; // Amount of zoom dampening (smoothness of movement) when bike is coming down.
public float zoomDampingGround; // Amount of zoom dampening (smoothness of movement) when bike is grounded.
public float rayLenghtDown; // Lenght of the ray used to detect ground collision while airborne.
public float rayLenghtRight; // Lenght of the ray used to detect forward collision.
public float velocityTresholdX; // If target's speed is bigger then this, start zooming out.
private bool getRelativePos;
void Start () {
// Cache the transform of this gameObject for speed
thisTransform = this.transform;
// Link all the variables from main script.
camMov.camOffsetX = camOffsetX;
camMov.camOffsetY = camOffsetY;
camMov.camOffsetZ = camOffsetZ;
camMov.maxCamPosZ = maxCamPosZ;
camMov.rightDamping = rightDamping;
camMov.leftDamping = leftDamping;
camMov.upDamping = upDamping;
camMov.downDamping = downDamping;
camMov.groundDamping = groundDamping;
camMov.zoomDampingUp = zoomDampingUp;
camMov.zoomDampingDown = zoomDampingDown;
camMov.zoomDampingGround = zoomDampingGround;
camMov.rayLenghtDown = rayLenghtDown;
camMov.rayLenghtRight = rayLenghtRight;
orthoCam.velocityTresholdX = velocityTresholdX;
}
void LateUpdate () {
if( GameStates.initDone != true )
{
//Debug.LogWarning("This is LateUpdate in SideCam_3D.\n " +
//"GameStates Initialization isn't done yet, so we're not starting the Camera functions.");
return;
}
else
{
// Temporary Link all the variables from main script to the local ones so we can test
// them while running the game. Once numbers locked, remove the following references from LateUpdate
camMov.camOffsetX = camOffsetX;
camMov.camOffsetY = camOffsetY;
camMov.camOffsetZ = camOffsetZ;
camMov.maxCamPosZ = maxCamPosZ;
camMov.rightDamping = rightDamping;
camMov.leftDamping = leftDamping;
camMov.upDamping = upDamping;
camMov.downDamping = downDamping;
camMov.groundDamping = groundDamping;
camMov.zoomDampingUp = zoomDampingUp;
camMov.zoomDampingDown = zoomDampingDown;
camMov.zoomDampingGround = zoomDampingGround;
camMov.rayLenghtDown = rayLenghtDown;
camMov.rayLenghtRight = rayLenghtRight;
orthoCam.velocityTresholdX = velocityTresholdX;
if( this.camera.enabled )
getRelativePos = true;
else
getRelativePos = false;
// Invoke the CamFollow function.
// Pass "target" for the object to follow and "transform" for the camera it's attached to.
SmoothCamFollow( Managers.BikeObjCache.transform, thisTransform, getRelativePos );
}
}
}
That's the only way I have found so far to make those variables public and appear in the inspector...is there another (easier, cleaner) way to achieve this? I want to create a new instance of the nested class for each camera, instead of using the base nested class directly - in which case I would just use [System.Serializable] to show the class members in the inspector.
Thanks for your time!
Answer by roamcel · Aug 05, 2011 at 09:29 AM
Depending on the scope and extension of your CameraController original class, you might be able to solve the "wrapper" issue by creating your CameraController class as STATIC. This way, you surely will have to declare all your SideCam_3D public variables, but your method calls would be in the form of
mysidecam_3dvalue = CameraController.mystaticmethod();
which comes from its script
using UnityEngine;
using System.Collections;
public static class CameraController {
your structs, functions, etc. here
}
saving you the need to declare and assign the CameraController variables, and rather have them as function parameters, or even assigning them to the class in a constructor.
Thanks for your response roamcel. I did a quick test with what you suggested, but unfortunately it doesn't work the way I need it to work.
Thanks anyways