Skip to content

Instantly share code, notes, and snippets.

@TigerHix
Created July 25, 2023 02:17
Show Gist options
  • Save TigerHix/fe35442e9052cd8c4ea80e0261349321 to your computer and use it in GitHub Desktop.
Save TigerHix/fe35442e9052cd8c4ea80e0261349321 to your computer and use it in GitHub Desktop.
Stickers
using System;
using Cysharp.Threading.Tasks;
using UnityEngine;
using Warudo.Core;
using Warudo.Core.Attributes;
using Warudo.Core.Data;
using Warudo.Core.Resource;
using Warudo.Core.Utils;
using Warudo.Plugins.Core.Utils;
namespace Warudo.Plugins.Interactions.Nodes {
[NodeType(Id = "f02094b5-36a7-41b5-8dc7-868cef52d9d1", Title = "SPAWN_LOCAL_IMAGE_STICKER", Category = "CATEGORY_INTERACTIONS")]
public class SpawnLocalImageStickerNode : SpawnStickerNode {
[DataInput(order: -1000)]
[Label("IMAGE_SOURCE")]
[PreviewGallery]
[AutoCompleteResource("Image")]
public string ImageSource;
[Trigger(order: -999)]
[Label("OPEN_IMAGES_FOLDER")]
public void OpenImagesFolder() {
var url = "file:///" + Application.streamingAssetsPath + "/Images";
Debug.Log("Launching " + url);
Application.OpenURL(url);
}
protected override async UniTask<ImageResource> CreateImageResource() => createdImageResource;
protected override void OnCreate() {
base.OnCreate();
Watch(nameof(ImageSource), UpdateImageSource);
UpdateImageSource();
}
protected void UpdateImageSource() {
if (createdImageResource != null) {
async void DestroyLater(ImageResource res) {
await UniTask.Delay(TimeSpan.FromSeconds(Duration));
res.Destroy();
}
DestroyLater(createdImageResource);
}
if (ImageSource.IsNullOrWhiteSpace()) {
return;
}
createdImageResource = Context.ResourceManager.ResolveResourceUri<ImageResource>(ImageSource);
}
private ImageResource createdImageResource;
}
}
using System;
using Cysharp.Threading.Tasks;
using Warudo.Core;
using Warudo.Core.Attributes;
using Warudo.Core.Utils;
using Warudo.Plugins.Core.Utils;
using Warudo.Plugins.Interactions;
namespace Warudo.Plugins.Interactions.Nodes {
[NodeType(Id = "6976de36-2423-4ae4-8da0-1bc07d0b6fe7", Title = "SPAWN_ONLINE_IMAGE_STICKER", Category = "CATEGORY_INTERACTIONS")]
public class SpawnOnlineImageStickerNode : SpawnStickerNode {
[DataInput(order: -1000)]
[Label("URL")]
public string Url;
protected override async UniTask<ImageResource> CreateImageResource() {
if (Url.IsNullOrWhiteSpace()) {
return null;
}
var url = Url.Trim();
Uri.TryCreate(url, UriKind.Absolute, out var result);
if (result == null) {
return null;
}
return await Context.PluginManager.GetPlugin<InteractionsPlugin>().ImageCache.GetImageResource(url);
}
}
}
using Cysharp.Threading.Tasks;
using Warudo.Core;
using Warudo.Core.Attributes;
using Warudo.Core.Data;
using Warudo.Core.Graphs;
using Warudo.Core.Resource;
using Warudo.Plugins.Core.Assets;
using Warudo.Plugins.Core.Assets.Character;
using Warudo.Plugins.Core.Utils;
using Warudo.Plugins.Interactions.Mixins;
using Warudo.Plugins.Interactions.Utils;
namespace Warudo.Plugins.Interactions.Nodes {
public abstract class SpawnStickerNode : Node {
protected abstract UniTask<ImageResource> CreateImageResource();
[DataInput]
[PreviewGallery]
[Label("TRAIL_SOURCE")]
[AutoCompleteResource("Prop")]
public string TrailSource;
[DataInput]
[Label("TRAIL_INTENSITY")]
public float TrailIntensity = 1f;
[DataInput]
[Label("TARGET_CHARACTER")]
public CharacterAsset Character;
[Mixin]
public FromTo FromTo;
[DataInput]
[Label("SCALE")]
[FloatSlider(0.1f, 3f)]
public float Scale = 1f;
[DataInput]
[Label("DURATION")]
[FloatSlider(0.5f, 5f)]
public float Duration = 2f;
[FlowInput]
public Continuation Enter() {
if (Character.IsNullOrInactiveOrDisabled()) {
return Exit;
}
SpawnSticker();
return Exit;
}
protected override void OnCreate() {
base.OnCreate();
FromTo.CharacterGetter = () => Character;
}
protected async void SpawnSticker() {
var image = await CreateImageResource();
if (image == null) return;
if (Context.IsDestroyed) return;
FromTo.CharacterBodyPosition = Character.SampleVisiblePointOnHumanoidCharacter(FromTo.FinalFromCamera);
Sticker.Create(image, Scale * 1.5f)
.Move(FromTo.FromPosition, FromTo.ToPositionFunction, Duration, FromTo.FinalFromCamera)
.WithTrail(TrailSource, TrailIntensity);
}
[FlowOutput]
public Continuation Exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment