XML Decentralization无法在Windows中运行的Unity iOS构建中运行

时间:2018-07-22 12:07:32

标签: ios xml unity3d serialization xml-deserialization

我正在获取有关通过放置在构建的自定义文件夹中的XML设置UI和消息的详细信息。它可以在Windows版本中正常运行,而不是在iOS中。我要附加使用的脚本。

XML操作

using System.IO;
using System.Xml.Serialization;
using UnityEngine;
using System.Xml;

public class XMLOperations
{
    public static void Serialize(object item, string path)
    {
        XmlSerializer serializer = new XmlSerializer(item.GetType());
        StreamWriter writer = new StreamWriter(path);
        serializer.Serialize(writer.BaseStream, item);
        writer.Close();
    }

    public static T Deserialize<T>(string path)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        StreamReader reader = new StreamReader(path);
        T deserialized = (T)serializer.Deserialize(reader.BaseStream);
        reader.Close();
        Debug.Log(deserialized);
        return deserialized;
    }
}

提取XML的详细信息

using System.Xml.Serialization;
using System.IO;
using System.Text;

[XmlRoot("WelcomePage")]
public class WelcomePage
{
    [XmlElement("Background")]
    public GenericDetails background;
    [XmlElement("MotionBand")]
    public GenericDetails motionBand;
    [XmlElement("DefaultLogo")]
    public GenericDetails defaultLogo;
    [XmlElement("WelcomeMessage")]
    public GenericDetails welcomeMessage;
    [XmlElement("VisitName")]
    public GenericDetails visitName;
    [XmlElement("VisitorName")]
    public GenericDetails[] visitorName;
    [XmlElement("Date")]
    public GenericDetails date;
    [XmlElement("Location")]
    public GenericDetails location;
    [XmlElement("CustomerLogo")]
    public GenericDetails customerLogo;
}

public class GenericDetails
{
    [XmlElement("ImagePath")]
    public string path;
    [XmlElement("Rect")]
    public string rect;
    [XmlElement("BGColor")]
    public string bgColor;
    [XmlElement("TextAlignment")]
    public string alignment;
    [XmlElement("TextColor")]
    public string textColor;
    [XmlElement("BGTransparency")]
    public float bgTransparency;
    [XmlElement("MaxFontSize")]
    public int maxFontSize;
}

public class WelcomePageSetup
{
    public WelcomePageSetup(string url)
    {
        if (!File.Exists(url))
        {
            if (!Directory.Exists(Path.GetDirectoryName(url)))
                Directory.CreateDirectory(Path.GetDirectoryName(url));

            File.WriteAllText(url, WelcomeLayout._main.welcomePageDetails, Encoding.UTF8);
        }
    }
}

带有详细信息的XML文件

<?xml version="1.0" encoding="utf-8"?>
<WelcomePage>

  <Background>
    <Rect>1232,0,688,1080</Rect>
    <BGColor>#000000</BGColor>
    <BGTransparency>0.7</BGTransparency>
  </Background>

  <MotionBand>
    <ImagePath>/UI/Images/MotionBand.png</ImagePath>
    <Rect>1842,0,78,1080</Rect>
  </MotionBand>

  <DefaultLogo>
    <ImagePath>/UI/Images/DefaultLogo.png</ImagePath>
    <Rect>1497,23,320,60</Rect>
  </DefaultLogo>

  <WelcomeMessage>
    <Rect>1272,942,557,126</Rect>
    <TextAlignment>mc</TextAlignment>
    <TextColor>#F0AB00</TextColor>
    <MaxFontSize>55</MaxFontSize>
  </WelcomeMessage>

  <VisitName>
    <Rect>1272,810,557,123</Rect>
    <TextAlignment>ml</TextAlignment>
    <TextColor>#ffffff</TextColor>
    <MaxFontSize>35</MaxFontSize>
  </VisitName>

  <!--Visitor name - we can have 5 visitor name display at a time-->
  <!--Visitor name 1-->
  <VisitorName>
    <Rect>1272,715,557,65</Rect>
    <TextAlignment>ml</TextAlignment>
    <TextColor>#ffffff</TextColor>
    <MaxFontSize>35</MaxFontSize>
  </VisitorName>

  <!--Visitor name 2-->
  <VisitorName>
    <Rect>1272,635,557,65</Rect>
    <TextAlignment>ml</TextAlignment>
    <TextColor>#ffffff</TextColor>
    <MaxFontSize>35</MaxFontSize>
  </VisitorName>

  <!--Visitor name 3-->
  <VisitorName>
    <Rect>1272,555,557,65</Rect>
    <TextAlignment>ml</TextAlignment>
    <TextColor>#ffffff</TextColor>
    <MaxFontSize>35</MaxFontSize>
  </VisitorName>

  <!--Visitor name 4-->
  <VisitorName>
    <Rect>1272,475,557,65</Rect>
    <TextAlignment>ml</TextAlignment>
    <TextColor>#ffffff</TextColor>
    <MaxFontSize>35</MaxFontSize>
  </VisitorName>

  <!--Visitor name 5-->
  <VisitorName>
    <Rect>1272,395,557,65</Rect>
    <TextAlignment>ml</TextAlignment>
    <TextColor>#ffffff</TextColor>
    <MaxFontSize>35</MaxFontSize>
  </VisitorName>

  <Date>
    <Rect>1272,190,557,48</Rect>
    <TextAlignment>ml</TextAlignment>
    <TextColor>#ffffff</TextColor>
    <MaxFontSize>30</MaxFontSize>
  </Date>

  <Location>
    <Rect>1272,140,557,48</Rect>
    <TextAlignment>ml</TextAlignment>
    <TextColor>#F0AB00</TextColor>
    <MaxFontSize>30</MaxFontSize>
  </Location>

  <CustomerLogo>
    <Rect>1272,274,140,70</Rect>
  </CustomerLogo>

</WelcomePage>

通过脚本和路径调用

using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Collections.Generic;
using System;
using System.Collections;

public class WelcomeLayout : MonoBehaviour
{
    public static WelcomeLayout _main;
    public WelcomePage welcomePage;

    public RectTransform background;
    public RectTransform motionBand;
    public RectTransform defaultLogo;
    public RectTransform welcomeMessage;
    public RectTransform visitName;
    public RectTransform date;
    public RectTransform location;
    public RectTransform[] visitorName;
    public RectTransform customerLogo;

    public Image backgroundBG;
    public Image motionBandImg;
    public Image defaultLogoImg;
    public Image customerLogoImg;

    public Text welcomeMessageTxt;
    public Text visitNameTxt;
    public Text dateTxt;
    public Text locationTxt;
    public Text[] visitorNameTxt;

    public GameObject[] visitorNameGObj;

    string path_base;
    string filePath;

    public GameObject welcomeScreenSet;

    public bool welcomeScreenDisplayTimer;
    public float welcomeTimer;
    public bool checkOnce;

    public bool displayVisiorNameNewSet;
    public float displayVisitorTimer;
    public float displayDuration;
    int displayFrom;

    public string currentTime;
    public string currentDay;

    public string currentTestTime;

    WelcomePageSetup welcomePageSetup;

    private void Awake()
    {
        _main = this;
    }

    private void Start()
    {
        path_base = Application.dataPath + "/Resources";

        path_base = Application.persistentDataPath+ "/Resources";
#endif

        filePath = path_base + "/WelcomeScreenTemplate.xml";

        FileInfo file = new FileInfo(filePath);

        if (file.Exists)
        {
            Debug.Log("welcome xml available");
        }
        else
        {
            Debug.Log("welcome xml not available - create xml file");
            welcomePageSetup = new WelcomePageSetup(filePath);
        }

        if (file.Exists)
        {
            welcomePage = XMLOperations.Deserialize<WelcomePage>(filePath);
        }
    }

