简体   繁体   English

构建后,Unity3d在项目文件夹中看不到XML文件

[英]Unity3d don't see the XML files in project folder after build

I'm using XML files to store some data so it will be more easy to localize to other language, since Unity have problems with resx i have to use XML. 我正在使用XML文件来存储一些数据,因此它更容易本地化到其他语言,因为Unity有resx问题我必须使用XML。

In UnityEditor everything works great, but when was i build it, it stop working at the first usage of XML. 在UnityEditor中,一切都很好,但是当我构建它时,它在第一次使用XML时停止工作。

I guess Unity just can't reach XML files for some reason, because when i try to manually put XML files to My Documents folder, and use reference to them, it worked. 我猜Unity由于某种原因无法访问XML文件,因为当我尝试手动将XML文件放到My Documents文件夹中并使用对它们的引用时,它起作用了。

this is current path to xml file: 这是xml文件的当前路径:

Assets/Scripts/Core/Localization/lang.eng.xml

Works great in UnityEditor, did not work when build. 在UnityEditor中工作得很好,在构建时不起作用。

this is another (But have to manually put them here): 这是另一个(但必须手动将它们放在这里):

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
           "\\testFolder\\lang.eng.xml"

But i don't like that, maybe there is some sort of way to make it work? 但我不喜欢这样,也许有某种方法可以让它发挥作用?

I know i can automatize this but i'm still don't like this. 我知道我可以自动化这个,但我仍然不喜欢这个。 I thought it will be binary like resx (.resources) in game folder by default. 我认为默认情况下它会像游戏文件夹中的resx(.resources)一样二进制。

What is the best solution in that situation? 那种情况下最好的解决方案是什么? If I have no choice I have to use "my documents" folder? 如果我别无选择,我必须使用“我的文档”文件夹?

Thanks in advance. 提前致谢。

---------------------Here is some code if necessary.----------------------- ---------------------如有必要,这里有一些代码.---------------------- -

To open xml files i use this: 要打开xml文件,我使用:

using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;

private static readonly XDocument Document = new XDocument;

private static void OpenDocument() {
    try {
        string filename = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +
           "\\testFolder\\lang.eng.xml";
        Document = XDocument.Load(filename);
    }
    catch (FileNotFoundException exception) {
        UnityEngine.Debug.Log(exception);
    }
}

To work with: 跟...共事:

  public static string GetEnumStringQ(Enum e, KindOfItemData kindOf) {
     string key = @"name=""" + e + @"""";
     string enumType = e.GetType().ToString();
     string kindOfValue = kindOf.ToString();
     if (Document == null) {
        OpenDocument();
     }

     if (Document.Root == null) return e.ToString();
     foreach (XElement el in Document.Root.Elements().Where(el => el.Name == enumType)) {
        foreach (XAttribute attr in el.Attributes().Where(attr => attr.ToString() == key)) {
           foreach (XElement element in el.Elements().Where(element => element.Name == kindOfValue)) {
              return element.Value;
           }
        }
     }
     return e.ToString();
  }

And this is piece of XML file: 这是一个XML文件:

<PassiveArmorSpecial name="StaminaRegenerationLow">   <value>Stamina Regeneration (low)</value></PassiveArmorSpecial>
<PassiveArmorSpecial name="StaminaRegenerationMed">   <value>Stamina Regeneration (med)</value></PassiveArmorSpecial>
<PassiveArmorSpecial name="StaminaRegenerationHigh">  <value>Stamina Regeneration (high)</value></PassiveArmorSpecial>
<PassiveArmorSpecial name="VampirismLow">             <value>Vampirism (low)</value></PassiveArmorSpecial>
<PassiveArmorSpecial name="VampirismMed">             <value>Vampirism (med)</value></PassiveArmorSpecial>
<PassiveArmorSpecial name="VampirismHigh">            <value>Vampirism (high)</value></PassiveArmorSpecial>


<TypeOfDefence name="Flesh">              <value>Flesh</value></TypeOfDefence>
<TypeOfDefence name="Bone">               <value>Bone</value></TypeOfDefence>
<TypeOfDefence name="Metal">              <value>Metal</value></TypeOfDefence>
<TypeOfDefence name="Rock">               <value>Rock</value></TypeOfDefence>

You have to put your files into Assets/Resources to make them available at runtime. 您必须将文件放入Assets / Resources以使它们在运行时可用。 You can get them via by Resources.Load method 您可以通过Resources.Load方法获取它们

XML files are then loaded as TextAssets. 然后将XML文件作为TextAssets加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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