在一些交互软件中,当点击按钮时,一般都有对应的内容显示出来,在这里我只是把资源分成了图片,文字和视频。我将文件放置在对应文件夹里,通过xml配置好文件路径,再在脚本里将按配置好的路径读取对应文件。所有我用了两个脚本来完成读取xml和文件。
读取xml的代码:
public class XmlReader : MonoBehaviour
{
private static XmlDocument xmlDocument;
/// <summary>
/// 得到某个元素的指定属性值
/// </summary>
public static string getAttributeValue (XmlElement xmlElement, string attributeName)
{
return xmlElement.GetAttribute (attributeName).ToString ().Trim ();
}
/// <summary>
/// 得到某个元素的内容
/// </summary>
public static string getInnerTxt (XmlElement xmlElement)
{
return xmlElement.InnerText.ToString ().Trim ();
}
/// <summary>
/// 获取到所有此标签名的所有节点
/// </summary>
public static XmlNodeList GetNodeList (string tagName)
{
return xmlDocument.GetElementsByTagName (tagName);
}
public static void initXml (string xmlName)
{
string Path = Application.streamingAssetsPath + "/Config/" + xmlName + ".xml";
xmlDocument = new XmlDocument ();
xmlDocument.Load (Path);
}
}
读取文件的部分代码:
public static List<string> getFileAllPath (string path)
{
List<string> strList = new List<string> ();
//string fileName="";
foreach (string s in getFileList (path))
{
if (s.EndsWith (".meta"))
{
continue;
}
//fileName=getName(path.Replace(@"\","/"));
//?是否需要输出到文件里,方便以后检测问题
//if(isContainFormat(fileName))
strList.Add (s);
}
return strList;
}
public static string getName (string path)
{
string fileName = path.Split ('/')[path.Split ('/').Length - 1];
return fileName;
}
public static string[] getFileList (string path)
{
if (Directory.Exists (path))
return Directory.GetFiles (path);
else
{
Debug.Log ("文件路径不存在 ");
return null;
}
}
为了将资源分好类,用MButton脚本表示按钮:
public string buttonName;
MResource表示资源:
public string[] TxtNameArry
{
get;
set;
}
public string[] PictureNameArry
{
get;
set;
}
public string[] VideoNameArry
{
get;
set;
}
public Dictionary<string, string> TxtURLDIC
{
get;
set;
}
public Dictionary<string, string> VideoURLDIC
{
get;
set;
}
public Dictionary<string, string> PictureURLDIC
{
get;
set;
}
public Dictionary<string, Texture2D> Texture2DDIC
{
get;
set;
}
在另一个脚本Resource里将资源进行了汇总(通过调用xmlreader和mfile的方法),通过按钮名字,即Mbutton的名字字段来进行查找。
public class Resource : MonoBehaviour
{
/// <summary>
/// 按钮对应的sprite
/// </summary>
public static Dictionary<string, Sprite> ButtonSpriteDIC
{
get;
set;
}
/// <summary>
/// 按钮对应的资源
/// </summary>
public static Dictionary<string, MResource> ButtonResourceDIC
{
get;
set;
}
public static void loadButtonSprite (string xmlName)
{
ButtonSpriteDIC = new Dictionary<string, Sprite> ();
XmlReader.initXml (xmlName);
//所有按钮
XmlNodeList mbuttonNodelist = XmlReader.GetNodeList ("mbutton");
foreach (XmlNode xn in mbuttonNodelist)
{
if (XmlReader.getAttributeValue ((XmlElement)xn, "url") != "")
{
string spriteUrl = Application.streamingAssetsPath + "/" + XmlReader.getAttributeValue ((XmlElement)xn, "url");
//if(!ButtonSpriteDIC.ContainsKey(XmlReader.getAttributeValue ((XmlElement) xn, "name")))
ButtonSpriteDIC.Add (XmlReader.getAttributeValue ((XmlElement)xn, "name"), MFile.getSprite (spriteUrl, 100, 100));
}
}
}
public static void loadButtonResource (string xmlName, bool listOrdictionary)
{
ButtonResourceDIC = new Dictionary<string, MResource> ();
XmlReader.initXml (xmlName);
//所有按钮
XmlNodeList mbuttonNodelist = XmlReader.GetNodeList ("mbutton");
MResource mResource = new MResource ();
mResource.init (listOrdictionary);
//遍历按钮s
foreach (XmlNode xn in mbuttonNodelist)
{
//按钮子节点
XmlNodeList xbChildList = xn.ChildNodes;
//遍历按钮子节点
foreach (XmlNode rn in xbChildList)
{
if (rn.Name.Equals ("mpic"))
{
if (XmlReader.getAttributeValue ((XmlElement)rn, "url") != "")
{
//图片的目录
string picUrl = Application.streamingAssetsPath + "/" + XmlReader.getAttributeValue ((XmlElement)rn, "url");
switch (listOrdictionary)
{
case true:
XmlNodeList rChildNodeList = rn.ChildNodes;
//遍历资源子节点
foreach (XmlNode r in rChildNodeList)
{
mResource.Texture2DDIC.Add (XmlReader.getAttributeValue ((XmlElement)r, "name"), MFile.getTexture2D (picUrl, 100, 100));
}
break;
case false:
mResource.Texture2DList = MFile.getTexture2Ds (MFile.getFileAllPath (picUrl), 100, 100);
break;
}
}
continue;
}
if (rn.Name.Equals ("mvideo"))
{
if (XmlReader.getAttributeValue ((XmlElement)rn, "url") != "")
{
string videoUrl = Application.streamingAssetsPath + "/" + XmlReader.getAttributeValue ((XmlElement)rn, "url");
XmlNodeList rChildNodeList = rn.ChildNodes;
foreach (XmlNode r in rChildNodeList)
{
string videoFullUrl = videoUrl + "/" + XmlReader.getInnerTxt ((XmlElement)r);
mResource.VideoURLDIC.Add (XmlReader.getAttributeValue ((XmlElement)r, "name"), videoFullUrl);
}
}
continue;
}
if (rn.Name.Equals ("mtxt"))
{
if (XmlReader.getAttributeValue ((XmlElement)rn, "url") != "")
{
string txtUrl = Application.streamingAssetsPath + "/" + XmlReader.getAttributeValue ((XmlElement)rn, "url");
XmlNodeList rChildNodeList = rn.ChildNodes;
foreach (XmlNode r in rChildNodeList)
{
string txtFullUrl = txtUrl + "/" + XmlReader.getInnerTxt ((XmlElement)r);
mResource.VideoURLDIC.Add (XmlReader.getAttributeValue ((XmlElement)r, "name"), txtFullUrl);
}
}
continue;
}
}
mResource.getAllName (listOrdictionary);
ButtonResourceDIC.Add (XmlReader.getAttributeValue ((XmlElement)xn, "name"), mResource);
}
}
}
}
上图是我解决的一个大概思路。
不是拿来开发游戏的项目,只是用unity弄的交互,点击按钮反馈给用户相关信息。为了以后方便使用做成了插件的形式。
链接:https://pan.baidu.com/s/1jJWfw9K 密码:1j3k