-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding custom drawer for networkprefab field
will draw create new and field referecne in 1 line if it doesn't exist
- Loading branch information
1 parent
77c8a48
commit bb34c04
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
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
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); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.