Это реализация BeginContact
в InteractionBehaviour
:
public void BeginContact(List<InteractionController> controllers)
{
foreach (var controller in controllers)
{
_contactingControllers.Add(controller);
OnPerControllerContactBegin(controller);
}
if (_contactingControllers.Count == controllers.Count)
{
OnContactBegin();
}
}
как вы можете видеть, соответствующие действия называются
public Action OnContactBegin;
public Action<InteractionController> OnPerControllerContactBegin;
Итак, в зависимости от ваших потребностей
Прямое добавление обратных вызовов через скрипт
например с использованием
private void Awake()
{
var interactionBehaviour= objectReference.GetComponent<InteractionBehaviour>();
// either as Lambda Expression
interactionBehaviour.OnContactBegin += () =>
{
// do something here
};
// or with a method
// use this if you also want to be able to remove callbacks later!
// it is always save to remove a callback before adding it
// even if it's not there yet. Makes sure it is only added exactly once
interactionBehaviour.OnContactBegin -= DoSomething;
interactionBehaviour.OnContactBegin += DoSomething;
}
// make sure to always remove any callbacks as soon as possible or when they are not needed anymore
private void OnDestroy()
{
interactionBehaviour.OnContactBegin -= DoSomething;
}
private void DoSomething()
{
// do something here e.g. turn red
// for efficiency you would ofcourse move the GetComponent
// call to Awake as well. Just wanted to keep the example clean
GetComponent<Renderer>().material.color = Color.red;
}
Или сделайте это более общим
Наследуйте сценарий и настройте его с помощью UnityEvent
, которые открывают видимость собственного инспектора интерфейс (точно так же, как событие Button.onClick
).
Теоретически вы можете добавить UnityEvent
для каждого доступного Action
.. Я просто сделаю это для двух вышеупомянутых.
[Serializeable]
public class InteractionControllerEvent : UnityEvent<InteractionController> { }
public class ExtendedInteractionBehaviour : InteractionBehaviour
{
public UnityEvent OnContactBeginEvent;
public InteractionControllerEvent OnPerControllerContactBegin;
// and just as before add those as callbacks to the actions
private void Awake()
{
OnContactBegin -= OnContactBeginInvokeEvent;
OnContactBegin += OnContactBeginInvokeEvent;
OnPerControllerContactBegin -= OnPerControllerContactBeginInvokeEvent;
OnPerControllerContactBegin += OnPerControllerContactBeginInvokeEvent;
}
private void OnDestroy()
{
OnContactBegin -= OnContactBeginInvokeEvent;
OnPerControllerContactBegin -= OnPerControllerContactBeginInvokeEvent;
}
private void OnContactBeginInvokeEvent()
{
OnContactBeginEvent?.Invoke();
}
private void OnPerControllerContactBeginInvokeEvent(InteractionController controller)
{
OnPerControllerContactBegin?.Invoke(controller);
}
}
Теперь вы можете просто ссылаться на GameObjects, их компоненты и методы через Inspector, как вы, вероятно, знаете из упомянутого _ 11_ (это тоже UnityEvent
).
В частности, InteractionControllerEvent
- это пример, который позволяет вызывать с параметром dynamic, что означает, что соответствующая ссылка InteractionController
может быть передана методу, на который имеется ссылка, принимая InteractionController
в качестве параметра.
person
derHugo
schedule
24.07.2019