Skip to content

Commit

Permalink
feat: adding custom drawer for networkprefab field
Browse files Browse the repository at this point in the history
will draw create new and field referecne in 1 line if it doesn't exist
  • Loading branch information
James-Frowen committed Feb 2, 2023
1 parent 77c8a48 commit bb34c04
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
71 changes: 71 additions & 0 deletions Assets/Mirage/Editor/NetworkPrefabsPropertyDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Mirage.EditorScripts.Logging;
using UnityEditor;
using UnityEngine;

namespace Mirage
{
[CustomPropertyDrawer(typeof(NetworkPrefabs))]
public class NetworkPrefabsPropertyDrawer : PropertyDrawer
{
private const float REF_WIDTH = 0.3f;
private const float LABEL_WIDTH = 0.4f;
private const float BUTTON_WIDTH = 0.3f;

public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
{
if (property.objectReferenceValue == null)
{
noValueGUI(rect, property, label);
}
else
{
EditorGUI.PropertyField(rect, property, label);
//valueGUI(rect, property, label);
}
}

private void noValueGUI(Rect rect, SerializedProperty property, GUIContent label)
{
var x = rect.x;
var refWidth = rect.width * REF_WIDTH;
var labelWidth = rect.width * LABEL_WIDTH;
var buttonWidth = rect.width * BUTTON_WIDTH;

EditorGUI.LabelField(new Rect(x, rect.y, labelWidth, rect.height), label);
x += labelWidth;

var createNew = GUI.Button(new Rect(x, rect.y, buttonWidth, rect.height), "Create New");
x += buttonWidth;

if (createNew)
{
createNewValue(property);
}

EditorGUI.BeginChangeCheck();
EditorGUI.PropertyField(new Rect(x, rect.y, refWidth, rect.height), property, GUIContent.none, false);
if (EditorGUI.EndChangeCheck())
{
property.serializedObject.ApplyModifiedProperties();
}
}

private void createNewValue(SerializedProperty property)
{
// name of gameobject that this property is on
var gameObjectName = property.serializedObject.targetObject.name;

var value = ScriptableObjectUtility.CreateAsset<NetworkPrefabs>(gameObjectName + "_NetworkPrefabs", "Assets");

property.objectReferenceValue = value;
property.serializedObject.ApplyModifiedProperties();
}

protected virtual void valueGUI(Rect rect, SerializedProperty property, GUIContent label)
{
// todo draw internal list here
//drawCanEditVar(pos, property, label);
//drawPropertyField(refWidth, refRect, property, GUIContent.none);
}
}
}
11 changes: 11 additions & 0 deletions Assets/Mirage/Editor/NetworkPrefabsPropertyDrawer.cs.meta

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

0 comments on commit bb34c04

Please sign in to comment.