public static GameObject RenderGameObject(VGameObject vGameObject) { GameObject go; if (vGameObject.prefab == null) { go = new GameObject(vGameObject.type.ToString()); } else { go = GameObject.Instantiate(vGameObject.prefab); go.name = vGameObject.type.ToString(); } RenderComponent(go, vGameObject); // Prefabs create their own children if (vGameObject.prefab == null) { foreach (VGameObject vChild in vGameObject.children) { GameObject child = RenderGameObject(vChild); child.transform.SetParent(go.transform); } } return(go); }
private void Render(string content) { var newApp = ParseXml.Parse(content, tagMap); GameObjectPatch patch = VDiff.Diff(vApp, newApp); root = patch(root); vApp = newApp; }
public static GameObject RenderFields(GameObject go, VGameObject vComponent) { foreach (KeyValuePair <string, object> field in vComponent.fields) { if (field.Key.ToLower() == "material") { Debug.Log("Material"); } SetField(vComponent, go, field); } return(go); }
public static GameObject RenderComponent(GameObject go, VGameObject vGameObject) { Component component = go.GetComponent(vGameObject.type); if (component == null) { component = go.AddComponent(vGameObject.type); } return(RenderFields(go, vGameObject)); }
internal VGameObject Clone() { var fields = this.fields .Select(field => new KeyValuePair <string, object>(field.Key, field.Value)) .ToArray(); var children = (VGameObject[])this.children .Select(child => child.Clone()) .ToArray(); VGameObject clone = new VGameObject(type, fields, children); return(clone); }
private static void CreateNode(VGameObject node, XElement element, TagMap tagMap) { var elements = element.Elements().ToArray(); var children = new VGameObject[elements.Length]; var childIdx = 0; foreach (var xElement in elements) { var child = NodeFactory(xElement, tagMap); children[childIdx] = child; CreateNode(child, xElement, tagMap); childIdx++; } node.children = children; }
public static void SetField(VGameObject vGameObject, GameObject go, KeyValuePair <string, object> field) { FieldInfo fieldInfo = vGameObject.type.GetField(field.Key); Component component = go.GetComponent(vGameObject.type); if (fieldInfo == null) { PropertyInfo myPropInfo = vGameObject.type.GetProperty(field.Key); myPropInfo.SetValue(component, field.Value); } else { fieldInfo.SetValue(component, field.Value); } }
private static GameObjectPatch DiffChildren(VGameObject[] vOldChildren, VGameObject[] vNewChildren) { List <GameObjectPatch> patches = new List <GameObjectPatch>(); for (int i = 0; i < vOldChildren.Length; i++) { VGameObject newChild = null; if (i < vNewChildren.Length) { newChild = vNewChildren[i]; } patches.Add(Diff(vOldChildren[i], newChild)); } List <GameObjectPatch> additionalPatches = new List <GameObjectPatch>(); if (vNewChildren.Length > vOldChildren.Length) { for (int i = vOldChildren.Length; i < vNewChildren.Length; i++) { VGameObject newChild = vNewChildren[i]; GameObjectPatch newChildPatch = (GameObject go) => { GameObject newGo = VRender.RenderGameObject(newChild); newGo.transform.SetParent(go.transform); return(newGo); }; additionalPatches.Add(newChildPatch); } } GameObjectPatch completePatch = (GameObject go) => { for (int i = 0; i < patches.Count; i++) { GameObject child = go.transform.GetChild(i).gameObject; patches[i](child); } foreach (GameObjectPatch patch in additionalPatches) { patch(go); } return(go); }; return(completePatch); }
public static GameObjectPatch Diff(VGameObject vOldGO, VGameObject vNewGO) { GameObjectPatch patch; // GameObject has been deleted if (vNewGO == null) { patch = (GameObject go) => { GameObject.Destroy(go); return(null); }; } // GameObject has been added else if (vOldGO == null) { patch = (GameObject go) => { GameObject newGo = VRender.RenderGameObject(vNewGO); newGo.transform.SetParent(go.transform); return(newGo); }; } // GameObject has been modified else { patch = (GameObject go) => { if (vOldGO.type != vNewGO.type) { var oldComponent = go.GetComponent(vOldGO.type); GameObject.DestroyImmediate(oldComponent); go.AddComponent(vNewGO.type); } var componentPatch = DiffFields(vOldGO, vNewGO); componentPatch(go); GameObjectPatch childPatch = DiffChildren(vOldGO.children, vNewGO.children); childPatch(go); return(go); }; } return(patch); }
private static FieldsPatch DiffFields(VGameObject vOldGameObject, VGameObject vNewGameObject) { FieldsPatch patch = (GameObject go) => { for (var i = 0; i < vOldGameObject.fields.Length; i++) { KeyValuePair <string, object> field = vNewGameObject.fields[i]; object currentValue = vOldGameObject.fields[i].Value; bool unchanged; if (currentValue == null) { unchanged = currentValue == field.Value; } else { unchanged = currentValue.Equals(field.Value); } if (!unchanged) { VRender.SetField(vNewGameObject, go, field); } } if (vNewGameObject.fields.Length > vOldGameObject.fields.Length) { var newFields = vNewGameObject.fields.Length - vOldGameObject.fields.Length - 1; for (var i = newFields; i < vNewGameObject.fields.Length; i++) { bool unchanged; KeyValuePair <string, object> field = vNewGameObject.fields[i]; VRender.SetField(vNewGameObject, go, field); } } }; return(patch); }
public void Replace(VGameObject current, VGameObject replace) { int idx = Array.IndexOf(children, current); children[idx] = replace; }
public static Task <GameObjectPatch> DiffThreaded(VGameObject vOldGO, VGameObject vNewGO) { return(Task.Run(() => Diff(vOldGO, vNewGO))); }