您的位置:首页 > 其它

关于资源版本管理的问题

2012-07-16 10:35 344 查看
unity3d 提供了比读取txt,xml更好用的ScriptableObject(脚本对象化)。为了方便于版本管理,一般会添加一个版本号(guid)

public class ScriptObjectCOS : ScriptableObject
{
public string guid = "";
}

public class LevelPortalRefTable : ScriptObjectCOS
{
public List<LevelPortalRef> levelPortalRefList = new List<LevelPortalRef>();
}

//每个关卡的portral
[System.Serializable]
public class LevelPortalRef
{
public int levelID;
public string levelName = string.Empty;
public List<PortalInstance> PortalInstanceList = new List<PortalInstance>();

//< 1.portalID, 2.PortalInstance>
private Dictionary<int, PortalInstance> cacheLevelPortalRefTable = new Dictionary<int, PortalInstance>();

//缓存所有PortalInstance
public static void PopulatePortalInstanceTable(LevelPortalRef _ref)
{
if (_ref == null) {
return;
}
foreach (PortalInstance _portal in _ref.PortalInstanceList) {
if (!_ref.cacheLevelPortalRefTable.ContainsKey(_portal.portalID)) {
_ref.cacheLevelPortalRefTable.Add(_portal.portalID, _portal);
} else {
Debug.LogError("PortalInstance repeat: " + _ref.levelID + " " + _portal.portalID);
}
}
}

//根据portalID获取PortalInstance
public PortalInstance GetPortalInstanceData(int _portalID)
{
if (cacheLevelPortalRefTable.ContainsKey(_portalID)) {
return cacheLevelPortalRefTable[_portalID];
}
return null;
}
}

[System.Serializable]
public class PortalInstance
{
public int portalID;
public int x;
public float y;
public int z;
public int soloDestinationPortalID;//目标关卡的portal
public string soloDestinationLevelName = string.Empty;//目标关卡名字
public int soloDestinationLevelID;//目标关卡ID
}


LevelPortalRelationShipBatchProcess.cs

using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
using System.Collections.Generic;
//using Aspose.Cells;
using System;
using UnityEditor;
using System.Xml;
public class LevelPortalRelationShipBatchProcess : ScriptableWizard
{
private static string filePath = "MyAssets/LevelPortalRelationShipData.asset";
static string path = @"d:\MySavedLevels\LevelMap\";
public static String[] filePaths;
private static Dictionary<int, LevelPortalRef> levelPortalRefTable = new Dictionary<int, LevelPortalRef>();
[MenuItem("Example/Portal/LevelPortalRelationShipBatchProcess")]
static void CreatePortalRelationShipData()
{
filePath = "MyAssets/LevelPortalRelationShipData.asset";
string _guid = Guid.NewGuid().ToString();
BuiltToScriptTableObject(_guid);
WriteToXML(_guid);
Debug.Log("  CreatePortalRelationShipData   Done");
}

private static void BuiltToScriptTableObject(string _guid)
{
//先创建一个空的.asset
LevelPortalRefTable _newTable = GenericAssetUtility<LevelPortalRefTable>.Create(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath));
_newTable.guid = _guid;
//获取数据
if (Directory.Exists(path))
{
filePaths = Directory.GetFiles(path, "*.xml");
}
foreach (string _filePath  in filePaths)
{
Debug.LogWarning("################   url: " + _filePath);
ReadMapXML(_filePath);
}
//Debug.Log("################ BuiltToScriptTableObject() " + levelPortalRefTable.Count);
foreach (LevelPortalRef _protalRef in levelPortalRefTable.Values)
{
LevelPortalRef levelPortalRef = new LevelPortalRef();
levelPortalRef.levelID = _protalRef.levelID;
levelPortalRef.levelName = _protalRef.levelName;
levelPortalRef.dungeonID = _protalRef.dungeonID;
foreach (PortalInstance _portal in _protalRef.PortalInstanceList)
{
PortalInstance portalData = new PortalInstance();
portalData.portalID = _portal.portalID; Debug.Log(" PortalID:  " + portalData.portalID);
portalData.x = _portal.x;
portalData.y = _portal.y;
portalData.z = _portal.z;
portalData.soloDestinationPortalID = _portal.soloDestinationPortalID;
portalData.soloDestinationLevelID = _portal.soloDestinationLevelID;
portalData.soloDestinationLevelName = _portal.soloDestinationLevelName;
levelPortalRef.PortalInstanceList.Add(portalData);
}
_newTable.levelPortalRefList.Add(levelPortalRef);
}

}

