allow editing of navmesh build settings

This commit is contained in:
in0finite 2022-03-06 03:15:40 +01:00
parent 3e001ad856
commit 4de69f976d
4 changed files with 67 additions and 31 deletions

View file

@ -28,24 +28,36 @@ namespace SanAndreasUnity.Editor
return types;
}
public static void DrawFieldsAndPropertiesInInspector(object objectToDraw, int inheritanceLevel)
public static object DrawFieldsAndPropertiesInInspector(
object objectToDraw,
int inheritanceLevel,
bool isEditable = false)
{
DrawFieldsInInspector(objectToDraw, inheritanceLevel);
DrawPropertiesInInspector(objectToDraw, inheritanceLevel);
objectToDraw = DrawFieldsInInspector(objectToDraw, inheritanceLevel, isEditable);
objectToDraw = DrawPropertiesInInspector(objectToDraw, inheritanceLevel, isEditable);
return objectToDraw;
}
public static void DrawFieldsInInspector(object objectToDraw, int inheritanceLevel)
public static object DrawFieldsInInspector(object objectToDraw, int inheritanceLevel, bool isEditable)
{
var fieldInfos = GetWithBaseTypes(objectToDraw.GetType(), inheritanceLevel)
.SelectMany(t => t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly));
foreach (var fieldInfo in fieldInfos)
{
DrawObjectInInspector(fieldInfo.FieldType, fieldInfo.GetValue(objectToDraw), fieldInfo.Name);
object newValue = DrawObjectInInspector(
fieldInfo.FieldType,
fieldInfo.GetValue(objectToDraw),
fieldInfo.Name);
if (isEditable && !fieldInfo.IsInitOnly)
fieldInfo.SetValue(objectToDraw, newValue);
}
return objectToDraw;
}
public static void DrawPropertiesInInspector(object objectToDraw, int inheritanceLevel)
public static object DrawPropertiesInInspector(object objectToDraw, int inheritanceLevel, bool isEditable)
{
var properties = GetWithBaseTypes(objectToDraw.GetType(), inheritanceLevel)
.SelectMany(t => t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly))
@ -53,11 +65,19 @@ namespace SanAndreasUnity.Editor
foreach (var propertyInfo in properties)
{
DrawObjectInInspector(propertyInfo.PropertyType, propertyInfo.GetValue(objectToDraw), propertyInfo.Name);
object newValue = DrawObjectInInspector(
propertyInfo.PropertyType,
propertyInfo.GetValue(objectToDraw),
propertyInfo.Name);
if (isEditable && propertyInfo.CanWrite)
propertyInfo.SetValue(objectToDraw, newValue);
}
return objectToDraw;
}
public static void DrawObjectInInspector(
public static object DrawObjectInInspector(
Type type,
object value,
string name)
@ -66,26 +86,47 @@ namespace SanAndreasUnity.Editor
if (typeof(Component).IsAssignableFrom(type))
{
EditorGUILayout.ObjectField(labelText, value as Component, type, true);
return EditorGUILayout.ObjectField(labelText, (Component)value, type, true);
}
else if (type.IsEnum)
{
if (type.GetCustomAttribute<System.FlagsAttribute>() != null)
EditorGUILayout.EnumFlagsField(labelText, value as System.Enum);
return EditorGUILayout.EnumFlagsField(labelText, (System.Enum)value);
else
EditorGUILayout.EnumPopup(labelText, value as System.Enum);
return EditorGUILayout.EnumPopup(labelText, (System.Enum)value);
}
else if (type == typeof(Color))
{
EditorGUILayout.ColorField(labelText, (Color) value);
return EditorGUILayout.ColorField(labelText, (Color) value);
}
else if (type == typeof(Color32))
{
EditorGUILayout.ColorField(labelText, (Color32) value);
return EditorGUILayout.ColorField(labelText, (Color32) value);
}
else if (type == typeof(string))
{
return EditorGUILayout.TextField(labelText, (string)value);
}
else if (type == typeof(int))
{
return EditorGUILayout.IntField(labelText, (int)value);
}
else if (type == typeof(uint))
{
return (uint)EditorGUILayout.IntField(labelText, (int)(uint)value);
}
else if (type == typeof(float))
{
return EditorGUILayout.FloatField(labelText, (float)value);
}
else if (type == typeof(bool))
{
return EditorGUILayout.Toggle(labelText, (bool)value);
}
else
{
EditorGUILayout.LabelField($"{labelText} {value}");
return value;
}
}

View file

@ -18,7 +18,7 @@ namespace SanAndreasUnity.Editor
var lightSource = (LightSource) this.target;
EditorUtils.DrawFieldsInInspector(lightSource.LightInfo, 0);
EditorUtils.DrawFieldsInInspector(lightSource.LightInfo, 0, false);
}
}

View file

@ -12,7 +12,8 @@ namespace SanAndreasUnity.Editor
{
public class NavMeshGenerator : EditorWindowBase
{
private static int s_selectedAgentId = 0;
private int m_selectedAgentId = 0;
private NavMeshBuildSettings m_navMeshBuildSettings;
[SerializeField] private NavMeshData m_navMeshData;
private NavMeshDataInstance m_navMeshDataInstance = new NavMeshDataInstance();
private static CoroutineInfo s_coroutine;
@ -28,6 +29,7 @@ namespace SanAndreasUnity.Editor
public NavMeshGenerator()
{
this.titleContent = new GUIContent("Nav mesh generator");
m_navMeshBuildSettings = NavMesh.GetSettingsByID(m_selectedAgentId);
}
void Cleanup()
@ -49,20 +51,19 @@ namespace SanAndreasUnity.Editor
GUILayout.BeginHorizontal();
GUILayout.Label("Agent ID:");
s_selectedAgentId = EditorGUILayout.IntField(s_selectedAgentId);
m_selectedAgentId = EditorGUILayout.IntField(m_selectedAgentId);
if (GUILayout.Button("Edit"))
UnityEditor.AI.NavMeshEditorHelpers.OpenAgentSettings(s_selectedAgentId);
UnityEditor.AI.NavMeshEditorHelpers.OpenAgentSettings(m_selectedAgentId);
if (GUILayout.Button("Use"))
m_navMeshBuildSettings = NavMesh.GetSettingsByID(m_selectedAgentId);
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.Space(10);
if (!string.IsNullOrEmpty(NavMesh.GetSettingsNameFromID(s_selectedAgentId)))
{
var navMeshBuildSettings = NavMesh.GetSettingsByID(s_selectedAgentId);
EditorUtils.DrawFieldsAndPropertiesInInspector(navMeshBuildSettings, 0);
}
m_navMeshBuildSettings = (NavMeshBuildSettings)EditorUtils.DrawFieldsAndPropertiesInInspector(
m_navMeshBuildSettings, 0, true);
GUILayout.Space(20);
EditorGUILayout.PrefixLabel("Current nav mesh:");
@ -177,12 +178,6 @@ namespace SanAndreasUnity.Editor
yield break;
}
if (string.IsNullOrEmpty(NavMesh.GetSettingsNameFromID(s_selectedAgentId)))
{
EditorUtility.DisplayDialog("", "Invalid agent id", "Ok");
yield break;
}
var cell = Cell.Instance;
if (null == cell)
{
@ -190,7 +185,7 @@ namespace SanAndreasUnity.Editor
yield break;
}
NavMeshBuildSettings navMeshBuildSettings = NavMesh.GetSettingsByID(s_selectedAgentId);
NavMeshBuildSettings navMeshBuildSettings = m_navMeshBuildSettings;
EditorUtility.DisplayProgressBar("Generating nav mesh", "Collecting objects...", 0f);

View file

@ -21,7 +21,7 @@ namespace SanAndreasUnity.Editor
_scrollViewPos = EditorGUILayout.BeginScrollView(_scrollViewPos, GUILayout.MinHeight(350));
EditorUtils.DrawPropertiesInInspector(staticGeometry, 1);
EditorUtils.DrawPropertiesInInspector(staticGeometry, 1, false);
GUILayout.Space (10);
GUILayout.Label("Object definition:");