Advertisement
p1ayer

XmlForm (for Unity 4.5.x)

Oct 31st, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 34.56 KB | None | 0 0
  1. //Code Status: draft 2014-10-31
  2.  
  3. //File: AppScript.cs
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Xml;
  9. using System.Xml.Serialization;
  10. using UnityEngine;
  11.  
  12. public class AppScript : MonoBehaviour
  13. {
  14.     private ConfigInfo _Config;
  15.  
  16.     private bool _IsExistConfig;
  17.  
  18.     //string _UserConfigFile = Application.persistentDataPath & Path.DirectorySeparatorChar & "Config.xml";
  19.     private string _UserConfigFile;
  20.  
  21.     private Uri _BaseUri;
  22.  
  23.     private XmlDocument _Document;
  24.     private string _FormID;
  25.     private string _FormID2;
  26.  
  27.     private Dictionary<string, string> _FromData;
  28.     private Dictionary<string, string> _headers;
  29.     private Dictionary<string, Texture2D> _uiTexture2D;
  30.     private Dictionary<string, GUIStyle> _uiStyle;
  31.  
  32.     private Vector2 _baseSize;
  33.     private Vector2 _scale;
  34.  
  35.     //private void Awake()
  36.     //{
  37.     //    Debug.Log("Awake called.");
  38.     //}
  39.  
  40.     private void Start()
  41.     {
  42.         _IsExistConfig = false;
  43.         //_UserConfigFile = Path.Combine(Application.persistentDataPath, "Config.xml");
  44.         _UserConfigFile = Application.persistentDataPath + "/Config.xml";
  45.         if (Path.DirectorySeparatorChar != '/')
  46.             _UserConfigFile = _UserConfigFile.Replace('/', Path.DirectorySeparatorChar);
  47.  
  48.         _BaseUri = new Uri("http://app.test.com.tw/");
  49.  
  50.         _Document = new XmlDocument();
  51.         _FormID = "";
  52.  
  53.         _FromData = new Dictionary<string, string>();
  54.         _headers = new Dictionary<string, string>();
  55.         _uiTexture2D = new Dictionary<string, Texture2D>();
  56.         _uiStyle = new Dictionary<string, GUIStyle>();
  57.  
  58.         _baseSize = new Vector2(640, 960);
  59.         _scale = new Vector2((float)Screen.width / _baseSize.x, (float)Screen.height / _baseSize.y);
  60.  
  61.         switch (Application.platform)
  62.         {
  63.             case RuntimePlatform.WindowsPlayer:
  64.             case RuntimePlatform.WindowsWebPlayer:
  65.             case RuntimePlatform.WindowsEditor:
  66.                 break;
  67.  
  68.             case RuntimePlatform.WP8Player:
  69.                 break;
  70.  
  71.             case RuntimePlatform.Android:
  72.                 break;
  73.  
  74.             case RuntimePlatform.OSXDashboardPlayer:
  75.             case RuntimePlatform.OSXPlayer:
  76.             case RuntimePlatform.OSXWebPlayer:
  77.             case RuntimePlatform.IPhonePlayer:
  78.                 break;
  79.  
  80.             default:
  81.                 break;
  82.         }
  83.  
  84.         FileInfo info = new FileInfo(_UserConfigFile);
  85.         if (info == null || info.Exists == false)
  86.         {
  87.             _IsExistConfig = false;
  88.             _FormID2 = "Config";
  89.         }
  90.         else
  91.         {
  92.             _IsExistConfig = true;
  93.             _FormID2 = "MainMenu";
  94.             loadConfig();
  95.         }
  96.     }
  97.  
  98.     // Update is called once per frame
  99.     private void Update()
  100.     {
  101.     }
  102.  
  103.     private void OnGUI()
  104.     {
  105.         //GUIUtility.ScaleAroundPivot(_scale, Vector2.zero);
  106.  
  107.         //GUI.Label(new Rect(0, 0, 6000, 20), Application.persistentDataPath);
  108.         xmlForm();
  109.     }
  110.  
  111.     private void loadConfig()
  112.     {
  113.         using (FileStream fs = new FileStream(_UserConfigFile, FileMode.Open, FileAccess.Read, FileShare.Read))
  114.         {
  115.             XmlDocument doc = new XmlDocument();
  116.             doc.Load(fs);
  117.             XmlReader reader = new XmlNodeReader(doc.DocumentElement);
  118.             XmlSerializer serializer = new XmlSerializer(typeof(ConfigInfo));
  119.             _Config = (ConfigInfo)serializer.Deserialize(reader);
  120.             fs.Close();
  121.         }
  122.         //Debug.Log(LoadedObj.lat);
  123.  
  124.         _headers.Add("GroupID", _Config.GroupID);
  125.         _headers.Add("LoginID", _Config.LoginID);
  126.         _headers.Add("Password", _Config.Password);
  127.     }
  128.  
  129.     private void saveConfig()
  130.     {
  131.         // Serialise to the XML document
  132.         XmlDocument doc = new XmlDocument();
  133.         using (XmlWriter writer = doc.CreateNavigator().AppendChild())
  134.         {
  135.             XmlSerializer serializer = new XmlSerializer(typeof(ConfigInfo));
  136.             serializer.Serialize(writer, _Config);
  137.         }
  138.  
  139.         doc.Save(_UserConfigFile);
  140.     }
  141.  
  142.     private void TryParseXmlForm(XmlNode node)
  143.     {
  144.         if ((node == null) || (!node.HasChildNodes))
  145.             return;
  146.  
  147.         XmlNodeList uiList = node.ChildNodes;
  148.  
  149.         foreach (XmlNode ui in uiList)
  150.         {
  151.             string type = ui.Name;
  152.  
  153.             string text = getXmlAttributesValue(ui, "text", string.Empty);
  154.             string image = getXmlAttributesValue(ui, "image", string.Empty);
  155.             string tooltip = getXmlAttributesValue(ui, "tooltip", string.Empty);
  156.  
  157.             string class2 = getXmlAttributesValue(ui, "class", string.Empty);
  158.             string style = getXmlAttributesValue(ui, "style", string.Empty);
  159.             string rect = getXmlAttributesValue(ui, "rect", "");
  160.             int maxLength = -1;
  161.  
  162.             GUIStyle style2 = null;
  163.             if (!string.IsNullOrEmpty(style) || !string.IsNullOrEmpty(class2))
  164.             {
  165.                 if (type == "Style")
  166.                 {
  167.                     TryParseStyle(string.Empty, class2, style, out style2);
  168.  
  169.                     string id = getXmlAttributesValue(ui, "id", string.Empty);
  170.                     if (string.IsNullOrEmpty(id))
  171.                     {
  172.                         DebugOut(ui);
  173.                         continue;
  174.                     }
  175.  
  176.                     _uiStyle.Add(id, style2);
  177.                     continue;
  178.                 }
  179.                 else
  180.                 {
  181.                     TryParseStyle(type, class2, style, out style2);
  182.                 }
  183.             }
  184.  
  185.             Rect position;
  186.             if (!TryParseRect(rect, out position))
  187.             {
  188.                 DebugOut(ui);
  189.                 continue;
  190.             }
  191.  
  192.             //------------------------------------------
  193.             //UI位置修正, 備用, 致能屬性尚未未定
  194.  
  195.             //水平置左(Left)
  196.             //position.x = 0;
  197.  
  198.             //水平置中(Center)
  199.             //position.x = (Screen.width >= position.width) ? ((Screen.width - position.width) / 2) : 0;
  200.  
  201.             //水平置右(Right)
  202.             //position.x = (Screen.width >= position.width) ? (Screen.width - position.width) : 0;
  203.  
  204.             //垂直置頂(Top)
  205.             //position.y = 0;
  206.  
  207.             //垂直置中
  208.             //position.y = (Screen.height >= position.height) ? ((Screen.height - position.height) / 2) : 0;
  209.  
  210.             //垂直置底(Bottom)
  211.             //position.y =  (Screen.height >= position.height) ? (Screen.height - position.height) : 0;
  212.             //------------------------------------------
  213.  
  214.             Texture2D image2 = null;
  215.             GUIContent content2 = new GUIContent();
  216.  
  217.             if (!string.IsNullOrEmpty(image))
  218.             {
  219.                 image2 = getTexture2D(image);
  220.                 content2.image = image2;
  221.             }
  222.  
  223.             if (!string.IsNullOrEmpty(text))
  224.                 content2.text = text;
  225.  
  226.             if (!string.IsNullOrEmpty(tooltip))
  227.                 content2.tooltip = tooltip;
  228.  
  229.             switch (type)
  230.             {
  231.                 case "Box":
  232.                     {
  233.                         if (style2 != null)
  234.                             GUI.Box(position, content2, style2);
  235.                         else
  236.                             GUI.Box(position, content2);
  237.                     }
  238.                     break;
  239.  
  240.                 case "Button":
  241.                     {
  242.                         if (style2 != null)
  243.                         {
  244.                             if (GUI.Button(position, content2, style2))
  245.                                 onButtonClick(ui);
  246.                         }
  247.                         else
  248.                         {
  249.                             if (GUI.Button(position, content2))
  250.                                 onButtonClick(ui);
  251.                         }
  252.                     }
  253.                     break;
  254.  
  255.                 case "Group":
  256.                     {
  257.                         if (string.IsNullOrEmpty(text) && (image2 == null))
  258.                         {
  259.                             if (style2 != null)
  260.                                 GUI.BeginGroup(position, style2);
  261.                             else
  262.                                 GUI.BeginGroup(position);
  263.                         }
  264.                         else
  265.                         {
  266.                             if (style2 != null)
  267.                                 GUI.BeginGroup(position, content2, style2);
  268.                             else
  269.                                 GUI.BeginGroup(position, content2);
  270.                         }
  271.  
  272.                         //遞迴展開
  273.                         TryParseXmlForm(ui);
  274.                         GUI.EndGroup();
  275.                     }
  276.                     break;
  277.  
  278.                 case "Label":
  279.                     {
  280.                         if (style2 != null)
  281.                             GUI.Label(position, content2, style2);
  282.                         else
  283.                             GUI.Label(position, content2);
  284.                     }
  285.                     break;
  286.  
  287.                 case "PasswordField":
  288.                     {
  289.                         string id = getXmlAttributesValue(ui, "id", string.Empty);
  290.                         if (string.IsNullOrEmpty(id))
  291.                         {
  292.                             DebugOut(ui);
  293.                             continue;
  294.                         }
  295.  
  296.                         if (_FromData.ContainsKey(id))
  297.                             text = _FromData[id];
  298.  
  299.                         if (ui.Attributes["maxLength"] != null)
  300.                             int.TryParse(ui.Attributes["maxLength"].Value, out maxLength);
  301.  
  302.                         char maskChar = '*';
  303.  
  304.                         if (style2 != null)
  305.                         {
  306.                             if (maxLength == -1)
  307.                                 _FromData[id] = GUI.PasswordField(position, text, maskChar, style2);
  308.                             else
  309.                                 _FromData[id] = GUI.PasswordField(position, text, maskChar, maxLength, style2);
  310.                         }
  311.                         else
  312.                         {
  313.                             if (maxLength == -1)
  314.                                 _FromData[id] = GUI.PasswordField(position, text, maskChar);
  315.                             else
  316.                                 _FromData[id] = GUI.PasswordField(position, text, maskChar, maxLength);
  317.                         }
  318.                     }
  319.                     break;
  320.  
  321.                 case "RepeatButton":
  322.                     {
  323.                         if (style2 != null)
  324.                         {
  325.                             if (GUI.RepeatButton(position, content2, style2))
  326.                                 onButtonClick(ui);
  327.                         }
  328.                         else
  329.                         {
  330.                             if (GUI.RepeatButton(position, content2))
  331.                                 onButtonClick(ui);
  332.                         }
  333.                     }
  334.                     break;
  335.  
  336.                 case "ScrollView":
  337.                     {
  338.                         //TODO: ScrollView
  339.  
  340.                         //遞迴展開
  341.                         //TryParseXmlForm(ui);
  342.                         //GUI.EndScrollView();
  343.                     }
  344.                     break;
  345.  
  346.                 case "TextArea":
  347.                     {
  348.                         string id = getXmlAttributesValue(ui, "id", string.Empty);
  349.                         if (string.IsNullOrEmpty(id))
  350.                         {
  351.                             DebugOut(ui);
  352.                             continue;
  353.                         }
  354.  
  355.                         if (_FromData.ContainsKey(id))
  356.                             text = _FromData[id];
  357.  
  358.                         if (ui.Attributes["maxLength"] != null)
  359.                             int.TryParse(ui.Attributes["maxLength"].Value, out maxLength);
  360.  
  361.                         if (style2 != null)
  362.                         {
  363.                             if (maxLength == -1)
  364.                                 _FromData[id] = GUI.TextArea(position, text, style2);
  365.                             else
  366.                                 _FromData[id] = GUI.TextArea(position, text, maxLength, style2);
  367.                         }
  368.                         else
  369.                         {
  370.                             if (maxLength == -1)
  371.                                 _FromData[id] = GUI.TextArea(position, text);
  372.                             else
  373.                                 _FromData[id] = GUI.TextArea(position, text, maxLength);
  374.                         }
  375.                     }
  376.                     break;
  377.  
  378.                 case "TextField":
  379.                     {
  380.                         string id = getXmlAttributesValue(ui, "id", string.Empty);
  381.                         if (string.IsNullOrEmpty(id))
  382.                         {
  383.                             DebugOut(ui);
  384.                             continue;
  385.                         }
  386.  
  387.                         if (_FromData.ContainsKey(id))
  388.                             text = _FromData[id];
  389.  
  390.                         if (ui.Attributes["maxLength"] != null)
  391.                             int.TryParse(ui.Attributes["maxLength"].Value, out maxLength);
  392.  
  393.                         if (style2 != null)
  394.                         {
  395.                             if (maxLength == -1)
  396.                                 _FromData[id] = GUI.TextField(position, text, style2);
  397.                             else
  398.                                 _FromData[id] = GUI.TextField(position, text, maxLength, style2);
  399.                         }
  400.                         else
  401.                         {
  402.                             if (maxLength == -1)
  403.                                 _FromData[id] = GUI.TextField(position, text);
  404.                             else
  405.                                 _FromData[id] = GUI.TextField(position, text, maxLength);
  406.                         }
  407.                     }
  408.                     break;
  409.  
  410.                 case "Toggle":
  411.                     {
  412.                         string id = getXmlAttributesValue(ui, "id", string.Empty);
  413.                         if (string.IsNullOrEmpty(id))
  414.                         {
  415.                             DebugOut(ui);
  416.                             continue;
  417.                         }
  418.  
  419.                         bool value = false;
  420.                         bool value2 = false;
  421.                         if (_FromData[id] != null)
  422.                             bool.TryParse(_FromData[id], out value);
  423.                         else if (ui.Attributes["value"] != null)
  424.                             bool.TryParse(ui.Attributes["value"].Value, out value);
  425.  
  426.                         if (style2 != null)
  427.                             value2 = GUI.Toggle(position, value, content2, style2);
  428.                         else
  429.                             value2 = GUI.Toggle(position, value, content2);
  430.  
  431.                         _FromData[id] = value2.ToString();
  432.                     }
  433.                     break;
  434.             }
  435.         }
  436.     }
  437.  
  438.     private void xmlForm()
  439.     {
  440.         try
  441.         {
  442.             if (_FormID != _FormID2)
  443.             {
  444.                 //string GUIFile = Path.Combine(Application.dataPath, _FormID2) + ".xml";
  445.                 string GUIFile = Application.dataPath + "/" + _FormID2 + ".xml";
  446.                 if (Path.DirectorySeparatorChar != '/')
  447.                     GUIFile = GUIFile.Replace('/', Path.DirectorySeparatorChar);
  448.                 //_Document.LoadXml(File.ReadAllText(GUIFile));
  449.                 _Document.Load(GUIFile);
  450.                 _FormID = _FormID2;
  451.                 _FromData.Clear();
  452.             }
  453.  
  454.             if (_Document == null)
  455.                 return;
  456.  
  457.             XmlNodeList formList = _Document.SelectNodes("/form");
  458.             if (formList == null)
  459.                 return;
  460.             if (formList.Count == 0)
  461.                 return;
  462.  
  463.             XmlNode form = formList.Item(0);
  464.             TryParseXmlForm(form);
  465.         }
  466.         catch (Exception ex)
  467.         {
  468.             DebugOut(ex);
  469.         }
  470.     }
  471.  
  472.     //private XmlNode findXmlNode(string id)
  473.     //{
  474.     //    string find = "/form/ui[@id='" + id + "']";
  475.     //    XmlNodeList uiList = _Document.SelectNodes(find);
  476.     //    foreach (XmlNode ui in uiList)
  477.     //        return ui;
  478.     //    return null;
  479.     //}
  480.  
  481.     private string getXmlAttributesValue(XmlNode node, string name, string defaultValue)
  482.     {
  483.         XmlAttribute attr = node.Attributes[name];
  484.         if (attr != null)
  485.             return attr.Value;
  486.         else
  487.             return defaultValue;
  488.     }
  489.  
  490.     private bool TryParseRect(string src, out Rect rect)
  491.     {
  492.         string[] num = src.Split(new Char[] { ',' });
  493.         if (num.Length == 4)
  494.         {
  495.             float x, y, w, h;
  496.             if (float.TryParse(num[0], out x) &&
  497.                 float.TryParse(num[1], out y) &&
  498.                 float.TryParse(num[2], out w) &&
  499.                 float.TryParse(num[3], out h))
  500.             {
  501.                 rect = new Rect(x, y, w, h);
  502.                 return true;
  503.             }
  504.         }
  505.         rect = new Rect();
  506.         return false;
  507.     }
  508.  
  509.     private bool TryParseVector2(string src, out Vector2 vec)
  510.     {
  511.         string[] num = src.Split(new Char[] { ',' });
  512.         if (num.Length == 2)
  513.         {
  514.             float x, y;
  515.             if (float.TryParse(num[0], out x) &&
  516.                 float.TryParse(num[1], out y))
  517.             {
  518.                 vec = new Vector2(x, y);
  519.                 return true;
  520.             }
  521.         }
  522.         vec = new Vector2();
  523.         return false;
  524.     }
  525.  
  526.     private bool TryParseRectOffset(string src, out RectOffset rectOffset)
  527.     {
  528.         string[] num = src.Split(new Char[] { ',' });
  529.         if (num.Length == 4)
  530.         {
  531.             int left, right, top, bottom;
  532.             if (int.TryParse(num[0], out left) &&
  533.                 int.TryParse(num[1], out right) &&
  534.                 int.TryParse(num[2], out top) &&
  535.                 int.TryParse(num[3], out bottom))
  536.             {
  537.                 rectOffset = new RectOffset(left, right, top, bottom);
  538.                 return true;
  539.             }
  540.         }
  541.         rectOffset = null;
  542.         return false;
  543.     }
  544.  
  545.     private bool TryParseTextAnchor(string src, out TextAnchor textAnchor)
  546.     {
  547.         switch (src)
  548.         {
  549.             case "UpperLeft":
  550.                 textAnchor = TextAnchor.UpperLeft;
  551.                 break;
  552.  
  553.             case "UpperCenter":
  554.                 textAnchor = TextAnchor.UpperCenter;
  555.                 break;
  556.  
  557.             case "UpperRight":
  558.                 textAnchor = TextAnchor.UpperRight;
  559.                 break;
  560.  
  561.             case "MiddleLeft":
  562.                 textAnchor = TextAnchor.MiddleLeft;
  563.                 break;
  564.  
  565.             case "MiddleCenter":
  566.                 textAnchor = TextAnchor.MiddleCenter;
  567.                 break;
  568.  
  569.             case "MiddleRight":
  570.                 textAnchor = TextAnchor.MiddleRight;
  571.                 break;
  572.  
  573.             case "LowerLeft":
  574.                 textAnchor = TextAnchor.LowerLeft;
  575.                 break;
  576.  
  577.             case "LowerCenter":
  578.                 textAnchor = TextAnchor.LowerCenter;
  579.                 break;
  580.  
  581.             case "LowerRight":
  582.                 textAnchor = TextAnchor.LowerRight;
  583.                 break;
  584.  
  585.             default:
  586.                 textAnchor = TextAnchor.UpperLeft;
  587.                 return false;
  588.         }
  589.         return true;
  590.     }
  591.  
  592.     private bool TryParseTextClipping(string src, out TextClipping textClipping)
  593.     {
  594.         switch (src)
  595.         {
  596.             case "Overflow":
  597.                 textClipping = TextClipping.Overflow;
  598.                 break;
  599.  
  600.             case "Clip":
  601.                 textClipping = TextClipping.Clip;
  602.                 break;
  603.  
  604.             default:
  605.                 textClipping = TextClipping.Overflow;
  606.                 return false;
  607.         }
  608.         return true;
  609.     }
  610.  
  611.     private bool TryParseFontStyle(string src, out FontStyle fontStyle)
  612.     {
  613.         switch (src)
  614.         {
  615.             case "Bold":
  616.                 fontStyle = FontStyle.Bold;
  617.                 break;
  618.  
  619.             case "BoldAndItalic":
  620.                 fontStyle = FontStyle.BoldAndItalic;
  621.                 break;
  622.  
  623.             case "Italic":
  624.                 fontStyle = FontStyle.Italic;
  625.                 break;
  626.  
  627.             case "Normal":
  628.                 fontStyle = FontStyle.Normal;
  629.                 break;
  630.  
  631.             default:
  632.                 fontStyle = FontStyle.Normal;
  633.                 return false;
  634.         }
  635.         return true;
  636.     }
  637.  
  638.     private bool TryParseImagePosition(string src, out ImagePosition imagePosition)
  639.     {
  640.         switch (src)
  641.         {
  642.             case "ImageLeft":
  643.                 imagePosition = ImagePosition.ImageLeft;
  644.                 break;
  645.  
  646.             case "ImageAbove":
  647.                 imagePosition = ImagePosition.ImageAbove;
  648.                 break;
  649.  
  650.             case "ImageOnly":
  651.                 imagePosition = ImagePosition.ImageOnly;
  652.                 break;
  653.  
  654.             case "TextOnly":
  655.                 imagePosition = ImagePosition.TextOnly;
  656.                 break;
  657.  
  658.             default:
  659.                 imagePosition = ImagePosition.ImageLeft;
  660.                 return false;
  661.         }
  662.         return true;
  663.     }
  664.  
  665.     private bool TryParseStyleState(string src, out GUIStyleState styleState)
  666.     {
  667.         //TODO: TryParseStyleState
  668.         styleState = new GUIStyleState();
  669.         return true;
  670.     }
  671.  
  672.     private Texture2D getTexture2D(string image)
  673.     {
  674.         Texture2D ret = _uiTexture2D[image];
  675.         if (ret == null)
  676.         {
  677.             ret = Resources.Load(image) as Texture2D;
  678.             _uiTexture2D.Add(image, ret);
  679.         }
  680.  
  681.         return ret;
  682.     }
  683.  
  684.     private void TryParseStyle(string type, string class2, string src, out GUIStyle style2)
  685.     {
  686.         if (!string.IsNullOrEmpty(class2))
  687.             style2 = _uiStyle[class2];
  688.         else if (!string.IsNullOrEmpty(type))
  689.             style2 = new GUIStyle(GUI.skin.GetStyle(type));
  690.         else
  691.             style2 = new GUIStyle();
  692.  
  693.         if (string.IsNullOrEmpty(src))
  694.             return;
  695.  
  696.         string[] style = src.Split(new Char[] { ';' });
  697.         string key, value;
  698.  
  699.         foreach (string str in style)
  700.         {
  701.             string[] s = str.Split(new Char[] { ':' });
  702.             if (s.Length == 2)
  703.             {
  704.                 key = s[0].Trim();
  705.                 value = s[1].Trim();
  706.  
  707.                 switch (key)
  708.                 {
  709.                     case "active":
  710.                         {
  711.                             GUIStyleState styleState;
  712.                             if (TryParseStyleState(value, out styleState))
  713.                                 style2.active = styleState;
  714.                         }
  715.                         break;
  716.  
  717.                     case "alignment":
  718.                         {
  719.                             TextAnchor textAnchor;
  720.                             if (TryParseTextAnchor(value, out textAnchor))
  721.                                 style2.alignment = textAnchor;
  722.                         }
  723.                         break;
  724.  
  725.                     case "border":
  726.                         {
  727.                             RectOffset rectOffset;
  728.                             if (TryParseRectOffset(value, out rectOffset))
  729.                                 style2.border = rectOffset;
  730.                         }
  731.                         break;
  732.  
  733.                     case "clipping":
  734.                         {
  735.                             TextClipping textClipping;
  736.                             if (TryParseTextClipping(value, out textClipping))
  737.                                 style2.clipping = textClipping;
  738.                         }
  739.                         break;
  740.  
  741.                     //過時功能
  742.                     //case "clipOffset":
  743.                     //    {
  744.                     //        Vector2 vec;
  745.                     //        TryParseVector2(value, out vec);
  746.                     //        style2.clipOffset = vec;
  747.                     //    }
  748.                     //    break;
  749.  
  750.                     case "contentOffset":
  751.                         {
  752.                             Vector2 vec;
  753.                             if (TryParseVector2(value, out vec))
  754.                                 style2.contentOffset = vec;
  755.                         }
  756.                         break;
  757.  
  758.                     case "fixedHeight":
  759.                         {
  760.                             float fixedHeight;
  761.                             if (float.TryParse(value, out fixedHeight))
  762.                                 style2.fixedHeight = fixedHeight;
  763.                         }
  764.                         break;
  765.  
  766.                     case "fixedWidth":
  767.                         {
  768.                             float fixedWidth;
  769.                             if (float.TryParse(value, out fixedWidth))
  770.                                 style2.fixedWidth = fixedWidth;
  771.                         }
  772.                         break;
  773.  
  774.                     case "focused":
  775.                         {
  776.                             GUIStyleState styleState;
  777.                             if (TryParseStyleState(value, out styleState))
  778.                                 style2.focused = styleState;
  779.                         }
  780.                         break;
  781.  
  782.                     case "font":
  783.                         {
  784.                             Font font = new Font(value);
  785.                             style2.font = font;
  786.                         }
  787.                         break;
  788.  
  789.                     case "fontSize":
  790.                         {
  791.                             int fontSize;
  792.                             if (int.TryParse(value, out fontSize))
  793.                                 style2.fontSize = fontSize;
  794.                         }
  795.                         break;
  796.  
  797.                     case "fontStyle":
  798.                         {
  799.                             FontStyle fontStyle;
  800.                             if (TryParseFontStyle(value, out fontStyle))
  801.                                 style2.fontStyle = fontStyle;
  802.                         }
  803.                         break;
  804.  
  805.                     case "hover":
  806.                         {
  807.                             GUIStyleState styleState;
  808.                             if (TryParseStyleState(value, out styleState))
  809.                                 style2.hover = styleState;
  810.                         }
  811.                         break;
  812.  
  813.                     case "imagePosition":
  814.                         {
  815.                             ImagePosition imagePosition;
  816.                             if (TryParseImagePosition(value, out imagePosition))
  817.                                 style2.imagePosition = imagePosition;
  818.                         }
  819.                         break;
  820.  
  821.                     case "margin":
  822.                         {
  823.                             RectOffset rectOffset;
  824.                             if (TryParseRectOffset(value, out rectOffset))
  825.                                 style2.margin = rectOffset;
  826.                         }
  827.                         break;
  828.  
  829.                     case "normal":
  830.                         {
  831.                             GUIStyleState styleState;
  832.                             if (TryParseStyleState(value, out styleState))
  833.                                 style2.normal = styleState;
  834.                         }
  835.                         break;
  836.  
  837.                     case "onActive":
  838.                         {
  839.                             GUIStyleState styleState;
  840.                             if (TryParseStyleState(value, out styleState))
  841.                                 style2.onActive = styleState;
  842.                         }
  843.                         break;
  844.  
  845.                     case "onFocused":
  846.                         {
  847.                             GUIStyleState styleState;
  848.                             if (TryParseStyleState(value, out styleState))
  849.                                 style2.onFocused = styleState;
  850.                         }
  851.                         break;
  852.  
  853.                     case "onHover":
  854.                         {
  855.                             GUIStyleState styleState;
  856.                             if (TryParseStyleState(value, out styleState))
  857.                                 style2.onHover = styleState;
  858.                         }
  859.                         break;
  860.  
  861.                     case "onNormal":
  862.                         {
  863.                             GUIStyleState styleState;
  864.                             if (TryParseStyleState(value, out styleState))
  865.                                 style2.onNormal = styleState;
  866.                         }
  867.                         break;
  868.  
  869.                     case "overflow":
  870.                         {
  871.                             RectOffset rectOffset;
  872.                             if (TryParseRectOffset(value, out rectOffset))
  873.                                 style2.padding = rectOffset;
  874.                         }
  875.                         break;
  876.  
  877.                     case "padding":
  878.                         {
  879.                             RectOffset rectOffset;
  880.                             if (TryParseRectOffset(value, out rectOffset))
  881.                                 style2.padding = rectOffset;
  882.                         }
  883.                         break;
  884.  
  885.                     case "richText":
  886.                         {
  887.                             bool richText;
  888.                             if (bool.TryParse(value, out richText))
  889.                                 style2.richText = richText;
  890.                         }
  891.                         break;
  892.  
  893.                     case "stretchHeight":
  894.                         {
  895.                             bool stretchHeight;
  896.                             if (bool.TryParse(value, out stretchHeight))
  897.                                 style2.stretchHeight = stretchHeight;
  898.                         }
  899.                         break;
  900.  
  901.                     case "stretchWidth":
  902.                         {
  903.                             bool stretchWidth;
  904.                             if (bool.TryParse(value, out stretchWidth))
  905.                                 style2.stretchWidth = stretchWidth;
  906.                         }
  907.                         break;
  908.  
  909.                     case "wordWrap":
  910.                         {
  911.                             bool wordWrap;
  912.                             if (bool.TryParse(value, out wordWrap))
  913.                                 style2.wordWrap = wordWrap;
  914.                         }
  915.                         break;
  916.                 }
  917.             }
  918.         }
  919.     }
  920.  
  921.     private void onButtonClick(XmlNode node)
  922.     {
  923.         if (node != null)
  924.         {
  925.             string method = getXmlAttributesValue(node, "method", "");
  926.             string action = getXmlAttributesValue(node, "action", "");
  927.  
  928.             if (string.IsNullOrEmpty(method))
  929.             {
  930.                 switch (method)
  931.                 {
  932.                     case "browser":
  933.                         Application.OpenURL(action);
  934.                         break;
  935.  
  936.                     case "call":
  937.                         callFunc(action);
  938.                         break;
  939.  
  940.                     case "post":
  941.                         if (string.IsNullOrEmpty(action))
  942.                             xmlFormPost(action);
  943.                         break;
  944.  
  945.                     case "reset":
  946.                         _FormID2 = _FormID;
  947.                         _FormID = "";
  948.                         break;
  949.  
  950.                     case "form":
  951.                         _FormID2 = action;
  952.                         _FormID = "";
  953.                         break;
  954.  
  955.                     case "quit":
  956.                         Application.Quit();
  957.                         break;
  958.                 }
  959.             }
  960.         }
  961.     }
  962.  
  963.     private void callFunc(string action)
  964.     {
  965.     }
  966.  
  967.     private IEnumerator xmlFormPost(string strUrl)
  968.     {
  969.         yield return new WaitForEndOfFrame();
  970.  
  971.         if (_IsExistConfig == true)
  972.         {
  973.             var form = new WWWForm();
  974.  
  975.             foreach (string key in _FromData.Keys)
  976.                 form.AddField(key, _FromData[key]);
  977.  
  978.             string strUrl2;
  979.             Uri url = new Uri(strUrl);
  980.             if ((url.Scheme == "http") || (url.Scheme == "http"))
  981.                 strUrl2 = strUrl;
  982.             else
  983.                 strUrl2 = (new Uri(_BaseUri, strUrl)).PathAndQuery;
  984.  
  985.             var http = new WWW(strUrl2, form.data, _headers);
  986.             yield return http;
  987.  
  988.             if (!String.IsNullOrEmpty(http.error))
  989.             {
  990.                 DebugOut(http);
  991.             }
  992.             else
  993.             {
  994.                 string Location = http.responseHeaders["Location"];
  995.                 if (!string.IsNullOrEmpty(Location))
  996.                 {
  997.                     _FormID2 = Location;
  998.                 }
  999.                 else
  1000.                 {
  1001.                     _FormID = _FormID2 = "";
  1002.                     _Document.LoadXml(http.text);
  1003.                 }
  1004.             }
  1005.         }
  1006.     }
  1007.  
  1008.     private void DebugOut(object obj)
  1009.     {
  1010.         if (obj is string)
  1011.         {
  1012.             //Debug.Log(msg);
  1013.             print(obj as string);
  1014.         }
  1015.         else if (obj is Exception)
  1016.         {
  1017.             print((obj as Exception).Message);
  1018.         }
  1019.         else if (obj is WWW)
  1020.         {
  1021.             print((obj as WWW).error);
  1022.         }
  1023.         else
  1024.         {
  1025.             XmlWriter w = XmlWriter.Create("");
  1026.             (obj as XmlNode).WriteTo(w);
  1027.             print(w.ToString());
  1028.         }
  1029.     }
  1030. }
  1031.  
  1032.  
  1033. //--------------------------------------
  1034. //File: ConfigInfo.cs
  1035. using System;
  1036. using System.Xml;
  1037. using System.Xml.Serialization;
  1038.  
  1039. [XmlRoot("root")]
  1040. public class ConfigInfo {
  1041.     [XmlAttribute("GroupID")]
  1042.     public string GroupID = "";
  1043.    
  1044.     [XmlAttribute("LoginID")]
  1045.     public string LoginID = "";
  1046.    
  1047.     [XmlAttribute("Password")]
  1048.     public string Password = "";
  1049. }
  1050.  
  1051.  
  1052.  
  1053. //--------------------------------------
  1054. //File: Config.xml
  1055. <?xml version="1.0" encoding="utf-8"?>
  1056. <form>
  1057.  <Box text="設定使用者" rect="0,0,240,240" />
  1058.  <Label text="案場代號:" rect="20,60,80,60" />
  1059.  <TextField id="GroupID" rect="40,80,160,20" maxLength="20" />
  1060.  <Label text="帳號:" rect="20,100,40,20" />
  1061.  <TextField id="LoginID" rect="40,120,160,20" maxLength="20" />
  1062.  <Label text="密碼:" rect="20,140,40,20" />
  1063.  <TextField id="Password" rect="40,160,160,20" maxLength="20" />
  1064.  <Button text="儲存" rect="20,200,100,20" action="saveConfig" method="call" />
  1065.  <Button text="取消" rect="120,200,100,20" action="loadConfig" method="call" />
  1066. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement