- Home /
Custom editor is not applying the settings.
I made a script that round the points of the polygon collider 2D:
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(PolygonPoints))]
public class PolyColliderPointAling : Editor
{
PolygonPoints polygonPoints;
public override void OnInspectorGUI()
{
serializedObject.Update();
if(GUILayout.Button("Run!"))
{
polygonPoints = target as PolygonPoints;
PolygonCollider2D[] colliders = polygonPoints.gameObject.GetComponents<PolygonCollider2D>();
for(int y = 0; y < colliders.Length; y++)
{
Vector2[] points = colliders[y].points;
Debug.Log(points.Length);
for(int i = 0; i < points.Length; i++)
{
Vector2 pointPosition = new Vector2(Mathf.Round(points[i].x), Mathf.Round(points[i].y));
points[i] = pointPosition;
Debug.Log(points[i].ToString("F5"));
}
}
EditorUtility.SetDirty(polygonPoints);
}
serializedObject.ApplyModifiedProperties();
}
}
The PolygonPoints script:
using UnityEngine;
using System.Collections;
public class PolygonPoints : MonoBehaviour {
PolygonCollider2D[] colliders;
void Start()
{
colliders = gameObject.GetComponents<PolygonCollider2D>();
for(int y = 0; y < colliders.Length; y++)
{
Vector2[] points = colliders[y].points;
Debug.Log(points.Length + "pontos.");
for(int i = 0; i < points.Length; i++)
{
Debug.Log(points[i].ToString("F5"));
}
}
}
}
When I hit the "run" button the console shows that the points have been rounded. But when I hit play I lose all changes and I don't know why. How can I apply the changes that I have made ?
Answer by RudyTheDev · Mar 06, 2015 at 10:50 PM
These two:
Vector2[] points = colliders[y].points;
...
points[i] = pointPosition;
will not modify colliders[y].points
. Arrays are copies and Vector2
s are struct
s. Your Debug.Log(points[i].ToString("F5"));
prints your local array, not the .points
array from the collider.
You have to colliders[y].points = points
in the end.
Wou man, i'm feeling so dumb right now hahaha, thank you!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Inspector button to generate code? 1 Answer
DrawDefaultInspector of another script in custom inspector 1 Answer
How do I expose a UnityEvent in a Custom Inspector? 1 Answer