简体   繁体   中英

Sharing data in a list between two classes

I'm trying to make a camera in Unity through an external settings file. At the moment I have that file being read in and its values stored as follows:

    public struct Entry
{
    public System.Object value;
    public Type type;
}

public class HV_ReadSettingsFile : MonoBehaviour

{
    Entry _screenEntry;
    Entry _cameraEntry;


public Dictionary<string, Entry> cameraDictionary = new Dictionary<string, Entry>();
public List<HV_Camera> cameraList = new List<HV_Camera>();
public List<HV_Screen> screenList = new List<HV_Screen>();




// Use this for initialization
void Start()
{
    StoreXMLValues();

}

// Update is called once per frame
void Update()
{
}

void StoreXMLValues()
{
    var xdoc = XDocument.Load(@"C:\\Test.xml");
    var screens = xdoc.Descendants("Screen");
    var cameras = xdoc.Descendants("Camera");


    foreach (var screen in screens)
    {
        HV_Screen _screen = new HV_Screen();
        _screen.Name = (string)screen.Element("Name").Attribute("Name");
        _screen.Tag = (string)screen.Element("ScreenTag").Attribute("Tag");
        _screen.XPOS = (string)screen.Element("LocalPosition").Attribute("X");
        _screen.YPOS = (string)screen.Element("LocalPosition").Attribute("Y");
        _screen.ZPOS = (string)screen.Element("LocalPosition").Attribute("Z");
        _screen.Width = (string)screen.Element("Width").Attribute("Width");
        _screen.Height = (string)screen.Element("Height").Attribute("Height");
        _screen.YAW = (string)screen.Element("Orientation").Attribute("YAW");
        _screen.PITCH = (string)screen.Element("Orientation").Attribute("PITCH");
        _screen.ROLL = (string)screen.Element("Orientation").Attribute("ROLL");



        //Debug.Log("Screen name: " + _screen.Name);
        //Debug.Log("Screen tag: " + _screen.Tag);
        //Debug.Log("Screen xpos: " + _screen.XPOS);
        //Debug.Log("Screen ypos: " + _screen.YPOS);
        //Debug.Log("Screen zpos: " + _screen.ZPOS);
        //Debug.Log("Screen width: " + _screen.Width);
        //Debug.Log("Screen height: " + _screen.Height);
        //Debug.Log("Screen Yaw: " + _screen.YAW);
        //Debug.Log("Screen Pitch: " + _screen.PITCH);
        //Debug.Log("Screen Roll: " + _screen.ROLL);
        screenList.Add(_screen);
    }

    foreach (var camera in cameras)
    {
        HV_Camera _camera = new HV_Camera();
        _camera.Name = (string)camera.Element("Name").Attribute("Name");
        _camera.Tag = (string)camera.Element("CameraTag").Attribute("Tag");
        _camera.XPOS = (string)camera.Element("LocalPosition").Attribute("X");
        _camera.YPOS = (string)camera.Element("LocalPosition").Attribute("Y");
        _camera.ZPOS = (string)camera.Element("LocalPosition").Attribute("Z");
        _camera.YAW = (string)camera.Element("Orientation").Attribute("Yaw");
        _camera.PITCH = (string)camera.Element("Orientation").Attribute("Pitch");
        _camera.ROLL = (string)camera.Element("Orientation").Attribute("Roll");
        _camera.Near = (string)camera.Element("Near").Attribute("Near");
        _camera.Far = (string)camera.Element("Far").Attribute("Far");
        _camera.FOV = (string)camera.Element("FOV").Attribute("FOV");
        _camera.AspectRatio = (string)camera.Element("AspectRatio").Attribute("AspectRatio");
        _camera.ScreenDistance = (string)camera.Element("ScreenDistance").Attribute("ScreenDistance");

       // Debug.Log("Camera name: " + _camera.Name);
        cameraList.Add(_camera);

    }

    //Debug.Log("Camera Count: " + cameraList.Count);

    //Debug.Log("Screen Count: " + screenList.Count);
}

public List<HV_Camera> GetCameraList()
{
   // Debug.Log("Got list");
    return cameraList;
}

I've tested this with the commented out Debug.Logs and I know that my settings file is getting read in and the values stored.

In my HV_Camera class, I'm trying to access the data in the cameraList. This is what I have:

     HV_ReadSettingsFile settings;
        List<HV_Camera> testCamera = new List<HV_Camera>();

void Start () 
{
   settings = gameObject.GetComponent<HV_ReadSettingsFile>();
 GetList();
   CreateCamera();
}

// Update is called once per frame
void Update () 
{

}


public void GetList()
{
    testCamera = settings.GetCameraList();


}

public void CreateCamera()
{

    for (int i = 0; i < testCamera.Count; i++)
    {
        Debug.Log("I am camera");
    }


}

Now, in that for loop, I'm simple wanting to see if the data is getting passed over. If it works, "I am a camera" should get printed out 4 times. But this isn't the case. Is there something I'm missing or not doing correctly?

I suspect the problem is in the order of script execution. All of your logic is in the Start methods of the components, and you've no guarantee that HV_ReadSettingsFile.Start() will run before HV_Camera.Start() .

Most likely HV_Camera is running first, before your settings list is populated. It's possible to set a priority order for scripts in the Unity IDE to ensure that this doesn't happen, though I find this a little unintuitive from a developer perspective.

Alternatively, you can restructure your code to ensure that the code is run in the correct order. For example, if you made a public method HV_ReadSettingsFile.EnsureFileIsRead() , you can call that method from the Start() of both components. The method should use a flag to record whether it's already been run, and thus ensure that the code is run exactly once, and for whichever component requires it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM