2013-06-20 3 views
1

외부 설정 파일을 통해 Unity에서 카메라를 만들려고합니다. 순간 다음과 같이 내가 읽어 및 그 값이 저장되고 해당 파일이 있습니다두 클래스 사이의 목록에서 데이터 공유

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; 
} 

내가 주석 Debug.Logs으로이 테스트를했는데 내 설정 파일에 읽고 값을지고 있음을 알 수 저장된.

내 HV_Camera 클래스에서 cameraList의 데이터에 액세스하려고합니다. 이것은 내가 가지고있는 것입니다 :

 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"); 
    } 


} 

이제는 for 루프에서 데이터가 전달되는지 간단하게보고 싶습니다. 작동한다면 "나는 카메라 다"라는 메시지가 4 번 인쇄됩니다. 그러나 이것은 사실이 아닙니다. 내가 누락되었거나 제대로하지 않는 것이 있습니까?

+0

메서드 디버깅을 시도 했습니까? 그리고 당신은 당신의 씬에서 같은 GameObject에 두 개의 컴포넌트를 가지고 있습니까? –

+0

나는 그렇습니다. 모든 Debug.Log는 필자가 원하는대로 모든 것이 호출되는지 확인하기위한 시도입니다. 그리고 다시 예, 두 스크립트는 내 장면의 같은 객체에 있습니다. – N0xus

+0

명백한 질문은 :'StoreXMLValues ​​()'가'GetCameraList()'보다 먼저 호출되고 있는지 확실합니까? –

답변

0

문제가 스크립트 실행 순서에 있다고 생각됩니다. 귀하의 모든 논리는 구성 요소의 Start 방법에 있으며, 이 HV_Camera.Start() 전에 실행될 것이라고 보장 할 수는 없습니다.

설정 목록이 채워지기 전에 HV_Camera이 가장 먼저 실행됩니다. Unity IDE에서 스크립트의 우선 순위를 설정하여 이것이 발생하지 않도록 할 수 있습니다. 개발자의 관점에서 볼 때 조금은 직관적이지 않습니다.

또는 코드를 다시 구성하여 코드가 올바른 순서로 실행되는지 확인할 수 있습니다. 예를 들어 public 메서드 HV_ReadSettingsFile.EnsureFileIsRead()을 만든 경우 두 메서드의 Start()에서 해당 메서드를 호출 할 수 있습니다. 메소드는 이미 실행되었는지 여부를 기록하는 플래그를 사용하여 코드가 정확히 한 번만 실행되고 어떤 구성 요소가 필요한지를 확인해야합니다.

관련 문제