#NodeSystem
1 messages · Page 1 of 1 (latest)
using System.Collections.Generic;
using UnityEngine;
namespace Content
{
public class Node
{
public Node(Vector2 position, bool nodeStatus)
{
Position = position;
NodeStatus = nodeStatus;
}
public Vector2 Position { get; set; }
public bool NodeStatus { get; set; }
}
public class NodeSystem : MonoBehaviour
{
public NodeSystem()
{
FreeNodes = new List<Node>();
ConnectedNodes = new List<Node>();
}
public List<Node> FreeNodes { get; set; }
public List<Node> ConnectedNodes { get; set; }
public void Start()
{
}
//MergeNodeSystem
public void MergeNodeSystem(NodeSystem incomingNodeSystem) //todo use raycasting to detect nearby nodes
{
List<Node> duplicates = new List<Node>();
foreach (Node incomingNode in incomingNodeSystem.FreeNodes)
{
foreach (Node ourNode in FreeNodes)
{
if (incomingNode.Position == ourNode.Position)
{
duplicates.Add(incomingNode);
duplicates.Add(ourNode);
}
}
}
foreach (Node duplicate in duplicates)
{
incomingNodeSystem.FreeNodes.Remove(duplicate);
FreeNodes.Remove(duplicate);
incomingNodeSystem.ConnectedNodes.Add(duplicate);
ConnectedNodes.Add(duplicate);
}
}
public void AddNode(Vector2 position)
{
FreeNodes.Add(new Node(position,
true));
}
public void RemoveNode(Node node)
{
FreeNodes.Remove(node);
}
}
}
had to remove some redundant bits due to message limit
ok, so you have nothing marked as [Serialized] there
and contextual comments
do I just add serializefield to the freenodes and connected nodes?
wait, how are you expecting to save this, it's not a monobehaviour or a scriptable object
in another file which way goes over message limit
lemme break it into parts
private void OnSceneGUI()
{
Handles.color = Color.red;
// Loop through the attachPoints array
for (int i = 0; i < _nodeSystem.FreeNodes.Count; i++)
{
Vector2 newPosition =
Handles.PositionHandle(_nodeSystem.transform.TransformPoint(_nodeSystem.FreeNodes[i].Position),
Quaternion.identity);
// Check if the point has moved
if (newPosition != _nodeSystem.FreeNodes[i].Position)
{
Undo.RecordObject(_nodeSystem, "Move Attach Point");
_nodeSystem.FreeNodes[i].Position = _nodeSystem.transform.InverseTransformPoint(newPosition);
}
// Draw a sphere handle at the attach point's position
EditorGUI.BeginChangeCheck();
float handleSize =
HandleUtility.GetHandleSize(
_nodeSystem.transform.TransformPoint(_nodeSystem.FreeNodes[i].Position)) *
0.1f;
Vector2 newPointPosition = Handles.FreeMoveHandle(
_nodeSystem.transform.TransformPoint(_nodeSystem.FreeNodes[i].Position),
handleSize, Vector2.one, Handles.SphereHandleCap);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(_nodeSystem, "Move Sphere Handle");
_nodeSystem.FreeNodes[i].Position = _nodeSystem.transform.InverseTransformPoint(newPointPosition);
}
}
}```
public override void OnInspectorGUI()
{
// Draw the default inspector for the script
DrawDefaultInspector();
//NodeSystem nodeSystem = (NodeSystem)target;
for (int i = 0; i < _nodeSystem.FreeNodes.Count; i++)
{
EditorGUILayout.BeginHorizontal();
_nodeSystem.FreeNodes[i].Position =
EditorGUILayout.Vector2Field("Free Node " + (i + 1), _nodeSystem.FreeNodes[i].Position);
if (GUILayout.Button("Remove Node", GUILayout.Width(100), GUILayout.Height(20)))
{
_nodeSystem.FreeNodes.RemoveAt(i);
}
EditorGUILayout.EndHorizontal();
}
if (GUILayout.Button("Generate Attach Points"))
{
GenerateAttachPoints(_nodeSystem);
}
// Add a button to add a new point to the array
if (GUILayout.Button("Add Node"))
{
Undo.RecordObject(_nodeSystem, "Add Attach Point");
_nodeSystem.AddNode(Vector2.zero); //create new empty node
}
}```
I still see nothing there which will save anything
private void GenerateAttachPoints(NodeSystem nodeSystem)
{
SpriteRenderer spriteRenderer = nodeSystem.GetComponentInChildren<SpriteRenderer>();
if (spriteRenderer == null)
{
Debug.LogError("No SpriteRenderer found on NodeSystem's game object or any of its children.");
return;
}
float halfHeight = 0.5f, halfWidth = 0.5f; //TEMPORARY
nodeSystem.AddNode(new Vector2(-halfWidth, 0)); // Left side
nodeSystem.AddNode(new Vector2(halfWidth, 0)); // Right side
nodeSystem.AddNode(new Vector2(0, halfHeight)); // Top
nodeSystem.AddNode(new Vector2(0, -halfHeight)); // Bottom
}
nodeSystem.AddNode should work shouldn't it? as theres an instance of the Nodesystem attached to the object we are editing
Unity seems to know this
as if I click off the prefab and back on all my changes persist
you cannot just have POCO's and save them into thin air, you need to code a save system for them
what is POCO?
Plain old C# object
ok
ok, so when do I use that and why not when I serialize other fields? is it since unity does that for me when I use [serializefield]?
because you are using [serializefield] inside a Monobehavior or a ScriptableObject. Ask yourself, where do I expect Unity to save this stuff?
My answer given all my prior experience would be that it should be attached to the prefab, in the meta.cs file just as if I had edited a transform component on that object
I'm no professional of course which is why I'm looking for help understanding
but how can you attach a POCO to a Prefab, you can't use Add Component because it is not a Component
from the tutorials I had watched I thought a component was a behavoir that was charaterized by being able to be attached to many different objects so to be modular
true but your classes are non of those things
oh?
public class Node
where is the inheritance from a Component or Behaviour?
wait my bad
public class NodeSystem : MonoBehaviour
{
public NodeSystem()
{
FreeNodes = new List<Node>();
ConnectedNodes = new List<Node>();
}
public List<Node> FreeNodes { get; set; }
public List<Node> ConnectedNodes { get; set; }
here it is, I was looking at the Node class.
in that case it's simple
[System.Serializable]
public class Node
job done
yep, NodeSystem is serialized because it is a Monobehaviour.
Node was not because it is a POCO and you did not mark it as Serializable
ok I did that but it still isn't working, let me send a short video to demo what I'm trying to do since I found it super hard to explain
so now in your editor code you just need
SetDirty(_nodeSystem);
AssetDatabase.Save();
Don't bother, I don't do videos
ah ok
when should this be done? after the onscenegui?
when you want to save
OnDisable or OnFocus is favorite
unity isn't recognizing that, do I need to import something?
might it not be a good idea to refer to the documentation?
actually forget OnFocus, just use OnDisable
Also I would highly reccommend using UIToolkit for new editor scripts rather than IMGUI
is that something from package manager?
yes, it's the new Unity Ui system
gotcha
https://docs.unity3d.com/ScriptReference/EditorWindow.OnFocus.html
Tried onfocus and that won't seem to work I got to void OnFocus() but then I had to inherit Editorwindow, which was not allowed since my IDE said it needs to inherit UnityEditor.Editor instead
yep, Editor only has OnDisable and OnDestroy, but using OnDisable to save should be sufficient
EditorUtility.SetDirty(_nodeSystem);
AssetDatabase.SaveAssets();
This right?
looks about right
throws an error whenever window is closed as target is null
doesnt seem to save either
is target not _nodeSystem?
then it cannot be null, or, at least it should not be