public static YamlUnityObject Create(string type) { YamlUnityObject obj = null; switch (type) { case "GameObject": obj = new YamlGameObject(); break; case "Transform": obj = new YamlTransform(); break; case "ParticleSystem": obj = new YamlParticleSystem(); break; //--其他的不管了 } if (obj != null) { obj.TypeName = type; } return(obj); }
public override void YamlInit(YamlDataContext context) { base.YamlInit(context); var p = YamlObjectUtil.GetMappingValueNode <YamlMappingNode>(YamlNode); if (p != null) { ChildrenNode = YamlObjectUtil.SearchMappingChildNode <YamlSequenceNode>(p, "m_Children"); } var fatherNode = YamlObjectUtil.SearchMappingChildNode <YamlMappingNode>(p, "m_Father"); //Debug.LogError(fatherNode); Father = YamlObjectUtil.SearchMappingChildNode <YamlScalarNode>(fatherNode, "fileID"); Children = new List <YamlTransform>(); foreach (YamlMappingNode cn in ChildrenNode) { var c = YamlObjectUtil.SearchMappingChildNode <YamlScalarNode>(cn, "fileID"); YamlUnityObject yo = null; if (context.ObjectIdMap.TryGetValue(c.Value, out yo)) { //Debug.LogWarning("ADD CHILD :" + yo.ObjectName + " => " +ObjectName); Children.Add(yo as YamlTransform); } } }
public override void YamlInit(YamlDataContext context) { base.YamlInit(context); var p = YamlObjectUtil.GetMappingValueNode(YamlNode); if (p == null) { return; } NameNode = YamlObjectUtil.SearchMappingChildNode <YamlScalarNode>(p, "m_Name"); ComponmentNode = YamlObjectUtil.SearchMappingChildNode <YamlSequenceNode>(p, "m_Component"); Componments = new List <YamlComponment>(); foreach (var c in ComponmentNode.Children) { YamlMappingNode child = c as YamlMappingNode; if (child == null) { continue; } YamlKeyLocalRef r = new YamlKeyLocalRef(child); YamlUnityObject yo = null; if (context.ObjectIdMap.TryGetValue(r.RefId, out yo)) { Componments.Add(yo as YamlComponment); } } Transofrm = GetComponment <YamlTransform>(); }
private void Parse(YamlStream yaml, List <YamlObjectInfo> infoList, List <YamlMappingNode> nodeList) { string rootGameObjectId = ""; DataContext = new YamlDataContext(); //--解析 YAML 文件-- 缓存所有对象-- Yaml = yaml; InfoList = infoList; NodeList = nodeList; int length = Mathf.Min(infoList.Count, nodeList.Count); for (int i = 0; i < length; i++) { string typeStr; try { typeStr = nodeList[i].Children.First().Key.ToString(); } catch (Exception) { typeStr = "error parse type -- list node"; foreach (var pair in nodeList[i].Children) { Debug.LogWarning(pair.Key + " : " + pair.Value); } } YamlUnityObject yo = YamlObjectFactory.Create(typeStr); if (yo != null) { yo.SetLocalInfo(infoList[i].TypeId, infoList[i].LocalId); yo.SetYamlNode(nodeList[i]); ObjectIdMap.Add(yo.LocalId, yo); } if (typeStr == "Prefab") { //是prefab!记录一下ID var p = YamlObjectUtil.GetMappingValueNode(nodeList[i]); YamlMappingNode r = YamlObjectUtil.SearchMappingChildNode(p, "m_RootGameObject") as YamlMappingNode; if (r != null) { rootGameObjectId = YamlObjectUtil.SearchMappingChildNode(r, "fileID").ToString(); } //Debug.LogWarning("root gameobject = " + rootGameObjectId); } } //---重新关联组件对象--- foreach (var pair in ObjectIdMap) { //Debug.Log(string.Format("id:{0}, object:{1}", pair.Key, pair.Value.TypeName)); var type = pair.Value.GetType(); var obj = pair.Value; if (type != typeof(YamlGameObject)) { string attachId = obj.GetAttachId(); //找到attachId try { obj.SetAttachGameObject(ObjectIdMap[attachId] as YamlGameObject); } catch (Exception e) { Debug.LogError(e); } } else { obj.SetAttachGameObject(obj as YamlGameObject); } } DataContext.ObjectIdMap = ObjectIdMap; //--关联结束后,初始化GOMEOBJECT-- foreach (var pair in ObjectIdMap) { var go = pair.Value as YamlGameObject; if (go != null) { go.GameObjectInit(DataContext); } } //--设置prefab的根gameobject if (!string.IsNullOrEmpty(rootGameObjectId)) { RootGameObject = ObjectIdMap[rootGameObjectId] as YamlGameObject; } RootGameObject.Transofrm.RootTransofrmInit(DataContext); }