- Home /
XML Decentralization not working in Unity iOS build which working in Windows
I am getting details for setting up UI and message through XML placed in a custom folder of the build. Its working fine in Windows build, not in iOS. I am attaching the Script used.
XML Operation
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;
}
}
Extract details of 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 File with details
<?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>
Calling it through script and path
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);
}
}
Please do check the script and help me out to resolve the issue. Its working fine in standalone builds, I can edit the XML details, its reflecting the UI. In iOS build its not de-serializing, the function with XML details got error because of details not de-serializing and retrieved.
Exception in 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 :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 :0
at System.AppDomain.DefineDynamicAssembly (System.Reflection.AssemblyName name, System.Reflection.Emit.AssemblyBuilderAccess access) [0x00000] in :0
at System.Xml.Serialization.TempAssembly.GenerateRefEmitAssembly (System.Xml.Serialization.Xml$$anonymous$$apping[] xml$$anonymous$$appings, System.Type[] types, System.String defaultNamespace, System.Security.Policy.Evidence evidence) [0x00000] in :0
at System.Xml.Serialization.TempAssembly..ctor (System.Xml.Serialization.Xml$$anonymous$$apping[] xml$$anonymous$$appings, System.Type[] types, System.String defaultNamespace, System.String location, System.Security.Policy.Evidence evidence) [0x00000] in :0
at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly (System.Xml.Serialization.Xml$$anonymous$$apping xml$$anonymous$$apping, System.Type type, System.String defaultNamespace) [0x00000] in :0
at System.Xml.Serialization.XmlSerializer..ctor (System.Type type, System.String defaultNamespace) [0x00000] in :0
at X$$anonymous$$LOperations.Load[T] (System.String path) [0x00000] in :0
at WelcomeLayout.Start () [0x00000] in :0
Answer by ben06feb · Jul 22, 2018 at 01:06 PM
I have switched the API compatibility level to .NET Standard 2.0 from 4.x, its working
Your answer
![](https://koobas.hobune.stream/wayback/20220612171247im_/https://answers.unity.com/themes/thub/images/avi.jpg)