public void GetComponentsFrom(GameObject gameObject) { foreach (var component in gameObject.GetComponents <Widget>()) { if (component is MaterialWidget) { ((MaterialWidget)component).UpdateWidgetState(); } else if (component is LightWidget) { ((LightWidget)component).UpdateWidget(); } else if (component is ParticleWidget) { ((ParticleWidget)component).UpdateWidget(); } if (string.IsNullOrEmpty(component.InstanceID)) { component.GenerateInstanceID(); } var widget = new NodeWidget(component); if (widget.NeedsReferenceSerialization()) { NodeReferenceMap.AddNodeWidget(widget); } widget.Serialize(); NodeReferenceMap.AddReference(component, widget); widgetMap.Add(widget); } }
public void Serialize() { var Referent = NodeReferenceMap.GetObject(this); if (Referent) { Type = Referent.GetType().ToString(); Value = JsonUtility.ToJson(Referent); } }
public void SetWidgetsFor(GameObject gameObject, bool overwriteWidgetSettings = true) { var usedWidgets = new List <Component>(); foreach (var widget in widgetMap) { var widgetType = System.Type.GetType(widget.Type); bool existingWidget = false; if (widgetType == null) { Debug.LogWarning(this.name + " [Widget Type Unknown] " + widget.Type); continue; } var reconstitutedWidget = gameObject.GetComponents(widgetType).FirstOrDefault(w => !usedWidgets.Contains(w)); if (reconstitutedWidget) { existingWidget = true; } else { reconstitutedWidget = gameObject.AddComponent(widgetType); } usedWidgets.Add(reconstitutedWidget); NodeReferenceMap.AddReference(reconstitutedWidget, widget); if (!overwriteWidgetSettings && existingWidget) { continue; } if (widget.NeedsReferenceSerialization()) { NodeReferenceMap.AddNodeWidget(widget); } if (reconstitutedWidget) { JsonUtility.FromJsonOverwrite(widget.Value, reconstitutedWidget); } if (reconstitutedWidget is Widget && string.IsNullOrEmpty(((Widget)reconstitutedWidget).InstanceID)) { ((Widget)reconstitutedWidget).GenerateInstanceID(); } } }
public Node CreateChild(GameObject gameObject) { var child = CreateChild(gameObject.name); child.id = gameObject.GetInstanceID(); NodeReferenceMap.AddReference(gameObject, child); child.localPosition = gameObject.transform.localPosition; child.localRotation = gameObject.transform.localRotation; child.localScale = gameObject.transform.localScale; child.GetComponentsFrom(gameObject); return(child); }
public bool SerializeObjectReferences() { bool referencesComplete = false; var Referent = NodeReferenceMap.GetObject(this); if (Referent) { var widgetType = Referent.GetType(); foreach (var info in widgetType.GetMembers().Where(m => m.MemberType == System.Reflection.MemberTypes.Field)) { var fieldInfo = info.Module.ResolveField(info.MetadataToken); var field = fieldInfo.GetValue(Referent); if (field is ISerializableReference) { var objects = ((ISerializableReference)field).GetReferencedObjects(); int completeRefs = 0; foreach (var obj in objects) { var objectID = NodeReferenceMap.GetObjectID(obj); if (!string.IsNullOrEmpty(objectID)) { completeRefs++; ((ISerializableReference)field).SetReferenceID(obj, objectID); } } if (objects.Count == completeRefs) { referencesComplete = true; } } } Serialize(); } else { referencesComplete = true; } return(referencesComplete); }
public Node(GameObject gameObject) { Initialize(); instanceID = System.Guid.NewGuid().ToString(); id = gameObject.GetInstanceID(); name = gameObject.name; NodeReferenceMap.AddReference(gameObject, this); localPosition = gameObject.transform.position; localRotation = gameObject.transform.rotation; localScale = gameObject.transform.lossyScale; GetComponentsFrom(gameObject); CreateChildrenFrom(gameObject); //TODO: test for references NodeReferenceMap.UpdateReferenceIDs(); }
public static GameObject RenderToGameObject(Node node, GameObject target = null) { if (!target) { target = new GameObject(node == null ? "Root" : node.name); } else { target.name = node.name; } target.transform.position = node.localPosition; target.transform.rotation = node.localRotation; target.transform.localScale = node.localScale; node.SetWidgetsFor(target); NodeReferenceMap.AddReference(target, node); return(target); }
public bool NeedsReferenceSerialization() { System.Reflection.MemberInfo[] memberInfo = new System.Reflection.MemberInfo[0]; var Referent = NodeReferenceMap.GetObject(this); if (Referent) { memberInfo = Referent.GetType().GetMembers(); } else if (!string.IsNullOrEmpty(Type)) { var widgetType = System.Type.GetType(Type); if (widgetType != null) { memberInfo = widgetType.GetMembers(); } } foreach (var info in memberInfo) { if (info.MemberType == System.Reflection.MemberTypes.Field) { var token = info.MetadataToken; var type = info.Module.ResolveField(token); if (type.FieldType.GetInterfaces().Contains(typeof(ISerializableReference))) { return(true); } //Debug.Log("METHOD: " + info + " " + type.FieldType + " " + hasInterface); } } return(false); }
public static IEnumerator RenderNodeTree(Node node, GameObject gameObject = null) { var nodeQueue = new Queue <Node>(); nodeQueue.Enqueue(node); var gameObjectQueue = new Queue <GameObject>(); if (gameObject) { gameObjectQueue.Enqueue(gameObject); NodeReferenceMap.AddReference(gameObject, node); } else { gameObjectQueue.Enqueue(RenderToGameObject(node, gameObject)); } while (gameObjectQueue.Count > 0) { var currentNode = nodeQueue.Dequeue(); var currentGameObject = gameObjectQueue.Dequeue(); foreach (Node childNode in currentNode.GetChildren()) { nodeQueue.Enqueue(childNode); var childGO = RenderToGameObject(childNode); childGO.transform.SetParent(currentGameObject.transform, false); gameObjectQueue.Enqueue(childGO); } yield return(null); } NodeReferenceMap.UpdateObjectReferences(); }
public NodeWidget(Widget widget) { ID = widget.GetInstanceID(); InstanceID = widget.InstanceID; NodeReferenceMap.AddReference(widget, this); }