    //Setup welcome page using XML details
    public void SetWelcomeLayout(string visit, string loc, string day, string welcome, List<string> visitor, string logo)
    {
        //Set BG
        SetRectTransform(welcomePage.background.rect, background);
        SetColor(backgroundBG, welcomePage.background.bgColor, welcomePage.background.bgTransparency);

        //Set motion band
        SetSpriteOnImage(motionBandImg, path_base + welcomePage.motionBand.path);
        SetRectTransform(welcomePage.motionBand.rect, motionBand);

        //Set default logo
        SetSpriteOnImage(defaultLogoImg, path_base + welcomePage.defaultLogo.path);
        SetRectTransform(welcomePage.defaultLogo.rect, defaultLogo);

        //Set Welcome message
        welcomeMessageTxt.text = welcome;
        SetRectTransform(welcomePage.welcomeMessage.rect, welcomeMessage);
        SetColor(welcomeMessageTxt, welcomePage.welcomeMessage.textColor);
        AlignTextAndMaxFont(welcomeMessageTxt, welcomePage.welcomeMessage.alignment, welcomePage.welcomeMessage.maxFontSize);

        //Set visit name
        //byte[] bytes = Encoding.Default.GetBytes(visit);
        //visit = Encoding.UTF8.GetString(bytes);
        visitNameTxt.text = visit.ToUpper();
        SetRectTransform(welcomePage.visitName.rect, visitName);
        SetColor(visitNameTxt, welcomePage.visitName.textColor);
        AlignTextAndMaxFont(visitNameTxt, welcomePage.visitName.alignment, welcomePage.visitName.maxFontSize);

        //Set date
        //dateTxt.text = day;
        currentDay = day;
        SetRectTransform(welcomePage.date.rect, date);
        SetColor(dateTxt, welcomePage.date.textColor);
        AlignTextAndMaxFont(dateTxt, welcomePage.date.alignment, welcomePage.date.maxFontSize);

        //Set location
        locationTxt.text = loc;
        SetRectTransform(welcomePage.location.rect, location);
        SetColor(locationTxt, welcomePage.location.textColor);
        AlignTextAndMaxFont(locationTxt, welcomePage.location.alignment, welcomePage.location.maxFontSize);
}

}

请务必检查脚本并帮助我解决问题。它可以在独立版本中正常工作,我可以编辑XML详细信息,以反映UI。在iOS版本中,它不进行反序列化,由于未对细节进行反序列化和检索,带有XML详细信息的函数出错。

iOS中显示的异常-Xcode

welcome xml available

WelcomeLayout:Start()



(Filename: /Users/builduser/buildslave/unity/build/Runtime/Export/Debug.bindings.h Line: 43)



NotSupportedException: /Users/builduser/buildslave/unity/build/External/il2cpp/il2cpp/libil2cpp/icalls/mscorlib/System.Reflection.Emit/AssemblyBuilder.cpp(20) : Unsupported internal call for IL2CPP:AssemblyBuilder::basic_init - System.Reflection.Emit is not supported.

  at System.Reflection.Emit.AssemblyBuilder..ctor (System.Reflection.AssemblyName n, System.String directory, System.Reflection.Emit.AssemblyBuilderAccess access, System.Boolean corlib_internal) [0x00000] in <00000000000000000000000000000000>:0 

  at System.AppDomain.DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access, System.String dir, System.Security.Policy.Evidence evidence, System.Security.PermissionSet requiredPermissions, System.Security.PermissionSet optionalPermissions, System.Security.PermissionSet refusedPermissions, System.Boolean isSynchronized) [0x00000] in <00000000000000000000000000000000>:0 

  at System.AppDomain.DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access) [0x00000] in <00000000000000000000000000000000>:0 

  at System.Xml.Serialization.TempAssembly.GenerateRefEmitAssembly (System.Xml.Serialization.XmlMapping[] xmlMappings, System.Type[] types, System.String defaultNamespace, System.Security.Policy.Evidence evidence) [0x00000] in <00000000000000000000000000000000>:0 

  at System.Xml.Serialization.TempAssembly..ctor (System.Xml.Serialization.XmlMapping[] xmlMappings, System.Type[] types, System.String defaultNamespace, System.String location, System.Security.Policy.Evidence evidence) [0x00000] in <00000000000000000000000000000000>:0 

  at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly (System.Xml.Serialization.XmlMapping xmlMapping, System.Type type, System.String defaultNamespace) [0x00000] in <00000000000000000000000000000000>:0 

  at System.Xml.Serialization.XmlSerializer..ctor (System.Type type, System.String defaultNamespace) [0x00000] in <00000000000000000000000000000000>:0 

  at XMLOperations.Load[T] (System.String path) [0x00000] in <00000000000000000000000000000000>:0 

  at WelcomeLayout.Start () [0x00000] in <00000000000000000000000000000000>:0 

2 个答案:

答案 0 :(得分:1)

您在Unity用于AOT平台(如iOS)的.NET配置文件实现中遇到了限制。在Unity 2018.3和更高版本(尚未发布)中,我们已纠正了此问题,因此Unity中的任何Api兼容性级别选项都将使用AOT友好的类库实现。

在Unity 2018.2和更早版本中,您需要根据确定使用.NET Standard 2.0 Api兼容性级别。

通常,.NET Standard 2.0 Api兼容性级别是正确的选择。它包含大多数Unity项目中使用的大多数API,并且它将生成比.NET 4.x Api兼容级别小的代码。

答案 1 :(得分:0)

我已经将API兼容性级别从4.x切换到了.NET Standard 2.0,