关于资源版本管理的问题
unity3d 提供了比读取txt,xml更好用的ScriptableObject(脚本对象化)。为了方便于版本管理,一般会添加一个版本号(guid)
1 public class ScriptObjectCOS : ScriptableObject 2 { 3 public string guid = ""; 4 } 5 6 public class LevelPortalRefTable : ScriptObjectCOS 7 { 8 public List<LevelPortalRef> levelPortalRefList = new List<LevelPortalRef>(); 9 } 10 11 //每个关卡的portral 12 [System.Serializable] 13 public class LevelPortalRef 14 { 15 public int levelID; 16 public string levelName = string.Empty; 17 public List<PortalInstance> PortalInstanceList = new List<PortalInstance>(); 18 19 //< 1.portalID, 2.PortalInstance> 20 private Dictionary<int, PortalInstance> cacheLevelPortalRefTable = new Dictionary<int, PortalInstance>(); 21 22 //缓存所有PortalInstance 23 public static void PopulatePortalInstanceTable(LevelPortalRef _ref) 24 { 25 if (_ref == null) { 26 return; 27 } 28 foreach (PortalInstance _portal in _ref.PortalInstanceList) { 29 if (!_ref.cacheLevelPortalRefTable.ContainsKey(_portal.portalID)) { 30 _ref.cacheLevelPortalRefTable.Add(_portal.portalID, _portal); 31 } else { 32 Debug.LogError("PortalInstance repeat: " + _ref.levelID + " " + _portal.portalID); 33 } 34 } 35 } 36 37 //根据portalID获取PortalInstance 38 public PortalInstance GetPortalInstanceData(int _portalID) 39 { 40 if (cacheLevelPortalRefTable.ContainsKey(_portalID)) { 41 return cacheLevelPortalRefTable[_portalID]; 42 } 43 return null; 44 } 45 } 46 47 48 [System.Serializable] 49 public class PortalInstance 50 { 51 public int portalID; 52 public int x; 53 public float y; 54 public int z; 55 public int soloDestinationPortalID;//目标关卡的portal 56 public string soloDestinationLevelName = string.Empty;//目标关卡名字 57 public int soloDestinationLevelID;//目标关卡ID 58 }
LevelPortalRelationShipBatchProcess.cs
1 using UnityEngine; 2 using System.Collections; 3 using System.IO; 4 using System.Text; 5 using System.Collections.Generic; 6 //using Aspose.Cells; 7 using System; 8 using UnityEditor; 9 using System.Xml; 10 public class LevelPortalRelationShipBatchProcess : ScriptableWizard 11 { 12 private static string filePath = "MyAssets/LevelPortalRelationShipData.asset"; 13 static string path = @"d:\MySavedLevels\LevelMap\"; 14 public static String[] filePaths; 15 private static Dictionary<int, LevelPortalRef> levelPortalRefTable = new Dictionary<int, LevelPortalRef>(); 16 [MenuItem("Example/Portal/LevelPortalRelationShipBatchProcess")] 17 static void CreatePortalRelationShipData() 18 { 19 filePath = "MyAssets/LevelPortalRelationShipData.asset"; 20 string _guid = Guid.NewGuid().ToString(); 21 BuiltToScriptTableObject(_guid); 22 WriteToXML(_guid); 23 Debug.Log(" CreatePortalRelationShipData Done"); 24 } 25 26 27 private static void BuiltToScriptTableObject(string _guid) 28 { 29 //先创建一个空的.asset 30 LevelPortalRefTable _newTable = GenericAssetUtility<LevelPortalRefTable>.Create(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath)); 31 _newTable.guid = _guid; 32 //获取数据 33 if (Directory.Exists(path)) 34 { 35 filePaths = Directory.GetFiles(path, "*.xml"); 36 } 37 foreach (string _filePath in filePaths) 38 { 39 Debug.LogWarning("################ url: " + _filePath); 40 ReadMapXML(_filePath); 41 } 42 //Debug.Log("################ BuiltToScriptTableObject() " + levelPortalRefTable.Count); 43 foreach (LevelPortalRef _protalRef in levelPortalRefTable.Values) 44 { 45 LevelPortalRef levelPortalRef = new LevelPortalRef(); 46 levelPortalRef.levelID = _protalRef.levelID; 47 levelPortalRef.levelName = _protalRef.levelName; 48 levelPortalRef.dungeonID = _protalRef.dungeonID; 49 foreach (PortalInstance _portal in _protalRef.PortalInstanceList) 50 { 51 PortalInstance portalData = new PortalInstance(); 52 portalData.portalID = _portal.portalID; Debug.Log(" PortalID: " + portalData.portalID); 53 portalData.x = _portal.x; 54 portalData.y = _portal.y; 55 portalData.z = _portal.z; 56 portalData.soloDestinationPortalID = _portal.soloDestinationPortalID; 57 portalData.soloDestinationLevelID = _portal.soloDestinationLevelID; 58 portalData.soloDestinationLevelName = _portal.soloDestinationLevelName; 59 levelPortalRef.PortalInstanceList.Add(portalData); 60 } 61 _newTable.levelPortalRefList.Add(levelPortalRef); 62 } 63 64 } 65 66 static void ReadMapXML(string _filePath) 67 { 68 try 69 { 70 XmlDocument _doc = new XmlDocument(); 71 _doc.Load(_filePath); 72 XmlElement _root = _doc.DocumentElement; 73 if (_root.Name.ToUpper().Equals("LEVEL") || _root.Name.ToUpper().Equals("OPENLEVEL")) 74 { 75 LevelPortalRef _levelPortalRef = new LevelPortalRef();//每个关卡 76 XmlNodeList elemList = _root.ChildNodes; 77 foreach (XmlNode xmlNode in elemList) 78 { 79 if (xmlNode != null) 80 { 81 #region every Level 82 83 string _tagName = xmlNode.Name.ToUpper(); 84 switch (_tagName) 85 { 86 case "ID": 87 _levelPortalRef.levelID = Convert.ToInt32(xmlNode.InnerXml); 88 break; 89 case "Name": 90 _levelPortalRef.levelName = xmlNode.InnerXml; 91 break; 92 case "DEMGUON": 93 _levelPortalRef.dungeonID = Convert.ToInt32(xmlNode.InnerXml); 94 break; 95 case "PORTALS": 96 XmlNodeList _portalList = xmlNode.ChildNodes; 97 foreach (XmlNode _portalNode in _portalList) 98 { 99 PortalInstance _portalInstance = new PortalInstance(); 100 if (_portalNode.Name.ToUpper().Equals("PORTAL") && _portalNode != null) 101 { 102 #region every portal 103 XmlNodeList _nodesList = _portalNode.ChildNodes; 104 foreach (XmlNode _portalElement in _nodesList) 105 { 106 string _elementName = _portalElement.Name.ToUpper(); 107 if (!string.IsNullOrEmpty(_portalElement.InnerXml)) 108 { 109 switch (_elementName) 110 { 111 case "ID": 112 //Debug.Log(_elementName + " " + _portalElement.InnerXml); 113 _portalInstance.portalID = Convert.ToInt32(_portalElement.InnerXml); 114 break; 115 case "X": 116 _portalInstance.x = (int)Convert.ToSingle(_portalElement.InnerXml); 117 break; 118 case "Y": 119 _portalInstance.y = Convert.ToSingle(_portalElement.InnerXml); 120 break; 121 case "Z": 122 _portalInstance.z = (int)Convert.ToSingle(_portalElement.InnerXml); 123 break; 124 /////////////////////////因为要兼容旧的格式///////// 125 case "DESTINATIONPORTALID": 126 _portalInstance.soloDestinationPortalID = Convert.ToInt32(_portalElement.InnerXml); 127 break; 128 case "DESTINATIONLEVELNAME": 129 _portalInstance.soloDestinationLevelName = _portalElement.InnerXml; 130 break; 131 case "DESTINATIONLEVELID": 132 _portalInstance.soloDestinationLevelID = Convert.ToInt32(_portalElement.InnerXml); 133 break; 134 /////////////////////////////////////////////////// 135 /////////////////////////新的格式////////////////// 136 case "SoloDestination": 137 foreach (XmlNode solodes in _portalElement) 138 { 139 string _soldesName = solodes.Name.ToUpper(); 140 switch (_soldesName) 141 { 142 case "SOLODESTINATIONPORTALID": 143 _portalInstance.soloDestinationPortalID = Convert.ToInt32(solodes.InnerXml); 144 break; 145 case "SOLODESTINATIONLEVELNAME": 146 _portalInstance.soloDestinationLevelName = solodes.InnerXml; 147 break; 148 case "SOLODESTINATIONLEVELID": 149 _portalInstance.soloDestinationLevelID = Convert.ToInt32(solodes.InnerXml); 150 break; 151 } 152 } 153 break; 154 /////////////////////////////////////////////////// 155 default: 156 break; 157 } 158 } 159 160 } 161 #endregion 162 } 163 _levelPortalRef.PortalInstanceList.Add(_portalInstance); 164 } 165 break; 166 default: 167 break; 168 } 169 170 #endregion 171 } 172 } 173 levelPortalRefTable[_levelPortalRef.levelID] = _levelPortalRef; 174 } 175 } 176 catch (System.Exception ex) 177 { 178 Debug.LogError(ex.ToString()); 179 } 180 181 } 182 183 184 185 private static void WriteToXML(string _guid) 186 { 187 188 } 189 }
然后把创建出来的,已经带有数据的*.assets文件直接打包成*.unity3d文件。
客户端可以直接读取*.unity3d文件,获取到数据