static void ReadMapXML(string _filePath)
{
try
{
XmlDocument _doc = new XmlDocument();
_doc.Load(_filePath);
XmlElement _root = _doc.DocumentElement;
if (_root.Name.ToUpper().Equals("LEVEL") || _root.Name.ToUpper().Equals("OPENLEVEL"))
{
LevelPortalRef _levelPortalRef = new LevelPortalRef();//每个关卡
XmlNodeList elemList = _root.ChildNodes;
foreach (XmlNode xmlNode in elemList)
{
if (xmlNode != null)
{
#region  every Level

string _tagName = xmlNode.Name.ToUpper();
switch (_tagName)
{
case "ID":
_levelPortalRef.levelID = Convert.ToInt32(xmlNode.InnerXml);
break;
case "Name":
_levelPortalRef.levelName = xmlNode.InnerXml;
break;
case "DEMGUON":
_levelPortalRef.dungeonID = Convert.ToInt32(xmlNode.InnerXml);
break;
case "PORTALS":
XmlNodeList _portalList = xmlNode.ChildNodes;
foreach (XmlNode _portalNode in _portalList)
{
PortalInstance _portalInstance = new PortalInstance();
if (_portalNode.Name.ToUpper().Equals("PORTAL") && _portalNode != null)
{
#region  every portal
XmlNodeList _nodesList = _portalNode.ChildNodes;
foreach (XmlNode _portalElement in _nodesList)
{
string _elementName = _portalElement.Name.ToUpper();
if (!string.IsNullOrEmpty(_portalElement.InnerXml))
{
switch (_elementName)
{
case "ID":
//Debug.Log(_elementName + "    " + _portalElement.InnerXml);
_portalInstance.portalID = Convert.ToInt32(_portalElement.InnerXml);
break;
case "X":
_portalInstance.x = (int)Convert.ToSingle(_portalElement.InnerXml);
break;
case "Y":
_portalInstance.y = Convert.ToSingle(_portalElement.InnerXml);
break;
case "Z":
_portalInstance.z = (int)Convert.ToSingle(_portalElement.InnerXml);
break;
/////////////////////////因为要兼容旧的格式/////////
case "DESTINATIONPORTALID":
_portalInstance.soloDestinationPortalID = Convert.ToInt32(_portalElement.InnerXml);
break;
case "DESTINATIONLEVELNAME":
_portalInstance.soloDestinationLevelName = _portalElement.InnerXml;
break;
case "DESTINATIONLEVELID":
_portalInstance.soloDestinationLevelID = Convert.ToInt32(_portalElement.InnerXml);
break;
///////////////////////////////////////////////////
/////////////////////////新的格式//////////////////
case "SoloDestination":
foreach (XmlNode solodes in _portalElement)
{
string _soldesName = solodes.Name.ToUpper();
switch (_soldesName)
{
case "SOLODESTINATIONPORTALID":
_portalInstance.soloDestinationPortalID = Convert.ToInt32(solodes.InnerXml);
break;
case "SOLODESTINATIONLEVELNAME":
_portalInstance.soloDestinationLevelName = solodes.InnerXml;
break;
case "SOLODESTINATIONLEVELID":
_portalInstance.soloDestinationLevelID = Convert.ToInt32(solodes.InnerXml);
break;
}
}
break;
///////////////////////////////////////////////////
default:
break;
}
}

}
#endregion
}
_levelPortalRef.PortalInstanceList.Add(_portalInstance);
}
break;
default:
break;
}

#endregion
}
}
levelPortalRefTable[_levelPortalRef.levelID] = _levelPortalRef;
}
}
catch (System.Exception ex)
{
Debug.LogError(ex.ToString());
}

}

private static void WriteToXML(string _guid)
{

}
}


然后把创建出来的,已经带有数据的*.assets文件直接打包成*.unity3d文件。

客户端可以直接读取*.unity3d文件,获取到数据
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: