Instantly share code, notes, and snippets.
Last active
July 26, 2023 14:18
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save RobTranquillo/58613de916a8493475b05433c2d46e11 to your computer and use it in GitHub Desktop.
Extend the basic VR-Builder "Enable by Tag" to support a second tag.
This file contains hidden or 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
using VRBuilder.Core.Behaviors; | |
using VRBuilder.Editor.UI.StepInspector.Menu; | |
namespace VRBuilder.Editor.UI.Behaviors | |
{ | |
/// <inheritdoc /> | |
public class DisableObjectsWithTagsMenuItem : MenuItem<IBehavior> | |
{ | |
/// <inheritdoc /> | |
public override string DisplayedName { get; } = "Environment/Disable Objects By Tags"; | |
/// <inheritdoc /> | |
public override IBehavior GetNewItem() | |
{ | |
return new SetObjectsWithTagsEnabledBehavior(false); | |
} | |
} | |
} |
This file contains hidden or 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
using VRBuilder.Core.Behaviors; | |
using VRBuilder.Editor.UI.StepInspector.Menu; | |
namespace VRBuilder.Editor.UI.Behaviors | |
{ | |
/// <inheritdoc /> | |
public class EnableObjectsWithTagsMenuItem : MenuItem<IBehavior> | |
{ | |
/// <inheritdoc /> | |
public override string DisplayedName { get; } = "Environment/Enable Objects By Tags"; | |
/// <inheritdoc /> | |
public override IBehavior GetNewItem() | |
{ | |
return new SetObjectsWithTagsEnabledBehavior(true); | |
} | |
} | |
} |
This file contains hidden or 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
// This is just Mindport's default SetObjectsWithTagEnabledBehavior.cs file, extended with a second tag. So only objects that contain both tags will be enabled. | |
// If you need more tags, just add more, it's simple. | |
// I tried to provide an array of tags, but failed to display an array of tags in the ProcessInspector. | |
// I put the files right next to the original EnableObjectsWithTagMenuItem.cs and SetObjectsWithTagEnabledBehavior.cs, but that won't update-save | |
using Newtonsoft.Json; | |
using System; | |
using System.Diagnostics; | |
using System.Runtime.Serialization; | |
using UnityEngine.Scripting; | |
using VRBuilder.Core.Attributes; | |
using VRBuilder.Core.Configuration; | |
using VRBuilder.Core.SceneObjects; | |
using VRBuilder.Core.Settings; | |
namespace VRBuilder.Core.Behaviors | |
{ | |
/// <summary> | |
/// Sets enabled or disabled all objects with a given tag. | |
/// </summary> | |
[DataContract(IsReference = true)] | |
public class SetObjectsWithTagsEnabledBehavior : Behavior<SetObjectsWithTagsEnabledBehavior.EntityData> | |
{ | |
/// <summary> | |
/// Behavior data for <see cref="SetObjectsWithTagsEnabledBehavior"/>. | |
/// </summary> | |
[DisplayName("Enable Objects by Tag")] | |
[DataContract(IsReference = true)] | |
public class EntityData : IBehaviorData | |
{ | |
/// <summary> | |
/// The object to enable. | |
/// </summary> | |
[DataMember] | |
[DisplayName("Tag 1")] | |
public SceneObjectTag<ISceneObject> Tag { get; set; } | |
/// <summary> | |
/// The object to enable. | |
/// </summary> | |
[DataMember] | |
[DisplayName("Tag 2")] | |
public SceneObjectTag<ISceneObject> Tag2 { get; set; } | |
[DataMember] | |
[HideInProcessInspector] | |
public bool SetEnabled { get; set; } | |
/// <inheritdoc /> | |
public Metadata Metadata { get; set; } | |
[DataMember] | |
[DisplayName("Revert after step is complete")] | |
public bool RevertOnDeactivation { get; set; } | |
/// <inheritdoc /> | |
[IgnoreDataMember] | |
public string Name | |
{ | |
get | |
{ | |
string setEnabled = SetEnabled ? "Enable" : "Disable"; | |
return $"{setEnabled} objects with multiple tags"; | |
} | |
} | |
} | |
private class ActivatingProcess : InstantProcess<EntityData> | |
{ | |
public ActivatingProcess(EntityData data) : base(data) | |
{ | |
} | |
/// <inheritdoc /> | |
public override void Start() | |
{ | |
foreach(ISceneObject sceneObject in RuntimeConfigurator.Configuration.SceneObjectRegistry.GetByTag(Data.Tag.Guid)) | |
{ | |
if (((ITagContainer)sceneObject).HasTag(Data.Tag2.Guid)) | |
RuntimeConfigurator.Configuration.SceneObjectManager.SetSceneObjectActive(sceneObject, Data.SetEnabled); | |
} | |
} | |
} | |
private class DeactivatingProcess : InstantProcess<EntityData> | |
{ | |
public DeactivatingProcess(EntityData data) : base(data) | |
{ | |
} | |
/// <inheritdoc /> | |
public override void Start() | |
{ | |
if (Data.RevertOnDeactivation) | |
{ | |
foreach (ISceneObject sceneObject in RuntimeConfigurator.Configuration.SceneObjectRegistry.GetByTag(Data.Tag.Guid)) | |
{ | |
RuntimeConfigurator.Configuration.SceneObjectManager.SetSceneObjectActive(sceneObject, !Data.SetEnabled); | |
} | |
} | |
} | |
} | |
[JsonConstructor, Preserve] | |
public SetObjectsWithTagsEnabledBehavior() : this(Guid.Empty, false) | |
{ | |
} | |
public SetObjectsWithTagsEnabledBehavior(bool setEnabled) : this(Guid.Empty, setEnabled, false) | |
{ | |
} | |
public SetObjectsWithTagsEnabledBehavior(Guid tag, bool setEnabled, bool revertOnDeactivate = false) | |
{ | |
Data.Tag = new SceneObjectTag<ISceneObject>(tag); | |
Data.Tag2 = new SceneObjectTag<ISceneObject>(tag); | |
Data.SetEnabled = setEnabled; | |
Data.RevertOnDeactivation = revertOnDeactivate; | |
} | |
/// <inheritdoc /> | |
public override IStageProcess GetActivatingProcess() | |
{ | |
return new ActivatingProcess(Data); | |
} | |
public override IStageProcess GetDeactivatingProcess() | |
{ | |
return new DeactivatingProcess(Data); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment