Skip to content

Commit

Permalink
💥 feat!: New custom editor, editor components removed
Browse files Browse the repository at this point in the history
  • Loading branch information
esnya committed Jun 10, 2022
1 parent f9b635a commit 8a34577
Show file tree
Hide file tree
Showing 17 changed files with 338 additions and 567 deletions.
148 changes: 148 additions & 0 deletions Packages/com.nekometer.esnya.udon-simple-cars/Editor/USC_CarEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UdonSharp;
using UdonSharpEditor;
using UnityEditor;
using UnityEngine;

namespace UdonSimpleCars
{
[CustomEditor(typeof(USC_Car))]
public class USC_CarEditor : Editor
{
private struct WheelDescriptor
{
public WheelCollider wheelCollider;
public bool ignored;
public bool steered;
public bool driving;
public bool brake;
public bool detached;
public Transform visual;

public static IEnumerable<WheelDescriptor> GetDescriptors(USC_Car car)
{
var wheels = car.wheels.ToList();
return wheels.Concat(car.detachedWheels).Concat(car.GetComponentsInChildren<WheelCollider>(true)).Distinct().Select(wheelCollider =>
{
var index = wheels.IndexOf(wheelCollider);
return new WheelDescriptor()
{
wheelCollider = wheelCollider,
ignored = index < 0,
steered = car.steeredWheels.Contains(wheelCollider),
driving = car.drivingWheels.Contains(wheelCollider),
brake = car.brakeWheels.Contains(wheelCollider),
detached = car.detachedWheels.Contains(wheelCollider),
visual = index < 0 || index >= (car.wheelVisuals?.Length ?? 0) ? null : car.wheelVisuals[index],
};
});
}

public void OnGUI()
{
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.ObjectField(wheelCollider, typeof(WheelCollider), true);
ignored = EditorGUILayout.ToggleLeft("Ignored", ignored, GUILayout.Width(60));
steered = EditorGUILayout.ToggleLeft("Steered", steered, GUILayout.Width(60));
driving = EditorGUILayout.ToggleLeft("Driving", driving, GUILayout.Width(60));
brake = EditorGUILayout.ToggleLeft("Brake", brake, GUILayout.Width(60));
detached = EditorGUILayout.ToggleLeft("Detached", detached, GUILayout.Width(60));
visual = EditorGUILayout.ObjectField(visual, typeof(Transform), true) as Transform;
}
}

public static void Apply(USC_Car car, IEnumerable<WheelDescriptor> descriptors)
{
var filtered = descriptors.Where(d => !d.ignored).ToArray();

car.wheels = filtered.Where(d => !d.detached).Select(d => d.wheelCollider).ToArray();
car.wheelVisuals = filtered.Where(d => !d.detached).Select(d => d.visual).ToArray();

car.steeredWheels = filtered.Where(d => d.steered).Select(d => d.wheelCollider).ToArray();
car.drivingWheels = filtered.Where(d => d.driving).Select(d => d.wheelCollider).ToArray();
car.brakeWheels = filtered.Where(d => d.brake).Select(d => d.wheelCollider).ToArray();
car.detachedWheels = filtered.Where(d => d.detached).Select(d => d.wheelCollider).ToArray();
}
}
private void OnWheelsGUI(SerializedProperty property)
{

serializedObject.ApplyModifiedProperties();

var car = target as USC_Car;

var descriptors = WheelDescriptor.GetDescriptors(car).ToArray();
using (var change = new EditorGUI.ChangeCheckScope())
{
EditorGUILayout.Space();
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.LabelField("Wheels", EditorStyles.boldLabel);
EditorGUILayout.LabelField("", GUILayout.Width(60 * 5));
EditorGUILayout.LabelField("Visuals", EditorStyles.boldLabel);
}
foreach (var descriptor in descriptors)
{
descriptor.OnGUI();
}
if (change.changed) WheelDescriptor.Apply(car, descriptors);
}

serializedObject.Update();
while (property.name.ToLower().Contains("wheel")) property.NextVisible(false);
}

private static readonly Dictionary<string, string> gameObjectNameTable = new Dictionary<string, string> () {
{ nameof(USC_Car.engineSound), "EngineSound" },
{ nameof(USC_Car.steeringWheel), "SteeringWheel" },
{ nameof(USC_Car.operatingOnly), "OperatingOnly" },
{ nameof(USC_Car.inVehicleOnly), "InVehicleOnly" },
{ nameof(USC_Car.driverOnly), "DriverOnly" },
{ nameof(USC_Car.backGearOnly), "BackGearOnlny" },
{ nameof(USC_Car.brakingOnly), "BrakingOnly" },
{ nameof(USC_Car.detachedObjects), "DetachedObjects" },
};

public override void OnInspectorGUI()
{
if (UdonSharpGUI.DrawDefaultUdonSharpBehaviourHeader(target)) return;

var property = serializedObject.GetIterator();
property.NextVisible(true);

do
{
if (property.name == nameof(USC_Car.wheels)) OnWheelsGUI(property);
else
{
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.PropertyField(property, true);

if (gameObjectNameTable.ContainsKey(property.name))
{
var name = gameObjectNameTable[property.name];
if (GUILayout.Button("Find", EditorStyles.miniButton, GUILayout.ExpandWidth(false)))
{
var gameObject = EditorUtility.CollectDeepHierarchy(serializedObject.targetObjects)
.Select(o => o as GameObject)
.FirstOrDefault(o => o && o.name == name);

if (gameObject)
{
var fieldType = typeof(USC_Car).GetField(property.name, BindingFlags.Instance | BindingFlags.DeclaredOnly)?.FieldType;
property.objectReferenceValue = fieldType?.IsSubclassOf(typeof(Component)) == true ? (UnityEngine.Object)gameObject.GetComponent(fieldType) : gameObject;
}
}
}
}
}
}
while (property.NextVisible(false));
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"name": "UdonSimpleCars.Editor",
"references": [
"GUID:da9d4fdad7c29b645bc5814f602035c1",
"GUID:3c1bc1267eab5884ebe7f232c09ee0d9",
"GUID:99835874ee819da44948776e0df4ff1d",
"GUID:80a8b162ac804404bae7b5fdc3041428",
"GUID:84265b35cca3905448e623ef3903f0ff",
"GUID:f253815156df439380cb4780773d184b"
],
"includePlatforms": [
"Editor"
Expand All @@ -11,6 +17,7 @@
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [
"UDON"
],
"versionDefines": [],
"noEngineReferences": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ GameObject:
m_Component:
- component: {fileID: 2973368669135221506}
- component: {fileID: 2973368669135221509}
- component: {fileID: 7759677602001531047}
m_Layer: 0
m_Name: EngineSound
m_TagString: Untagged
Expand Down Expand Up @@ -128,15 +127,3 @@ AudioSource:
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
--- !u!114 &7759677602001531047
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2973368669135221508}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 230f5d06a17889b40bcc22e38ed198a0, type: 3}
m_Name:
m_EditorClassIdentifier:
Original file line number Diff line number Diff line change
Expand Up @@ -1794,7 +1794,6 @@ GameObject:
m_Component:
- component: {fileID: 8311205895693598331}
- component: {fileID: 8311205895693598328}
- component: {fileID: 6175065190278401515}
m_Layer: 17
m_Name: Wheel.RL
m_TagString: Untagged
Expand Down Expand Up @@ -1846,18 +1845,6 @@ WheelCollider:
m_AsymptoteValue: 0.75
m_Stiffness: 1
m_Enabled: 1
--- !u!114 &6175065190278401515
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8311205895693598330}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c0b56fdad804b0d43807dce7693b834a, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &8311205895856690607
GameObject:
m_ObjectHideFlags: 0
Expand All @@ -1868,7 +1855,6 @@ GameObject:
m_Component:
- component: {fileID: 8311205895856690604}
- component: {fileID: 8311205895856690605}
- component: {fileID: 5329487354829655193}
m_Layer: 17
m_Name: Wheel.FL
m_TagString: Untagged
Expand Down Expand Up @@ -1920,18 +1906,6 @@ WheelCollider:
m_AsymptoteValue: 0.75
m_Stiffness: 1
m_Enabled: 1
--- !u!114 &5329487354829655193
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8311205895856690607}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c0b56fdad804b0d43807dce7693b834a, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &8311205895916332077
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -2021,7 +1995,6 @@ GameObject:
m_Component:
- component: {fileID: 8311205896164849315}
- component: {fileID: 8311205896164849312}
- component: {fileID: 6662045182965436981}
m_Layer: 17
m_Name: Wheel.FR
m_TagString: Untagged
Expand Down Expand Up @@ -2073,18 +2046,6 @@ WheelCollider:
m_AsymptoteValue: 0.75
m_Stiffness: 1
m_Enabled: 1
--- !u!114 &6662045182965436981
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8311205896164849314}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c0b56fdad804b0d43807dce7693b834a, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &8311205896242710736
GameObject:
m_ObjectHideFlags: 0
Expand All @@ -2095,7 +2056,6 @@ GameObject:
m_Component:
- component: {fileID: 8311205896242710737}
- component: {fileID: 8311205896242710742}
- component: {fileID: 2577880027193508572}
m_Layer: 17
m_Name: Wheel.RR
m_TagString: Untagged
Expand Down Expand Up @@ -2147,18 +2107,6 @@ WheelCollider:
m_AsymptoteValue: 0.75
m_Stiffness: 1
m_Enabled: 1
--- !u!114 &2577880027193508572
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8311205896242710736}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c0b56fdad804b0d43807dce7693b834a, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &8311205896542626617
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -2668,18 +2616,18 @@ PrefabInstance:
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: bd08a4ba72ae188439fae9e40a4dee8f, type: 3}
--- !u!82 &8311205894919746665 stripped
AudioSource:
m_CorrespondingSourceObject: {fileID: 2973368669135221509, guid: bd08a4ba72ae188439fae9e40a4dee8f,
type: 3}
m_PrefabInstance: {fileID: 6491044646431901548}
m_PrefabAsset: {fileID: 0}
--- !u!4 &8311205894919746670 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 2973368669135221506, guid: bd08a4ba72ae188439fae9e40a4dee8f,
type: 3}
m_PrefabInstance: {fileID: 6491044646431901548}
m_PrefabAsset: {fileID: 0}
--- !u!82 &8311205894919746665 stripped
AudioSource:
m_CorrespondingSourceObject: {fileID: 2973368669135221509, guid: bd08a4ba72ae188439fae9e40a4dee8f,
type: 3}
m_PrefabInstance: {fileID: 6491044646431901548}
m_PrefabAsset: {fileID: 0}
--- !u!1001 &8385989238830587977
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 8a34577

Please sign in to comment.