Last active
April 25, 2023 07:45
-
-
Save ProGM/226204b2a7f99998d84d755ffa1fb39a to your computer and use it in GitHub Desktop.
A Unity Editor extension to generate draggable points for vector3 serialized attributes.
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; | |
using UnityEngine; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
public class DraggablePoint : PropertyAttribute {} | |
#if UNITY_EDITOR | |
[CustomEditor(typeof(MonoBehaviour), true)] | |
public class DraggablePointDrawer : Editor { | |
readonly GUIStyle style = new GUIStyle(); | |
void OnEnable(){ | |
style.fontStyle = FontStyle.Bold; | |
style.normal.textColor = Color.white; | |
} | |
public void OnSceneGUI () { | |
var property = serializedObject.GetIterator (); | |
while (property.Next (true)) { | |
if (property.propertyType == SerializedPropertyType.Vector3) { | |
var field = serializedObject.targetObject.GetType ().GetField (property.name); | |
if (field == null) { | |
continue; | |
} | |
var draggablePoints = field.GetCustomAttributes (typeof(DraggablePoint), false); | |
if (draggablePoints.Length > 0) { | |
Handles.Label(property.vector3Value, property.name); | |
property.vector3Value = Handles.PositionHandle (property.vector3Value, Quaternion.identity); | |
serializedObject.ApplyModifiedProperties (); | |
} | |
} | |
} | |
} | |
} | |
#endif |
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 System.Collections; | |
public class ExampleBehavior : MonoBehaviour { | |
[DraggablePoint] public Vector3 SpawnPosition; | |
public GameObject SpawnableObject; | |
public void Spawn() { | |
Instantiate(SpawnableObject, SpawnPosition, Quaternion.identity); | |
} | |
} |
I realise I'm replying a year and a half later, but I'm trying to adapt this to be able to edit Vector3 arrays in scriptable objects. I'm using SOs to generate spawn points for a mobile game and being able to edit them quickly in the scene view would be useful, rather than doing so in the inspector.
I'm assuming I need to use the OnOpenAsset to access assets as opposed to game objects but I keep hitting brick walls. Anybody managed to do this?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
public class DraggablePoint : PropertyAttribute {}
should not be defined in a script inside theEditor
folder but somewhere inside aScript
orAssets
folder. Otherwise you'll be forced to place all the script using this inside theEditor
folder