1616using Newtonsoft . Json . UnityConverters . Configuration ;
1717using UnityEditor . VersionControl ;
1818using UnityEngine ;
19- using UnityEngine . Rendering ;
2019
2120[ CustomEditor ( typeof ( CorePartData ) ) ]
2221public class PartEditor : Editor
2322{
2423
2524 private static bool _initialized = false ;
2625 private static readonly Color ComColor = new Color ( Color . yellow . r , Color . yellow . g , Color . yellow . b , 0.5f ) ;
26+
27+ private static string _jsonPath = "%NAME%.json" ;
28+
29+ private static bool _centerOfMassGizmos = true ;
30+ private static bool _centerOfLiftGizmos = true ;
31+ private static bool _attachNodeGizmos = true ;
32+
33+ public static bool DragCubeGizmos = true ;
2734
2835 // Just initialize all the conversion stuff
2936 private static void Initialize ( )
@@ -37,18 +44,31 @@ private static void Initialize()
3744 private void OnSceneGUI ( )
3845 {
3946 }
47+ private GameObject TargetObject => TargetData . gameObject ;
48+ private CorePartData TargetData => target as CorePartData ;
49+ private PartCore TargetCore => TargetData . Core ;
4050
4151 public override void OnInspectorGUI ( )
4252 {
4353 base . OnInspectorGUI ( ) ;
54+ GUILayout . Label ( "Gizmo Settings" , EditorStyles . boldLabel ) ;
55+ EditorGUI . BeginChangeCheck ( ) ;
56+ _centerOfMassGizmos = EditorGUILayout . Toggle ( "CoM gizmos" , _centerOfMassGizmos ) ;
57+ _centerOfLiftGizmos = EditorGUILayout . Toggle ( "CoL gizmos" , _centerOfLiftGizmos ) ;
58+ _attachNodeGizmos = EditorGUILayout . Toggle ( "Attach Node Gizmos" , _attachNodeGizmos ) ;
59+ DragCubeGizmos = EditorGUILayout . Toggle ( "Drag Cube Gizmos" , DragCubeGizmos ) ;
60+ if ( EditorGUI . EndChangeCheck ( ) )
61+ {
62+ EditorUtility . SetDirty ( target ) ;
63+ }
64+ GUILayout . Label ( "Part Saving" , EditorStyles . boldLabel ) ;
65+ _jsonPath = EditorGUILayout . TextField ( "JSON Path" , _jsonPath ) ;
4466 if ( ! GUILayout . Button ( "Save Part JSON" ) ) return ;
4567 if ( ! _initialized ) Initialize ( ) ;
46- var core = ( serializedObject . targetObject as CorePartData ) ? . Core ! ;
47- var targetGO = ( serializedObject . targetObject as CorePartData ) . gameObject ;
48- if ( core == null ) return ;
68+ if ( TargetCore == null ) return ;
4969 // Clear out the serialized part modules and reserialize them
50- core . data . serializedPartModules . Clear ( ) ;
51- foreach ( var child in targetGO . GetComponents < Component > ( ) )
70+ TargetCore . data . serializedPartModules . Clear ( ) ;
71+ foreach ( var child in TargetObject . GetComponents < Component > ( ) )
5272 {
5373 if ( ! ( child is PartBehaviourModule partBehaviourModule ) ) continue ;
5474 var addMethod = child . GetType ( ) . GetMethod ( "AddDataModules" , BindingFlags . Instance | BindingFlags . NonPublic ) ??
@@ -59,22 +79,42 @@ public override void OnInspectorGUI()
5979 var rebuildMethod = data . GetType ( ) . GetMethod ( "RebuildDataContext" , BindingFlags . Instance | BindingFlags . NonPublic ) ?? data . GetType ( ) . GetMethod ( "RebuildDataContext" , BindingFlags . Instance | BindingFlags . Public ) ;
6080 rebuildMethod ? . Invoke ( data , new object [ ] { } ) ;
6181 }
62- core . data . serializedPartModules . Add ( new SerializedPartModule ( partBehaviourModule , false ) ) ;
82+ TargetCore . data . serializedPartModules . Add ( new SerializedPartModule ( partBehaviourModule , false ) ) ;
6383 }
64- var json = IOProvider . ToJson ( core ) ;
65- File . WriteAllText ( $ "{ Application . dataPath } /{ core . data . partName } .json", json ) ;
84+ var json = IOProvider . ToJson ( TargetCore ) ;
85+ var path = $ "{ Application . dataPath } /{ _jsonPath } ";
86+ path = path . Replace ( "%NAME%" , TargetCore . data . partName ) ;
87+ File . WriteAllText ( $ "{ path } ", json ) ;
6688 AssetDatabase . Refresh ( ) ;
67- EditorUtility . DisplayDialog ( "Part Exported" , $ "Json is at: { Application . dataPath } / { core . data . partName } .json ", "ok" ) ;
89+ EditorUtility . DisplayDialog ( "Part Exported" , $ "Json is at: { path } ", "ok" ) ;
6890 }
91+
6992 [ DrawGizmo ( GizmoType . Active | GizmoType . Selected ) ]
70- static void DrawGizmoForPartCoreData ( CorePartData data , GizmoType gizmoType )
93+ public static void DrawGizmoForPartCoreData ( CorePartData data , GizmoType gizmoType )
7194 {
72- var centerOfMassPosition = data . Data . coMassOffset ;
7395 var localToWorldMatrix = data . transform . localToWorldMatrix ;
74- centerOfMassPosition = localToWorldMatrix . MultiplyPoint ( centerOfMassPosition ) ;
75- Gizmos . DrawIcon ( centerOfMassPosition , "com_icon.png" , false ) ;
76- var centerOfLiftPosition = data . Data . coLiftOffset ;
77- centerOfLiftPosition = localToWorldMatrix . MultiplyPoint ( centerOfLiftPosition ) ;
78- Gizmos . DrawIcon ( centerOfLiftPosition , "col_icon.png" , false ) ;
96+ if ( _centerOfMassGizmos )
97+ {
98+ var centerOfMassPosition = data . Data . coMassOffset ;
99+ centerOfMassPosition = localToWorldMatrix . MultiplyPoint ( centerOfMassPosition ) ;
100+ Gizmos . DrawIcon ( centerOfMassPosition , "com_icon.png" , false ) ;
101+ }
102+ if ( _centerOfLiftGizmos )
103+ {
104+ var centerOfLiftPosition = data . Data . coLiftOffset ;
105+ centerOfLiftPosition = localToWorldMatrix . MultiplyPoint ( centerOfLiftPosition ) ;
106+ Gizmos . DrawIcon ( centerOfLiftPosition , "col_icon.png" , false ) ;
107+ }
108+ if ( ! _attachNodeGizmos ) return ;
109+ Gizmos . color = new Color ( Color . green . r , Color . green . g , Color . green . b , 0.5f ) ;
110+ foreach ( var attachNode in data . Data . attachNodes )
111+ {
112+ var pos = attachNode . position ;
113+ pos = localToWorldMatrix . MultiplyPoint ( pos ) ;
114+ var dir = attachNode . orientation ;
115+ dir = localToWorldMatrix . MultiplyVector ( dir ) ;
116+ Gizmos . DrawRay ( pos , dir * 0.25f ) ;
117+ Gizmos . DrawSphere ( pos , 0.05f ) ;
118+ }
79119 }
80120}
0 commit comments