我試圖通過外部設置文件在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次。但事實並非如此。有什麼我失蹤或不正確?
你有沒有試過調試這個以確保你的方法被調用?你有兩個組件在你場景中的同一個GameObject上嗎? –
我有。所有Debug.Logs都是我試圖檢查以確保所有內容都被調用的。同樣是的,這兩個腳本都在我場景中的同一個對象上。 – N0xus
顯而易見的問題是:你確定'StoreXMLValues()'在'GetCameraList()'之前被調用嗎? –