Unity3D: UnityEvent with inputs does not appear in editor - unity3d

Does anyone know how make complex UnityEvent appear in editor?
public class Test : MonoBehaviour
{
public UnityEvent myEvent;
public UnityEvent<string> myAnotherEvent;
}
myEvent appears just fine, but myAnotherEvent does not.
Thanks in advance.
UPD Also tried to override the UnityEvent like:
public class NewEvent : UnityEvent<string>
{
}
public class Test : MonoBehaviour
{
public UnityEvent myEvent;
public NewEvent myAnotherEvent;
}
didn't seem to help.

[System.Serializable]
public class NewEvent : UnityEvent<string>
{
}
public class Test : MonoBehaviour
{
public NewEvent myAnotherEvent;
}
You need to inform that your new class is serializable.

Related

Custom onClick list

Can i create custom region with grouped methods for list onClick like dynamic and statics?
like this
Yes and no! ^^
Yes, you can create your own event type taking a parameter and assign dynamic callbacks to it. What you are looking for is UnityEvent.
For the dynamic parameterized ones see UnityEvent<T0> to UnityEvent<T0, T1, T2, T3> depending on how many parameters you need.
For the example with a single int it would be (exactly as in the API example)
// Since Unity doesn't support direct serialization of generics you have to implement this [Serializable] wrapper
[Serializable]
public class MyIntEvent : UnityEvent<int>
{
}
public class ExampleClass : MonoBehaviour
{
public MyIntEvent m_MyEvent;
}
No, you can not simply change the existing implementation of UI.Button.onClick which is parameterless.
What you could do, however, is build a new component and attach it on a button like
[RequireComponent(typeof(Button))]
public class ExampleClass : MonoBehaviour
{
[SerializeField] private Button _button;
public MyIntEvent onClickWithIntParameter;
private void Awake()
{
if(!_button) _button = GetComponent<Button>();
_button.onClick.AddListener(HandleOnClick);
}
private void HandleOnClick()
{
// Wherever you get your int from
var value = 123;
onClickWithIntParameter.Invoke(value);
}
}
In the case that [Serializable] isn't working for you try [System.Serializable] or using System; at the top.
For Those who want to create a custom event Script:
I made a collision detection script. The events set in the inspector are called when a collision is detected. Just like a button.
https://i.stack.imgur.com/r4epP.png
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.Events;
public class CollisionCallFunc : MonoBehaviour {
[System.Serializable]
public class CollisionEvent : UnityEvent<object> {
public object value;
}
[SerializeField]
private CollisionEvent collisionEvents = new();
private void OnCollisionEnter(Collision collision) {
try {
collisionEvents.Invoke(collisionEvents.value);
} catch(System.Exception exception) {
Debug.LogWarning("Couldn't invoke action. Error:");
Debug.LogWarning(exception.Message);
}
}
private void OnCollisionEnter2D(Collision2D collision) {
try {
collisionEvents.Invoke(collisionEvents.value);
} catch(System.Exception exception) {
Debug.LogWarning("Couldn't invoke action. Error:");
Debug.LogWarning(exception.Message);
}
}
}

Make a gameObject follow another gameObject's behavior?

I am controlling a text GameObject. I am performing operations such as SetActive() and changing the text of the gameObject. Now I am duplicating the same object and I want the duplicated object to follow the behaviors of its main gameObject. That is ObjA is parent and ObjB is a clone. If I change the text UI through code of objA, I want objB to automatically change its component. How do I achieve this behavior?
There are multiple ways to do that, but it will always be by code, there is no "authomatism" to do that.
So by code you can make a relation child to parent or parent to child.
One way could be that one:
public class Parent : MonoBehaviour
{
public Child child = null;
public void DoSomething()
{
this.gameObject.SetActive(true);
child.DoSomething();
}
}
public class Child : MonoBehaviour
{
public void DoSomething()
{
this.gameObject.SetActive(true);
}
}
Another fancy way to do that is using delegates, or Actions:
public class Parent : MonoBehaviour
{
public Action OnDoSomething = null;
public Action OnDoSomethingElse = null;
public void DoSomething()
{
this.gameObject.SetActive(false);
OnDoSomething();
}
public void DoSomethingElse()
{
this.gameObject.SetActive(true);
OnDoSomethingElse();
}
}
public class Child : MonoBehaviour
{
public Parent parent = null;
public void Awake()
{
parent.OnDoSomething += ChildDoSometing;
parent.OnDoSomethingElse += ChildDoSometingElse;
}
public void OnDestroy()
{
parent.OnDoSomething -= ChildDoSometing;
parent.OnDoSomethingElse -= ChildDoSometingElse;
}
public void ChildDoSometing()
{
this.gameObject.SetActive(false);
}
public void ChildDoSometingElse()
{
this.gameObject.SetActive(false);
}
}
You can be tempted to pass your own method as Action parameter like Action<Action> but remember that child will call parent method, won't operate on his own. So in this case if you do something like:
public class Parent : MonoBehaviour
{
public Action<Action> OnDoSomething = null;
[ContextMenu("A")]
public void DoSomething()
{
this.gameObject.SetActive(true);
OnDoSomething(this.DoSomething);
}
public void DoSomethingElse()
{
print("Hello");
}
}
public class Child : MonoBehaviour
{
public Parent parent = null;
public void Awake()
{
parent.OnDoSomething += RepeatedAction;
}
public void OnDestroy()
{
parent.OnDoSomething -= RepeatedAction;
}
public void RepeatedAction(Action actionToRepeat)
{
actionToRepeat?.Invoke();
}
}
Will result on StackOverflow exception, cause child will call parent, who calls child, who calls again parent...you can see the problem.
Anyway would be nice to declare an abstract base class that have all the methods, and let both classes inherit from that class, and implement those methods.

Calling Invoke within a public static function Unity

I'm getting an error that I don't understand. The simplified version of my code:
using UnityEngine;
public class RunLater : MonoBehaviour
{
public static void Do()
{
Invoke("RunThisLater", 2.0f);
}
public void RunThisLater()
{
Debug.Log("This will run later");
}
}
You can pass it in as a parameter like this:
public class RunLater : MonoBehaviour
{
public static void Do(RunLater instance)
{
instance.Invoke("RunThisLater", 2.0f);
}
public void RunThisLater()
{
Debug.Log("This will run later");
}
}
One approach is to have the static parts of the class store a MonoBehaviour reference to itself. Like so:
public class RunLater : MonoBehaviour
{
public static RunLater selfReference = null;
public static void Do()
{
InitSelfReference();
selfReference.DoInstanced();
}
static void InitSelfReference()
{
if (selfReference == null)
{
// We're presuming you only have RunLater once in the entire hierarchy.
selfReference = Object.FindObjectOfType<RunLater>();
}
}
public void DoInstanced()
{
Invoke("RunThisLater", 2f);
}
void RunThisLater()
{
Debug.Log("This will run later");
}
}
You would now be able to call RunLater.Do() from anywhere in your code of other gameObjects. Good luck!

Can't draw Serializable class on Unity Inspector

This is my Example class and MonoBehaviour script:
using UnityEngine;
using System;
[Serializable]
public class TestSerializableClass
{
public int a;
public string b;
}
public class TestScript : MonoBehaviour
{
public TestSerializableClass field1;
[HideInInspector] public TestSerializableClass field2;
}
and this is the editor script for the MonoBehaviour Script
using UnityEditor;
[CustomEditor(typeof(TestScript))]
public class TestScriptEditor : Editor
{
SerializedProperty testClass;
public override void OnInspectorGUI()
{
DrawDefaultInspector();
SerializedProperty testField = serializedObject.FindProperty("field2");
EditorGUILayout.PropertyField(testField, true);
}
}
This is how my inspector for TestScript looks.
Why doesn't it draw the "field2" varialble like the first one?
Can I do it with Editor script and property field? How?
(the includeChildre flag is true)
Remove the HideInInspector attribute from field2

See child gameObjects from code

Is it possible to put parent gameObject in UnityEditor and see child gameObjects from code? I tried to create custom class with necessary structure, nut unity doesn't understand it.
public Path Path1;
public Path Path2;
public Path Path3;
public Path Path4;
[Serializable]
public class Path : MonoBehaviour
{
public List<Way> Ways;
public Path ()
{
}
}
Way class
[Serializable]
public class Way : MonoBehaviour
{
public Transform StartPoint { get; set;}
public Transform[] EndPoints { get; set;}
public Transform[] AngleWaypoints { get; set;}
public Way ()
{
}
public Way (Transform startPoint, Transform[] endPoints, Transform[] angleWaypoints)
{
this.StartPoint = startPoint;
this.EndPoints = endPoints;
this.AngleWaypoints = angleWaypoints;
}
}
Your class needs to inherit from MonoBehaviour, maybe it is it that your missing.
It should look like
class myClass : MonoBehaviour{
}
You have selected your GameManager script. Try selecting Path_1, all public variables of Path class should appear in the UnityEditor.
Also you can change the UnityEditor Mode to Debug, which will show much more information UnityEditor Debug Mode