Skip to content

Instantly share code, notes, and snippets.

@esnya
Created July 7, 2021 09:24
Show Gist options
  • Save esnya/c880d464cc3125f97839a6765f7e5e43 to your computer and use it in GitHub Desktop.
Save esnya/c880d464cc3125f97839a6765f7e5e43 to your computer and use it in GitHub Desktop.
UdonChipsInstantShop Trigger
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon.Common.Interfaces;
#if !COMPILER_UDONSHARP && UNITY_EDITOR
using System.Linq;
using System.Reflection;
using UdonSharpEditor;
using UnityEditor;
#endif
namespace UCS.EsnyaUCS
{
[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
public class UdonChipsInstantShop_Trigger : UdonSharpBehaviour
{
public float price = 150.0f;
public bool oneTimeOnly;
public bool networked;
public NetworkEventTarget networkEventTarget;
public UdonSharpBehaviour[] eventTargets = {};
public string[] eventNames = {};
[UdonSynced] private bool oneTimeBought;
private UdonChips udonChips;
private UdonChipsShopAnimator button;
private void Start()
{
udonChips = GameObject.Find("UdonChips").GetComponent<UdonChips>();
button = GetComponentInChildren<UdonChipsShopAnimator>();
}
private void Update()
{
button.isLackOfFunds = price > udonChips.money;
button.isSoldout = oneTimeOnly && oneTimeBought;
}
private void TakeOwnership()
{
if (Networking.IsOwner(gameObject)) return;
Networking.SetOwner(Networking.LocalPlayer, gameObject);
}
private void DispatchEvents()
{
if (eventTargets == null || eventNames == null) return;
var count = Mathf.Min(eventTargets.Length, eventNames.Length);
for (int i = 0; i < count; i++)
{
var eventTarget = eventTargets[i];
if (eventTarget == null) continue;
var eventName = eventNames[i];
if (networked) eventTarget.SendCustomNetworkEvent(networkEventTarget, eventName);
else eventTarget.SendCustomEvent(eventName);
}
}
public override void OnDeserialization()
{
if (oneTimeOnly && oneTimeBought) button.OnSoldout();
}
public override void Interact()
{
if (oneTimeOnly && oneTimeBought)
{
button.OnSoldout();
return;
}
if (udonChips.money < price)
{
button.OnLackOfFunds();
return;
}
udonChips.money -= price;
button.OnBuy();
if (oneTimeOnly)
{
TakeOwnership();
oneTimeBought = true;
RequestSerialization();
}
DispatchEvents();
}
}
#if !COMPILER_UDONSHARP && UNITY_EDITOR
[CustomEditor(typeof(UdonChipsInstantShop_Trigger))]
public class UdonChipsInstantShop_TriggerEditor : Editor
{
public override void OnInspectorGUI()
{
if (UdonSharpGUI.DrawDefaultUdonSharpBehaviourHeader(target)) return;
serializedObject.Update();
var property = serializedObject.GetIterator();
property.NextVisible(true);
while (property.NextVisible(false))
{
if (property.name == nameof(UdonChipsInstantShop_Trigger.eventTargets))
{
EditorGUILayout.PropertyField(property, false);
if (!property.isExpanded) continue;
var namesProperty = serializedObject.FindProperty(nameof(UdonChipsInstantShop_Trigger.eventNames));
namesProperty.arraySize = property.arraySize;
for (int i = 0; i < property.arraySize; i++)
{
using (new EditorGUILayout.HorizontalScope())
{
var eventTargetProperty = property.GetArrayElementAtIndex(i);
var eventNameProperty = namesProperty.GetArrayElementAtIndex(i);
if (GUILayout.Button("▲", EditorStyles.miniButtonLeft, GUILayout.ExpandWidth(false)))
{
property.MoveArrayElement(i, i - 1);
namesProperty.MoveArrayElement(i, i - 1);
}
if (GUILayout.Button("▼", EditorStyles.miniButtonRight, GUILayout.ExpandWidth(false)))
{
property.MoveArrayElement(i, i + 1);
namesProperty.MoveArrayElement(i, i + 1);
}
EditorGUILayout.PropertyField(eventTargetProperty, new GUIContent(), false);
var events = (eventTargetProperty.objectReferenceValue?.GetType()?.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public) ?? Enumerable.Empty<MethodInfo>()).Select(info => info.Name).ToArray();
var index = events.Select((e, j) => (e, j)).Where(t => t.e == eventNameProperty.stringValue).Select(t => t.j).FirstOrDefault();
index = EditorGUILayout.Popup(index, events);
eventNameProperty.stringValue = events.Skip(index).FirstOrDefault();
if (GUILayout.Button("-", EditorStyles.miniButton, GUILayout.ExpandWidth(false)))
{
property.DeleteArrayElementAtIndex(i);
namesProperty.DeleteArrayElementAtIndex(i);
break;
}
}
}
if (GUILayout.Button("Add Element"))
{
property.arraySize++;
}
}
else if (property.name == nameof(UdonChipsInstantShop_Trigger.eventNames))
{
}
else
{
EditorGUILayout.PropertyField(property, true);
}
}
serializedObject.ApplyModifiedProperties();
}
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment