Create new node position

        public void CreateNode(DialogueNode parent)
        {
            Vector2 rectOffset = new Vector2(200, 0);
            DialogueNode newNode = CreateInstance<DialogueNode>();
            newNode.name = Guid.NewGuid().ToString();
            Undo.RegisterCreatedObjectUndo(newNode, "Created Dialogue Node");
            if (parent != null)
            {
                parent.children.Add(newNode.name);
                newNode.rect.position = parent.rect.position + rectOffset;
            }
            nodes.Add(newNode);
            OnValidate();
        }```

If you want to have new spawned nodes to spawn to the right of the parent you can get the parent position and add an offset like the above.
3 Likes

Good tip!

I’ve made it a bit different, but about the same :sweat_smile:

newNode.rect.position = new Vector2(parent.rect.position.x + parent.rect.width + 50, parent.rect.position.y);

Also I’ve used 50 'cause it’s the same value of the background grid… but I didn’t want to access to the constant on the DialogueEditor.cs

[Edit]
Extended the code to avoid overlapping if children are already present, for example you create a new from Node1 when Node1 already have a child

if(parent.GetChildren().Count > 1)
            {
                newNode.SetPosition(new Vector2(parent.GetRect().position.x + parent.GetRect().width + 75, parent.GetRect().position.y + 75));
                AddNode(newNode);
            }
            else
            {
                newNode.SetPosition(new Vector2(parent.GetRect().position.x + parent.GetRect().width + 50, parent.GetRect().position.y));
                AddNode(newNode);
            }
1 Like

Privacy & Terms