浏览代码

Added editor for ScriptableObject with events. It allows to automatically show buttons for each event and raise them. (#256)

/main
GitHub 4 年前
当前提交
8490d386
共有 6 个文件被更改,包括 84 次插入0 次删除
  1. 38
      UOP1_Project/Assets/Scripts/Editor/ScriptableObjectHelper.cs
  2. 11
      UOP1_Project/Assets/Scripts/Editor/ScriptableObjectHelper.cs.meta
  3. 8
      UOP1_Project/Assets/Scripts/Input/Editor.meta
  4. 16
      UOP1_Project/Assets/Scripts/Input/Editor/InputReaderEditor.cs
  5. 11
      UOP1_Project/Assets/Scripts/Input/Editor/InputReaderEditor.cs.meta

38
UOP1_Project/Assets/Scripts/Editor/ScriptableObjectHelper.cs


using System;
using UnityEngine;
public static class ScriptableObjectHelper
{
public static void GenerateButtonsForEvents<T>(UnityEngine.Object target)
where T : ScriptableObject
{
var targetIr = target as T;
if (targetIr != null)
{
var typeIr = targetIr.GetType();
var events = typeIr.GetEvents();
foreach (var ev in events)
{
if (GUILayout.Button(ev.Name))
{
//Delegates doesn't support direct access to RaiseMethod, must use backing field
// https://stackoverflow.com/questions/14885325/eventinfo-getraisemethod-always-null
// https://social.msdn.microsoft.com/Forums/vstudio/en-US/44b0d573-5c53-47b0-8e85-6056cbae95b0/raising-an-event-via-reflection
var eventDelagate = typeIr.GetField(ev.Name, System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic)
?.GetValue(targetIr) as MulticastDelegate;
try
{
eventDelagate?.DynamicInvoke();
}
catch
{
Debug.LogWarning($"Event '{ev.Name}' requires some arguments which weren't provided. Delegate cannot be invoked directly from UI.");
}
}
}
}
}
}

11
UOP1_Project/Assets/Scripts/Editor/ScriptableObjectHelper.cs.meta


fileFormatVersion: 2
guid: 0a7eba3a614f3d74fb12d27b80cd0597
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
UOP1_Project/Assets/Scripts/Input/Editor.meta


fileFormatVersion: 2
guid: 8aed24cb0c323134e96fb92cecbeafc4
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

16
UOP1_Project/Assets/Scripts/Input/Editor/InputReaderEditor.cs


using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(InputReader))]
public class InputReaderEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
if (!Application.isPlaying)
return;
ScriptableObjectHelper.GenerateButtonsForEvents<InputReader>(target);
}
}

11
UOP1_Project/Assets/Scripts/Input/Editor/InputReaderEditor.cs.meta


fileFormatVersion: 2
guid: 0e46e2d2cecfc354a8ac70cae65b2343
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存