XML 去中心化在 Unity iOS 构建中不起作用,在 Windows 中起作用

XML Decentralization not working in Unity iOS build which working in Windows

我正在获取有关设置 UI 和通过 XML 消息的详细信息,这些信息位于构建的自定义文件夹中。它在 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 build its not deserializing 中,具有 XML details 的函数出错,因为 details 没有反序列化和检索。

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 

我已将 API 兼容级别从 4.x 切换到 .NET Standard 2.0,它可以正常工作

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

在 Unity 2018.2 及更早版本中,您需要使用 .NET Standard 2.0 Api 兼容级别,具体取决于您的决定。

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