public static void InitializeTrigger(VRC.SDKBase.VRC_Trigger trigger) { CyanEmuTriggerHelper helper = trigger.gameObject.AddComponent <CyanEmuTriggerHelper>(); helper.SetTrigger(trigger as VRC_Trigger); trigger.ExecuteTrigger = helper.ExecuteTrigger; }
public static void RemoveKeyTrigger(CyanEmuTriggerHelper trigger) { if (instance_ == null) { return; } instance_.keyTriggers_.Remove(trigger); }
public static void AddKeyTrigger(CyanEmuTriggerHelper trigger) { if (instance_ == null) { return; } instance_.keyTriggers_.Add(trigger); }
public static void InitializeTrigger(VRC.SDKBase.VRC_Trigger trigger) { CyanEmuTriggerHelper helper = trigger.gameObject.AddComponent <CyanEmuTriggerHelper>(); helper.SetTrigger(trigger as VRC_Trigger); if (trigger.ExecuteTrigger == null) { trigger.ExecuteTrigger = new Action <VRC_Trigger.TriggerEvent>(helper.ExecuteTrigger); } }
private static void ExecuteEventForObject(VRC_EventHandler.VrcEvent triggerEvent, GameObject obj, VRC_EventHandler.EventInfo eventInfo, VRC_Trigger.TriggerEvent originalTrigger) { instance_.Log("Executing Trigger Event: " + triggerEvent.GetEventAsString(obj) + "\n_On Trigger: " + originalTrigger?.GetTriggerEventAsString()); bool isBufferedExecution = eventInfo.broadcast.IsBufferedBroadcastType() && eventInfo.instagator == null; if (eventInfo.broadcast.IsOwnerBroadcastType()) { if (!Networking.IsOwner(obj) || isBufferedExecution) { instance_.LogWarning("Not executing as user is not the owner"); return; } } if (!isBufferedExecution && eventInfo.broadcast.IsMasterBroadcastType()) { if (!Networking.IsMaster || isBufferedExecution) { instance_.LogWarning("Not executing as user is not the master"); return; } } if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.ActivateCustomTrigger) { // Only activates first one VRC_Trigger trigger = obj.GetComponent <VRC_Trigger>(); if (obj.activeInHierarchy && trigger.enabled) { trigger.ExecuteCustomTrigger(triggerEvent.ParameterString); } } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationBool) { Animator animator = obj.GetComponent <Animator>(); bool value = animator.GetBool(triggerEvent.ParameterString); bool newValue = VRC_EventHandler.BooleanOp(triggerEvent.ParameterBoolOp, value); animator.SetBool(triggerEvent.ParameterString, newValue); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationInt) { Animator animator = obj.GetComponent <Animator>(); animator.SetInteger(triggerEvent.ParameterString, triggerEvent.ParameterInt); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationFloat) { Animator animator = obj.GetComponent <Animator>(); animator.SetFloat(triggerEvent.ParameterString, triggerEvent.ParameterFloat); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationIntAdd) { Animator animator = obj.GetComponent <Animator>(); int value = animator.GetInteger(triggerEvent.ParameterString); animator.SetInteger(triggerEvent.ParameterString, value + triggerEvent.ParameterInt); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationIntDivide) { Animator animator = obj.GetComponent <Animator>(); int value = animator.GetInteger(triggerEvent.ParameterString); animator.SetInteger(triggerEvent.ParameterString, value / triggerEvent.ParameterInt); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationIntMultiply) { Animator animator = obj.GetComponent <Animator>(); int value = animator.GetInteger(triggerEvent.ParameterString); animator.SetInteger(triggerEvent.ParameterString, value * triggerEvent.ParameterInt); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationIntSubtract) { Animator animator = obj.GetComponent <Animator>(); int value = animator.GetInteger(triggerEvent.ParameterString); animator.SetInteger(triggerEvent.ParameterString, value - triggerEvent.ParameterInt); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AnimationTrigger) { obj.GetComponent <Animator>().SetTrigger(triggerEvent.ParameterString); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.PlayAnimation) { obj.GetComponent <Animation>().Play(triggerEvent.ParameterString); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AudioTrigger) { AudioSource[] audioSources = obj.GetComponents <AudioSource>(); foreach (var source in audioSources) { if (source.clip.name == triggerEvent.ParameterString) { source.Play(); } } } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.DestroyObject) { Destroy(obj); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SendRPC) { object[] parameters = VRC_Serialization.ParameterDecoder(triggerEvent.ParameterBytes); Type[] parameterTypes = new Type[parameters.Length]; for (int paramIndex = 0; paramIndex < parameters.Length; ++paramIndex) { parameterTypes[paramIndex] = parameters[paramIndex].GetType(); } foreach (MonoBehaviour mono in obj.GetComponents <MonoBehaviour>()) { MethodInfo methodInfo = mono.GetType().GetMethod(triggerEvent.ParameterString, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, Type.DefaultBinder, parameterTypes, null); if (methodInfo != null) { methodInfo.Invoke(mono, parameters); } } } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetComponentActive) { Type type = instance_.GetTypeForComponent(triggerEvent.ParameterString); if (type != null) { PropertyInfo property = type.GetProperty("enabled"); if (property != null) { foreach (Component component in obj.GetComponents(type)) { bool value = (bool)property.GetValue(component, null); bool newValue = VRC_EventHandler.BooleanOp(triggerEvent.ParameterBoolOp, value); property.SetValue(component, newValue, null); } } } } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetGameObjectActive) { bool newValue = VRC_EventHandler.BooleanOp(triggerEvent.ParameterBoolOp, obj.activeSelf); obj.SetActive(newValue); CyanEmuTriggerHelper triggerHelper = obj.GetComponent <CyanEmuTriggerHelper>(); if (triggerHelper != null && isTriggerGlobalBroadcast_) { if (newValue && triggerHelper.HasGlobalOnDisable) { instance_.LogWarning("Posisble OnEnable or OnTimer oversync!"); } else if (!newValue && triggerHelper.HasGlobalOnDisable) { instance_.LogWarning("Posisble OnDisable oversync!"); } } } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetLayer) { obj.layer = triggerEvent.ParameterInt; } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetMaterial) { Material mat = VRC_SceneDescriptor.GetMaterial(triggerEvent.ParameterString); obj.GetComponent <Renderer>().material = mat; } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetParticlePlaying) { ParticleSystem system = obj.GetComponent <ParticleSystem>(); bool newValue = VRC_EventHandler.BooleanOp(triggerEvent.ParameterBoolOp, system.isPlaying); if (newValue) { system.Play(); } else { system.Stop(); } } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetUIText) { obj.GetComponent <UnityEngine.UI.Text>().text = triggerEvent.ParameterString; } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SpawnObject) { GameObject prefab = VRC_SceneDescriptor.GetPrefab(triggerEvent.ParameterString); CyanEmuMain.SpawnObject(prefab, obj.transform.position, obj.transform.rotation); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.TeleportPlayer) { if (isBufferedExecution) { instance_.LogWarning("Teleport player actions should not be buffered. Ignoring"); } else { if (CyanEmuPlayerController.instance != null) { CyanEmuPlayerController.instance.Teleport(triggerEvent.ParameterObjects[0].transform, triggerEvent.ParameterBoolOp == VRC_EventHandler.VrcBooleanOp.True); } else { instance_.LogWarning("No player container to teleport!"); } if (eventInfo.broadcast != VRC_EventHandler.VrcBroadcastType.Local) { instance_.LogWarning("TeleportPlayer action should be set to local!"); } } } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AddForce) { HandleTriggerPhysicsEvent(triggerEvent, obj); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AddVelocity) { HandleTriggerPhysicsEvent(triggerEvent, obj); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetVelocity) { HandleTriggerPhysicsEvent(triggerEvent, obj); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AddAngularVelocity) { HandleTriggerPhysicsEvent(triggerEvent, obj); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetAngularVelocity) { HandleTriggerPhysicsEvent(triggerEvent, obj); } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AddDamage) { HandleTriggerDamageEvent(triggerEvent, obj); if (isBufferedExecution) { instance_.LogWarning("AddDamage action should not be buffered!"); } else if (eventInfo.broadcast != VRC_EventHandler.VrcBroadcastType.Local) { instance_.LogWarning("AddDamage action should be set to local!"); } } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.AddHealth) { HandleTriggerDamageEvent(triggerEvent, obj); if (isBufferedExecution) { instance_.LogWarning("AddHealth action should not be buffered!"); } else if (eventInfo.broadcast != VRC_EventHandler.VrcBroadcastType.Local) { instance_.LogWarning("AddHealth action should be set to local!"); } } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetWebPanelURI) { } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.SetWebPanelVolume) { } else if (triggerEvent.EventType == VRC_EventHandler.VrcEventType.MeshVisibility) { } }
private void CheckForMissingComponents() { Collider stationCollider = GetComponent <Collider>(); if (stationCollider == null) { gameObject.AddComponent <BoxCollider>().isTrigger = true; } #if UDON && UNITY_EDITOR UdonBehaviour udon = GetComponent <UdonBehaviour>(); if (udon == null) { udon = gameObject.AddComponent <UdonBehaviour>(); udon.interactText = "Sit"; AbstractUdonProgramSource program = UnityEditor.AssetDatabase.LoadAssetAtPath <AbstractUdonProgramSource>("Assets/VRChat Examples/Prefabs/VRCChair/StationGraph.asset"); if (program != null) { udon.AssignProgramAndVariables(program.SerializedProgramAsset, new UdonVariableTable()); } } #endif #if VRC_SDK_VRCSDK2 // Auto add a Interact Trigger to use the station VRC_Trigger trigger = GetComponent <VRC_Trigger>(); if (trigger == null) { trigger = gameObject.AddComponent <VRCSDK2.VRC_Trigger>(); trigger.Triggers = new List <VRC_Trigger.TriggerEvent>(); trigger.interactText = "Sit"; VRC_Trigger.TriggerEvent onInteract = new VRC_Trigger.TriggerEvent { BroadcastType = VRC_EventHandler.VrcBroadcastType.Local, TriggerType = VRC_Trigger.TriggerType.OnInteract, Events = new List <VRC_EventHandler.VrcEvent>() }; VRC_EventHandler.VrcEvent useStationEvent = new VRC_EventHandler.VrcEvent { EventType = VRC_EventHandler.VrcEventType.SendRPC, ParameterString = "UseStation", ParameterObjects = new[] { gameObject }, ParameterInt = 6, }; onInteract.Events.Add(useStationEvent); trigger.Triggers.Add(onInteract); // Reinitialize the trigger now that it has the proper events added. // Note that this only works as there were no vrc triggers on this object before. CyanEmuTriggerHelper helper = GetComponent <CyanEmuTriggerHelper>(); if (helper != null) { DestroyImmediate(helper); } CyanEmuTriggerHelper.InitializeTrigger(trigger); } #endif }