Created
December 27, 2016 12:43
-
-
Save YclepticStudios/a1250209a81f3123b84959b831110cc5 to your computer and use it in GitHub Desktop.
Unity event callback with inspector button for test firing.
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
// | |
// ============================================================================ | |
// MIT License | |
// | |
// Copyright (c) 2016 Eric Phillips | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a | |
// copy of this software and associated documentation files (the "Software"), | |
// to deal in the Software without restriction, including without limitation | |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
// and/or sell copies of the Software, and to permit persons to whom the | |
// Software is furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
// DEALINGS IN THE SOFTWARE. | |
// ============================================================================ | |
// | |
// | |
// This script creates a button and a UnityEvent in | |
// the inspector. Clicking the button fires the event | |
// which can be used for testing purposes. | |
// | |
// Created by Eric Phillips on December 26, 2016. | |
// | |
using UnityEngine; | |
using UnityEngine.Events; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
public class EventTester : MonoBehaviour | |
{ | |
public UnityEvent TestEvent; | |
} | |
#if UNITY_EDITOR | |
[CustomEditor(typeof(EventTester))] | |
public class EventTesterEditor : Editor | |
{ | |
/// <summary> | |
/// Called to draw custom inspector components in an editor script. | |
/// </summary> | |
public override void OnInspectorGUI() | |
{ | |
// Draw event list | |
DrawDefaultInspector(); | |
// Draw button | |
GUIStyle btStyle = new GUIStyle(GUI.skin.button); | |
btStyle.fixedHeight = 23; | |
btStyle.margin = new RectOffset(50, 50, 10, 10); | |
btStyle.fontSize = 12; | |
if (GUILayout.Button("Fire Event", btStyle)) | |
((EventTester)target).TestEvent.Invoke(); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment