【问题标题】:read XML file to create a 3D model读取 XML 文件以创建 3D 模型
【发布时间】:2021-07-09 20:08:37
【问题描述】:

我必须从 XML 加载数据(自定义,但它基于 XML 文件格式 (URF-8))并使用它们在 Unity 中创建模型。我使用了 XmlSerializer 类、自定义创建并尝试了论坛中的所有 XML 帮助等。 我调试了一切(一步一步),一切正常,但在那之后我总是遇到容器问题。 (我有阅读错误) 请,如果您对此有任何经验,您能告诉我什么方法对此有用吗?太棒了

XML struct file

这是我对 XMLserializer 的尝试:(对 XML 标记的一些描述进行了注释(尝试了一切:D)

//load.cs - creating a container
using System;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
using UnityEngine;

public class Load
{
    //[XmlAttribute("block type")]
    public float x_position;
    public float y_position;
    public float z_position;
}

//[XmlRoot("model version")]
public class Container
{
    //[XmlArrayItem("block type")]
    public List<Load> Load_container = new List<Load>();
}

//loadFile is main program.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;


public class LoadFile: MonoBehaviour
{
    public string path;

    // Start is called before the first frame update
    void Start()
    {
        var serializer = new XmlSerializer(typeof(Container));
        var stream = new FileStream(path, FileMode.Open);
        var container_load = serializer.Deserialize(stream) as Container;
        stream.Close();

        //Debug.Log();
    }

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

【问题讨论】:

    标签: c# xml unity3d


    【解决方案1】:

    尝试以下:

        [XmlRoot("model")]
        public class Model
        {
            [XmlAttribute()]
            public string version { get; set; }
            [XmlElement("block")]
            List<Block> block { get; set; }
        }
        public class Block
        {
            [XmlAttribute()]
            public string type { get; set; }
            public float x_position { get; set; }
            public float y_position { get; set; }
            public float z_position { get; set; }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      • 2011-07-15
      • 2011-07-05
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多