【问题标题】:Sharing data in a list between two classes在两个类之间共享列表中的数据
【发布时间】:2013-06-20 10:10:49
【问题描述】:

我正在尝试通过外部设置文件在 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 次。但事实并非如此。我有什么遗漏或做得不对吗?

【问题讨论】:

  • 您是否尝试过调试它以确保您的方法被调用?你的场景中的同一个游戏对象上是否有两个组件?
  • 我同意。所有 Debug.Logs 都是我尝试检查以确保所有内容都按应有的方式调用。再次是的,两个脚本都在我场景中的同一个对象上。
  • 显而易见的问题是:你确定StoreXMLValues() 是在GetCameraList() 之前被调用的吗?

标签: c# list unity3d


【解决方案1】:

我怀疑问题出在脚本执行的顺序上。你所有的逻辑都在组件的Start 方法中,你不能保证HV_ReadSettingsFile.Start() 会在HV_Camera.Start() 之前运行。

HV_Camera 很可能在您的设置列表被填充之前首先运行。可以在 Unity IDE 中为脚本设置优先顺序,以确保不会发生这种情况,尽管从开发人员的角度来看,这有点不直观。

或者,您可以重组代码以确保代码以正确的顺序运行。例如,如果您创建了一个公共方法HV_ReadSettingsFile.EnsureFileIsRead(),您可以从两个组件的Start() 调用该方法。该方法应该使用一个标志来记录它是否已经运行,从而确保代码只运行一次,并且对于任何需要它的组件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 2015-09-08
    相关资源
    最近更新 更多