Ever wanted a list of Vector3s easily accessible through Unity's inspector window?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
public class PositionListTool : MonoBehaviour | |
{ | |
[SerializeField] | |
List<Vector3> positions = new List<Vector3>(); | |
public List<Vector3> GetPositions() | |
{ | |
return positions; | |
} | |
public Vector3 GetPositionAtIndex(int index) | |
{ | |
return positions[index]; | |
} | |
public void SetPositionAtIndex(int index, Vector3 newVec) | |
{ | |
positions[index] = newVec; | |
} | |
public void AddNewPosition(Vector3 newVec) | |
{ | |
positions.Add(newVec); | |
} | |
public int Count() | |
{ | |
return positions.Count; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
/// <summary> | |
/// Put me in a folder called "Editor" | |
/// </summary> | |
[CustomEditor(typeof(PositionListTool)), CanEditMultipleObjects] | |
public class PositionListToolEditor : Editor | |
{ | |
SerializedProperty positionArray; | |
public static bool hideTransformTool; | |
protected virtual void OnSceneGUI() | |
{ | |
PositionListTool myScript = (PositionListTool)target; | |
if (myScript.GetPositions().Count > 1) Handles.DrawPolyLine(myScript.GetPositions().ToArray()); | |
for (int i = 0; i < myScript.GetPositions().Count; i++) | |
{ | |
Undo.RecordObject(myScript, "Changed position"); | |
myScript.SetPositionAtIndex(i, Handles.PositionHandle(myScript.GetPositionAtIndex(i), Quaternion.identity)); | |
} | |
} | |
private void OnEnable() | |
{ | |
positionArray = serializedObject.FindProperty("positions"); | |
Tools.hidden = hideTransformTool; | |
} | |
private void OnDisable() | |
{ | |
Tools.hidden = false; | |
} | |
public override void OnInspectorGUI() | |
{ | |
serializedObject.Update(); | |
PositionListTool myScript = (PositionListTool)target; | |
bool hideTool = EditorGUILayout.Toggle("Hide Transform Tool", hideTransformTool); | |
if (hideTool != hideTransformTool) | |
{ | |
hideTransformTool = hideTool; | |
Tools.hidden = hideTransformTool; | |
if (SceneView.sceneViews.Count > 0) SceneView.lastActiveSceneView.Repaint(); | |
} | |
DrawPropertiesExcluding(serializedObject, new string[] { "m_Script" }); | |
EditorGUILayout.BeginHorizontal(); | |
if (GUILayout.Button("Add Position At Local Origin")) | |
{ | |
int index = positionArray.arraySize; | |
positionArray.InsertArrayElementAtIndex(index); | |
SerializedProperty pos = positionArray.GetArrayElementAtIndex(index); | |
pos.vector3Value = myScript.transform.position; | |
} | |
if (GUILayout.Button("Add Position at World Center")) | |
{ | |
int index = positionArray.arraySize; | |
positionArray.InsertArrayElementAtIndex(index); | |
SerializedProperty pos = positionArray.GetArrayElementAtIndex(index); | |
pos.vector3Value = Vector3.zero; | |
} | |
EditorGUILayout.EndHorizontal(); | |
if (serializedObject.hasModifiedProperties) | |
{ | |
serializedObject.ApplyModifiedProperties(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment