配合单例类一起使用,可以获取Xml表中存储的模型的具体位置,转换成可以直接使用的Vector3和Vector4类型的变量。
using UnityEngine;
using System.Collections;
public class StringCommon : Singleton<StringCommon>
{
//去除字符串中的逗号
public string[] SplitStringWithComma(string str)
{
string[] temp = str.Split(',');
return temp;
}
//获取Vector3类型的变量
public Vector3 GetVector3Value(string value)
{
string newPos = value.Replace("(", "");
string lastPos = newPos.Replace(")", "");
Vector3 vector3Value = new Vector3(
float.Parse(SplitStringWithComma(lastPos)[0]),
float.Parse(SplitStringWithComma(lastPos)[1]),
float.Parse(SplitStringWithComma(lastPos)[2])
);
return vector3Value;
}
//获取Vector4类型的变量
public Vector4 GetVector4Value(string value)
{
string newPos = value.Replace("(", "");
string lastPos = newPos.Replace(")", "");
Vector4 vector4Value = new Vector4(
float.Parse(SplitStringWithComma(lastPos)[0]),
float.Parse(SplitStringWithComma(lastPos)[1]),
float.Parse(SplitStringWithComma(lastPos)[2]),
float.Parse(SplitStringWithComma(lastPos)[3])
);
return vector4Value;
}
}
因为xml表中的内容都是直接代码生成的类似这样:Position=”(6.29, -23.83, -11.58)”
所以想使用可以通过上面的脚本来转换一下成为new Vector3(6.29, -23.83, -11.58);
如果有更好的方法,希望能一起探讨,xml表是通过CreateSceneElements脚本实现,内容如下:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
public class CreateSenceElements : EditorWindow
{
[MenuItem("Sence/"+"生成场景元素XML表")]
static void SenceElements()
{
GameObject obj_Father = Selection.activeGameObject;
string filepath = Application.dataPath + "/StreamingAssets" + @"/" + obj_Father.name + ".xml";
//创建XML文档实例
XmlDocument xmlDoc = new XmlDocument();
//创建root节点,也就是最上一层节点
XmlElement root=null;
XmlElement rootSence = null;
XmlElement rootPrefab = null;
if (!File.Exists(filepath))
{
//创建root节点,也就是最上一层节点
root = xmlDoc.CreateElement(obj_Father.name);
rootSence = xmlDoc.CreateElement("SenceElement");
rootPrefab = xmlDoc.CreateElement("SencePrefab");
root.SetAttribute("ID", "ID001");
Debug.Log("createXml OK!");
}
else
{
xmlDoc.Load(filepath);
root = xmlDoc.SelectSingleNode(obj_Father.name) as XmlElement;
rootSence = xmlDoc.SelectSingleNode("SenceElement") as XmlElement;
rootPrefab = xmlDoc.SelectSingleNode("SencePrefab") as XmlElement;
root.RemoveAll();
}
//保存场景中元素的名称
Dictionary<string, int> dicPrefabs = new Dictionary<string, int>();
FindChild(ref xmlDoc, ref rootSence, dicPrefabs, obj_Father.transform, 0);
foreach (string key in dicPrefabs.Keys)
{
XmlElement _ele = xmlDoc.CreateElement("PrefabNode");
_ele.SetAttribute("name", key);
_ele.SetAttribute("num", dicPrefabs[key].ToString());
rootPrefab.AppendChild(_ele);
}
root.AppendChild(rootPrefab);
root.AppendChild(rootSence);
xmlDoc.AppendChild(root);
//把XML文件保存至本地
xmlDoc.Save(filepath);
AssetDatabase.Refresh();
}
static void FindChild(ref XmlDocument doc, ref XmlElement node,Dictionary<string,int> dic, Transform tran ,int level)
{
++level;
foreach (Transform child in tran)
{
XmlElement _ele = doc.CreateElement("SenceNode" + level.ToString());
_ele.SetAttribute("name", child.name);
_ele.SetAttribute("Position", child.localPosition.ToString("0.00"));
_ele.SetAttribute("EulerAngles", child.localEulerAngles.ToString("0.00"));
_ele.SetAttribute("Scale", child.localScale.ToString("0.00"));
MeshFilter _mesh = child.GetComponent<MeshFilter>();
if (_mesh!=null)
{
string _str = _mesh.sharedMesh.name;
_ele.SetAttribute("Prefab", _str);
MeshRenderer _render = child.gameObject.GetComponent<MeshRenderer>();
_ele.SetAttribute("LightMapScale", _render.lightmapScaleOffset.ToString("0.000000"));
_ele.SetAttribute("LightMapIndex", _render.lightmapIndex.ToString());
if (dic.ContainsKey(_str))
{
dic[_str] = dic[_str] + 1;
}
else dic.Add(_str, 1);
}
if (child.childCount >= 1)
{
FindChild(ref doc, ref _ele,dic, child, level);
}
node.AppendChild(_ele);
}
}
}