-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalizationComponent.cs
90 lines (76 loc) · 2.15 KB
/
LocalizationComponent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Linq;
public class LocalizationGenerator
{
public static LocalizationComponent[] GenerateLocale(Canvas[] canvas)
{
if (canvas.Length == 0)
{
Debug.Log(" неопределен массив Canvas.");
return null;
}
string path = Application.dataPath + "/Resources/Localization/Default.xml";
List<LocalizationComponent> list = new List<LocalizationComponent>();
foreach (Canvas target in canvas)
{
if (target)
{
LocalizationComponent[] comp = target.GetComponentsInChildren<LocalizationComponent>();
foreach (LocalizationComponent c in comp)
{
c.SetComponent();
if (c.target) list.Add(c);
}
}
}
if (list.Count == 0)
{
Debug.Log(" указанный Canvas, не содержит дочернего компонента LocalizationComponent.");
return null;
}
LocalizationComponent[] copy = list.ToArray();
LocalizationComponent[] result = list.Distinct(new HashComparer()).ToArray();
XmlNode userNode;
XmlAttribute attribute;
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration declaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmlDoc.AppendChild(declaration);
XmlNode rootNode = xmlDoc.CreateElement("locale");
xmlDoc.AppendChild(rootNode);
for (int i = 0; i < result.Length; i++)
{
if (result[i].isCustom)
{
userNode = xmlDoc.CreateElement("custom");
userNode.InnerText = result[i].content;
}
else
{
userNode = xmlDoc.CreateElement("content");
userNode.InnerText = result[i].target.text;
}
attribute = xmlDoc.CreateAttribute("id");
attribute.Value = result[i].hash.ToString();
userNode.Attributes.Append(attribute);
rootNode.AppendChild(userNode);
}
xmlDoc.Save(path);
Debug.Log(" создан фаил локали: " + path);
return copy;
}
}
class HashComparer : IEqualityComparer<LocalizationComponent>
{
public bool Equals(LocalizationComponent x, LocalizationComponent y)
{
return x.hash == y.hash;
}
public int GetHashCode(LocalizationComponent obj)
{
return obj.hash;
}
}